Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram
The largest rectangle is shown in the shaded area, which has area = 10
unit.
For example,
Given heights = [2,1,5,6,2,3]
,
return 10
.
在此程序中在i处,处理以i-1为右边界的最大面积;所以最开始push_back(0)来处理最后一个bar。
思路即当右侧的bar小于前一bar时,then we can compute the maximum area of zones which contain the complete bar which is longer than bar just in the right hand.
int largestRectangleArea(vector<int>& heights) {
int rst = 0;
vector<int> index;
heights.push_back(0);
for(int i = 0; i < heights.size(); i++){
while(index.size() > 0 && heights[index.back()] >= heights[i]){
int h = heights[index.back()];
index.pop_back();
int bound = index.size() > 0 ? index.back() : -1;
int width = i - 1 - bound;
rst = max(rst, width * h);
}
index.push_back(i);
}
return rst;
}
Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
For example, given the following matrix:
1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0Return 6.
解法1:使用histogram的思想,相当于逐层构建直方图,注意初始化二维数组为0的方式,不能用 = {0}的形式。
int maximalRectangle(vector<vector<char>>& matrix) {
if(matrix.empty()) return 0;
int rst = 0;
vector<int> index;
int n = matrix[0].size();
int heights[n+1] = {0};
int rowIndex = 1;
for(int i = 0; i < matrix.size(); i++){
matrix[i].push_back('0');
for(int j = 0; j < n+1; j++){
heights[j] = matrix[i][j] == '1' ? heights[j] + 1 : 0;
while(index.size() > 0 && heights[index.back()] >= heights[j]){
int h = heights[index.back()];
index.pop_back();
int bound = index.size() > 0 ? index.back() : -1;
int width = (j - 1) - bound;
rst = max(rst, width * h);
}
index.push_back(j);
}
index.pop_back();
}
return rst;
}
解法2:DP
height[j] 记录竖直方向最大高度,left[j]记录所属连续1序列的左边界, right[j]记录连续1序列的右边界。maxArea = max((right[j] - left[j]) * height[j])
思路和histogram有相似的地方。height[j]记录某行 j 列的最大高度。right[j]记录的是高度为height[j]的右边界,left[j]记录的是高度为height[j]的左边界。
histogram的思想就是,当找到第一个小于等于前面bar的bar时,便已知当前以bar为高度的矩形的右边界,然后向左搜索直至以bar为高度的矩形的左边界。
int maximalRectangle(vector<vector<char>>& matrix) {
if(matrix.empty()) return 0;
int maxA = 0;
int n = matrix[0].size();
int left[n], right[n]; //left and right bound of 1
int height[n];
fill_n(left,n,0); fill_n(right,n,n); fill_n(height,n,0);
for(int i = 0; i < matrix.size(); i++){
int curLeft = 0, curRight = n;
for(int j = 0; j < n; j++){
if(matrix[i][j] == '1') {
left[j] = max(left[j], curLeft); height[j]++;
}
else {
left[j] = 0; curLeft = j + 1; height[j] = 0;
}
}
for(int j = n-1; j > -1; j--){
if(matrix[i][j] == '1') right[j] = min(right[j], curRight);
else {right[j] = n; curRight = j;}
maxA = max(maxA, (right[j] - left[j]) * height[j]);
}
}
return maxA;
}
Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Note:
- You must not modify the array (assume the array is read only).
- You must use only constant, O(1) extra space.
- Your runtime complexity should be less than
O(n2)
. - There is only one duplicate number in the array, but it could be repeated more than once.
解释源自:
https://segmentfault.com/a/1190000003718848#articleHeader5
假设数组中没有重复,那我们可以做到这么一点,就是将数组的下标和1到n每一个数一对一的映射起来。比如数组是213,则映射关系为0->2, 1->1, 2->3。假设这个一对一映射关系是一个函数f(n),其中n是下标,f(n)是映射到的数。如果我们从下标为0出发,根据这个函数计算出一个值,以这个值为新的下标,再用这个函数计算,以此类推,直到下标超界。实际上可以产生一个类似链表一样的序列。比如在这个例子中有两个下标的序列,0->2->3。
但如果有重复的话,这中间就会产生多对一的映射。这样,我们推演的序列就一定会有环路了。
即如果A出现两次,则有nums[B] = nums[C] = A
此题涉及到链表中的两道题。 Linked List Cycle I 和 II.
在Linked List Cycle I 中,我们需要找出链表是否有环。采取的方法是快慢指针法,慢指针一次走一步,快指针一次走两步,如果两者可以相遇,则代表链表有环。
在Linked List Cycle II 中, 我们不仅需要判断有环,还要判断环的起始位置。如果慢指针到环起始位置时,快指针处于环的第k个节点,因为快指针比慢指针每次多走一步且快慢相差n-k步(n为环的长度),因此两者会在环的n-k处相遇。
假设环的起点位于链表的第m个节点,则慢指针在环起点快指针比慢指针多走m步,则 m = t * n + k (t = 0,1,2...)。因此,如果我们在快慢相遇时,将快指针放至起始处,每次改为走一步,则m步后,两者会在环的起点处相遇。此时,我们得到了环的起点。
详细证明及解释见:http://blog.csdn.net/sysucph/article/details/15378043
int findDuplicate(vector<int>& nums) {
int slow = nums[0], fast = nums[nums[0]];
while(slow != fast){
slow = nums[slow];
fast = nums[nums[fast]];
}
fast = 0;
while(slow != fast){
slow = nums[slow];
fast = nums[fast];
}
return slow;
}