1. 数据流变为多个不相交区间
 给你一个由非负整数 a1, a2, ..., an 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表。
实现 SummaryRanges 类:
- SummaryRanges()使用一个空数据流初始化对象。
- void addNum(int val)向数据流中加入整数- val。
- int[][] getIntervals()以不相交区间- [starti, endi]的列表形式返回对数据流中整数的总结。
示例:
输入: ["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"] [[], [1], [], [3], [], [7], [], [2], [], [6], []] 输出: [null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]] 解释: SummaryRanges summaryRanges = new SummaryRanges(); summaryRanges.addNum(1); // arr = [1] summaryRanges.getIntervals(); // 返回 [[1, 1]] summaryRanges.addNum(3); // arr = [1, 3] summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3]] summaryRanges.addNum(7); // arr = [1, 3, 7] summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3], [7, 7]] summaryRanges.addNum(2); // arr = [1, 2, 3, 7] summaryRanges.getIntervals(); // 返回 [[1, 3], [7, 7]] summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7] summaryRanges.getIntervals(); // 返回 [[1, 3], [6, 7]]
提示:
0 <= val <= 10^4
最多调用 addNum 和 getIntervals 方法 3 * 10^4 次
进阶:如果存在大量合并,并且与数据流的大小相比,不相交区间的数量很小,该怎么办?
出处:
https://edu.csdn.net/practice/25006605
代码:
class SummaryRanges { private final TreeSet<Integer> tree = new TreeSet<>(); public void addNum(int val) { tree.add(val); } public int[][] getIntervals() { ArrayList<int[]> result = new ArrayList<>(1 << 2); Iterator<Integer> iterator = tree.iterator(); int[] array = new int[tree.size()]; int i = 0; while (iterator.hasNext()) array[i++] = iterator.next(); int length = array.length; if (length == 0) return result.toArray(new int[0][]); int start = array[0]; for (i = 0; i < length; ++i) { if (i + 1 < length && array[i + 1] - array[i] != 1) { result.add(new int[] { start, array[i] }); start = array[i + 1]; } else if (i + 1 == length) { result.add(new int[] { start, array[i] }); } } return result.toArray(new int[result.size()][]); } }
输出:
略,示例解释部分即测试代码
2. 最小栈
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
- push(x)—— 将元素 x 推入栈中。
- pop()—— 删除栈顶的元素。
- top()—— 获取栈顶元素。
- getMin()—— 检索栈中的最小元素。
示例:
输入: ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]] 输出: [null,null,null,null,-3,null,0,-2] 解释: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); //--> 返回 -3. minStack.pop(); minStack.top(); //--> 返回 0. minStack.getMin(); //--> 返回 -2.
提示:
- pop、- top和- getMin操作总是在 非空栈 上调用。
出处:
https://edu.csdn.net/practice/25006606
代码:
class MinStack { Stack<Integer> data_stack; Stack<Integer> min_stack; /** initialize your data structure here. */ public MinStack() { data_stack = new Stack<Integer>(); min_stack = new Stack<Integer>(); } public void push(int x) { data_stack.push(x); if (min_stack.isEmpty()) { min_stack.push(x); } else { if (x > min_stack.peek()) { x = min_stack.peek(); } min_stack.push(x); } } public void pop() { data_stack.pop(); min_stack.pop(); } public int top() { return data_stack.peek(); } public int getMin() { return min_stack.peek(); } } /** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(x); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */
输出:
略,示例解释部分即测试代码
3. 柱状图中最大的矩形
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。
以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]。
图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。
示例:
输入: [2,1,5,6,2,3]
输出: 10
以下程序实现了这一功能,请你填补空白处内容:
```Java class Solution { public int largestRectangleArea(int[] heights) { int length = heights.length; if (length == 0) { return 0; } int maxSize = 0; for (int i = 0; i < length; i++) { int nowHeight = heights[i]; int nowWidth = 0; for (int j = i; j < length; j++) { ___________________; nowWidth++; if (maxSize < nowHeight * nowWidth) { maxSize = nowHeight * nowWidth; } } } return maxSize; } } ```
出处:
https://edu.csdn.net/practice/25006607
代码1: 暴力枚举
import java.util.*; public class largestRectangleArea { public static class Solution { public int largestRectangleArea(int[] heights) { int length = heights.length; if (length == 0) { return 0; } int maxSize = 0; for (int i = 0; i < length; i++) { int nowHeight = heights[i]; int nowWidth = 0; for (int j = i; j < length; j++) { if (heights[j] < nowHeight) { nowHeight = heights[j]; } nowWidth++; if (maxSize < nowHeight * nowWidth) { maxSize = nowHeight * nowWidth; } } } return maxSize; } } public static void main(String[] args) { Solution s = new Solution(); int[] heights = {2,1,5,6,2,3}; System.out.println(s.largestRectangleArea(heights)); } }
输出:
10
代码2:单调栈
import java.util.*; public class largestRectangleArea { public static class Solution { public int largestRectangleArea(int[] heights) { int n = heights.length; int[] left = new int[n]; // 存储每个矩形左边第一个小于它的矩形的下标 int[] right = new int[n]; // 存储每个矩形右边第一个小于它的矩形的下标 Stack<Integer> stack = new Stack<>(); // 单调栈,存储矩形的下标 for (int i = 0; i < n; i++) { while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) { stack.pop(); } left[i] = stack.isEmpty() ? -1 : stack.peek(); stack.push(i); } stack.clear(); for (int i = n - 1; i >= 0; i--) { while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) { stack.pop(); } right[i] = stack.isEmpty() ? n : stack.peek(); stack.push(i); } int maxArea = 0; for (int i = 0; i < n; i++) { int area = (right[i] - left[i] - 1) * heights[i]; // 计算以当前矩形为高的最大面积 maxArea = Math.max(maxArea, area); } return maxArea; } } public static void main(String[] args) { Solution s = new Solution(); int[] heights = {2,1,5,6,2,3}; System.out.println(s.largestRectangleArea(heights)); } }


 
                             
                 
                 
                 
                 
                 
                 
                