/*496. 下一个更大元素 I
- 输入: nums1 = [4,1,2], nums2 = [1,3,4,2].
输出: [-1,3,-1]*/
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> mp;
stack<int> st;
for (int i = nums2.size()-1; i>=0; i--) {
while (!st.empty() && nums2[i] > st.top()) {
st.pop();
}
if (st.empty()) {
mp[nums2[i]] = -1;
} else {
mp[nums2[i]] = st.top();
}
st.push(nums2[i]);
}
vector<int> res;
for (int i = 0; i < nums1.size(); i++) {
res.push_back(mp[nums1[i]]);
}
return res;
}
/*503给定一个循环数组(最后一个元素nextGreaterElement的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素。
- 数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1。
示例 1:
输入: [1,2,1]
输出: [2,-1,2]
解释: 第一个 1 的下一个更大的数是 2;
数字 2 找不到下一个更大的数;
第二个 1 的下一个最大的数需要循环搜索,结果也是 2。*/
vector<int> nextGreaterElements(vector<int>& nums) {
int n = nums.size();
vector<int> res(n, -1);
stack<int> st;
for (int i = 2 * n-1; i>= 0; i--) {
while (!st.empty() && st.top() <= nums[i % n]) {
st.pop();
}
if (!st.empty()) {
res[i % n] = st.top();
}
st.push(nums[i % n]);
}
return res;
}
/739:每日温度/
vector<int> dailyTemperatures(vector<int>& T) {
int n = T.size();
stack<int> st;
vector<int> res(n, 0);
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && T[st.top()] <= T[i]) {
st.pop();
}
if (!st.empty()) {
res[i] = st.top() - i;
}
st.push(i);
}
return res;
}
/316:去除重复字母/
string removeDuplicateLetters(string s) {
string res;
bool used[26] = {false};
stack<char> st;
int lastIdx[26] = {0};
for (int i = 0; i < s.size(); i++) {
lastIdx[s[i] - 'a'] = i;
}
for (int i = 0; i < s.size(); i++) {
if (used[s[i] - 'a']) {
continue;
}
while (!st.empty() && i < lastIdx[st.top() - 'a'] && st.top() > s[i]) {
used[st.top() - 'a'] = false;
st.pop();
}
st.push(s[i]);
used[st.top() - 'a'] = true;
}
while (!st.empty()) {
res += st.top();
st.pop();
}
reverse(res.begin(), res.end());
return res;
}
/402:移掉k位数字/
string removeKdigits(string num, int k) {
stack<char> st;
for (int i = 0; i < num.size(); i++) {
while (!st.empty() && k > 0 && st.top() >= num[i]) {
k--;
st.pop();
}
st.push(num[i]);
}
while (k && !st.empty()) {
st.pop();
k--;
}
if (st.empty()) {
return "0";
}
// deal with the first zero
string res;
while (!st.empty()) {
res += st.top();
st.pop();
}
reverse(res.begin(), res.end());
int idx = 0;
while (res[idx] == '0' && idx < res.size()-1) {
idx++;
}
return res.substr(idx);
}
/42:接雨水/
class Solution {
public:
int trap(vector<int>& height) {
int sz = height.size();
if (sz <= 0) {
return 0;
}
// left和right分别存储i位置左侧和右侧最高的柱子
vector<int> left(sz);
vector<int> right(sz);
left[0] = height[0];
for (int i = 1; i < sz; i++) {
left[i] = max(left[i-1], height[i]);
}
right[sz-1] = height[sz-1];
for (int i = sz - 2; i >= 0; i--) {
right[i] = max(right[i+1], height[i]);
}
int ans = 0;
for(int i = 1; i < sz - 1; i++) {
ans += (min(left[i], right[i]) - height[i]);
}
return ans;
}
};
int trap1(vector<int>& height) {
int n = height.size();
int res = 0;
stack<int> st;
for (int i = 0; i < n; i++) {
while(!st.empty() && height[st.top()] < height[i]) {
int top = st.top();
st.pop();
if (st.empty()) {
break;
}
int h = min(height[st.top()], height[i]) - height[top];
int width = i - st.top() - 1;
res += h * width;
}
st.push(i);
}
return res;
}
/84:柱状图最大的矩形/
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
vector<int> left(n);//left[i]为第i个值左侧第一个比他小的idx,不存在为-1
vector<int> right(n);//right[i]为第i个值右侧第一个比他小的idx,不存在为n
stack<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && heights[st.top()] >= heights[i]) {
st.pop();
}
if (st.empty()) {
left[i] = -1;
} else {
left[i] = st.top();
}
st.push(i);
}
while (!st.empty()) {
st.pop();
}
for (int i = n-1; i >= 0; i--) {
while (!st.empty() && heights[st.top()] >= heights[i]) {
st.pop();
}
if (st.empty()) {
right[i] = n;
} else {
right[i] = st.top();
}
st.push(i);
}
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, (right[i] - left[i] - 1) * heights[i]);
}
return ans;
}
int largestRectangleArea(vector<int>& heights) {
heights.push_back(0);
int n = heights.size();
stack<int> st;
int ans = 0;
for (int i = 0; i < n; i++) {
while (!st.empty() && heights[st.top()] >= heights[i]) {
int h = heights[st.top()];
st.pop();
int sidx = st.empty() ? -1: st.top();
ans = max(ans, (i - sidx - 1) * h);
}
st.push(i);
}
return ans;
}
#85. 最大矩形
给定一个仅包含 0 和 1 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
class Solution {
int largestRectangleArea(vector<int>& heights) {
heights.push_back(0);
int n = heights.size();
stack<int> st;
int ans = 0;
for (int i = 0; i < n; i++) {
//当遇到右边比当前矮的柱子,需要计算答案了
while (st.size() && heights[st.top()] >= heights[i]) {
int h = heights[st.top()];
st.pop();
int leftIdx = st.empty() ? -1 : st.top();//左边比当前矮的索引
ans = max(ans, (i - leftIdx - 1) * h);
}
st.push(i);
}
return ans;
}
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if (matrix.size() == 0) {
return 0;
}
vector<int> heights(matrix[0].size(), 0);
int ans = 0;
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[0].size(); j++) {
if (matrix[i][j] == '0') {
heights[j] = 0;
} else {
heights[j]++;
}
}
ans = max(ans, largestRectangleArea(heights));
}
return ans;
}
};
单调队列:
//给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值。
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> res;
if (k == 0) {
return res;
}
deque<int> qmax;
int i = 0;
for (int i = 0; i < k; i++) {
while (!qmax.empty() && nums[i] > nums[qmax.back()]) {
qmax.pop_back();
}
qmax.push_back(i);
}
res.push_back(nums[qmax.front()]);
for (int i = k; i < nums.size(); i++) {
if (!qmax.empty() && qmax.front() <= i-k) {
qmax.pop_front();
}
while (!qmax.empty() && nums[i] > nums[qmax.back()]) {
qmax.pop_back();
}
qmax.push_back(i);
res.push_back(nums[qmax.front()]);
}
return res;
}