Skip to content

Commit 64dcebf

Browse files
committed
fd
1 parent 6ca8397 commit 64dcebf

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

doc/木桶原理.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
287. Find the Duplicate Number
2+
220. Contains Duplicate III
3+
164. Maximum Gap

solution/src/main/java/com/inuker/solution/ClosestBinarySearchTreeValueII.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
/**
1515
* 有三种解法:
1616
* 2,中序遍历两遍二叉树,不过每次都会中途退出,构成双栈,一个是大于target,一个是小于target,然后依次出栈满k为止,复杂度O(n + k)
17-
* 3,中序完整遍历一遍二叉树,依次将节点加入优先级队列,按与target的差排序,选前k个,复杂度O(nlgk)
1817
*/
1918
public class ClosestBinarySearchTreeValueII {
2019

solution/src/main/java/com/inuker/solution/FindTheDuplicateNumber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public int findDuplicate(int[] nums) {
2828
}
2929

3030
/**
31-
* 如果count>mid,说明元素个数count比桶数mid还打,则可以肯定重复的数在[1,mid]中
31+
* 如果count>mid,说明元素个数count比桶数mid还大,则可以肯定重复的数在[1,mid]中
3232
* 但是count<=mid就能确定重复的数肯定不在[1,mid]中么?不一定吧,接下来推理一下:
3333
* 我们看右边的一半,[mid+1,n]区间的数有n+1-count个,可见元素有n+1-count个,桶有n-mid个,而count<=mid等价于count-1<mid,
3434
* 因此元素个数大于桶数,右区间一定有重复的数,由于题目已经限定只有一个重复数,因此只能在右区间

solution/src/main/java/com/inuker/solution/LargestRectangleInHistogram.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public int largestRectangleArea(int[] heights) {
2929
return max;
3030
}
3131

32-
/** 注意栈中的是index,不是高度 */
32+
/** 注意栈中的是index,不是高度
33+
* 栈保持递增,当出现小于栈顶的元素时,意味着可以计算栈顶可以延伸的矩形面积了
34+
* 其左边界是次栈顶,右边界是当前元素
35+
*/
3336
public int largestRectangleArea2(int[] heights) {
3437
int max = 0;
3538
Stack<Integer> stack = new Stack<Integer>();
@@ -40,6 +43,10 @@ public int largestRectangleArea2(int[] heights) {
4043
} else {
4144
int top = stack.pop();
4245
int left = stack.isEmpty() ? 0 : stack.peek() + 1;
46+
/**
47+
* top两边都是比top高的
48+
* [left,i-1]
49+
*/
4350
max = Math.max(max, heights[top] * (i - 1 - left + 1));
4451
}
4552
}

0 commit comments

Comments
 (0)