单调栈
| #include <iostream> |
| #include <vector> |
| #include <map> |
| #include <stack> |
| |
| using namespace std; |
| |
| |
| |
| int main() { |
| |
| int n; |
| cin >> n; |
| vector<int> arr(n); |
| for (int i = 0; i < n; ++i) cin >> arr[i]; |
| |
| |
| vector<pair<int, int>> res(n); |
| |
| stack<int> stk; |
| |
| |
| for (int i = 0; i < n; ++i) { |
| |
| while (!stk.empty() && arr[stk.top()] >= arr[i]) { |
| int popIndex = stk.top(); |
| stk.pop(); |
| |
| res[popIndex].first = stk.empty() ? -1 : stk.top(); |
| |
| res[popIndex].second = i; |
| } |
| stk.emplace(i); |
| } |
| |
| |
| while (!stk.empty()) { |
| int popIndex = stk.top(); |
| stk.pop(); |
| |
| res[popIndex].first = stk.empty() ? -1 : stk.top(); |
| |
| res[popIndex].second = -1; |
| } |
| |
| |
| for (int i = n - 2; i >= 0; i--) |
| |
| if (res[i].second != -1 && (arr[res[i].second] == arr[i])) |
| res[i].second = res[res[i].second].second; |
| |
| |
| for (int i = 0; i < n; ++i) |
| cout << res[i].first << " " << res[i].second << endl; |
| } |
| #include <iostream> |
| #include <vector> |
| #include <map> |
| #include <stack> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| vector<int> dailyTemperatures(vector<int> &temperatures) { |
| vector<int> res(temperatures.size()); |
| |
| stack<int> stk; |
| |
| for (int i = 0; i < temperatures.size(); ++i) { |
| |
| while (!stk.empty() && temperatures[stk.top()] < temperatures[i]) { |
| int popIndex = stk.top(); |
| stk.pop(); |
| |
| res[popIndex] = i - popIndex; |
| } |
| stk.emplace(i); |
| } |
| |
| while (!stk.empty()) { |
| int popIndex = stk.top(); |
| stk.pop(); |
| |
| res[popIndex] = 0; |
| } |
| |
| return res; |
| } |
| }; |
| #include <iostream> |
| #include <vector> |
| #include <map> |
| #include <stack> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| int sumSubarrayMins(vector<int> &arr) { |
| const int MOD = 1e9 + 7; |
| const int len = arr.size(); |
| long res = 0; |
| |
| stack<int> stk; |
| |
| |
| for (int i = 0; i < len; ++i) { |
| |
| while (!stk.empty() && (arr[stk.top()] >= arr[i])) { |
| int popIndex = stk.top(); |
| stk.pop(); |
| int left = stk.empty() ? -1 : stk.top(); |
| |
| |
| long tempRes = (popIndex - left) * (i - popIndex) * (long) arr[popIndex]; |
| res = (res + tempRes) % MOD; |
| } |
| stk.emplace(i); |
| } |
| |
| while (!stk.empty()) { |
| int popIndex = stk.top(); |
| stk.pop(); |
| int left = stk.empty() ? -1 : stk.top(); |
| long tempRes = (popIndex - left) * (len - popIndex) * (long) arr[popIndex]; |
| res = (res + tempRes) % MOD; |
| } |
| |
| return (int) res; |
| } |
| }; |
| #include <iostream> |
| #include <vector> |
| #include <map> |
| #include <stack> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| int largestRectangleArea(vector<int> &heights) { |
| int len = heights.size(); |
| int res = 0; |
| |
| stack<int> stk; |
| |
| |
| for (int i = 0; i < len; ++i) { |
| while (!stk.empty() && (heights[stk.top()] >= heights[i])) { |
| int popIndex = stk.top(); |
| stk.pop(); |
| int left = stk.empty() ? -1 : stk.top(); |
| int width = i - left - 1; |
| res = max(res, heights[popIndex] * width); |
| } |
| stk.emplace(i); |
| } |
| |
| while (!stk.empty()) { |
| int popIndex = stk.top(); |
| stk.pop(); |
| int left = stk.empty() ? -1 : stk.top(); |
| int width = len - left - 1; |
| res = max(res, heights[popIndex] * width); |
| } |
| |
| return res; |
| } |
| }; |
| #include <iostream> |
| #include <vector> |
| #include <map> |
| #include <stack> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| int largestRectangleArea(vector<int> &heights) { |
| int len = heights.size(); |
| int res = 0; |
| |
| stack<int> stk; |
| |
| |
| for (int i = 0; i < len; ++i) { |
| while (!stk.empty() && (heights[stk.top()] >= heights[i])) { |
| int popIndex = stk.top(); |
| stk.pop(); |
| int left = stk.empty() ? -1 : stk.top(); |
| int width = i - left - 1; |
| res = max(res, heights[popIndex] * width); |
| } |
| stk.emplace(i); |
| } |
| |
| while (!stk.empty()) { |
| int popIndex = stk.top(); |
| stk.pop(); |
| int left = stk.empty() ? -1 : stk.top(); |
| int width = len - left - 1; |
| res = max(res, heights[popIndex] * width); |
| } |
| |
| return res; |
| } |
| |
| int maximalRectangle(vector<vector<char>> &matrix) { |
| int res = 0; |
| int row = matrix.size(); |
| int column = matrix.at(0).size(); |
| |
| vector<int> heights(column, 0); |
| |
| for (int i = 0; i < row; ++i) { |
| for (int j = 0; j < column; ++j) |
| heights[j] = matrix[i][j] == '1' ? (heights[j] + 1) : 0; |
| |
| res = max(res, largestRectangleArea(heights)); |
| } |
| |
| return res; |
| } |
| }; |
| #include <iostream> |
| #include <vector> |
| #include <map> |
| #include <stack> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| int maxWidthRamp(vector<int> &nums) { |
| int res = 0; |
| |
| stack<int> stk; |
| |
| for (int i = 0; i < nums.size(); ++i) { |
| |
| if (stk.empty() || nums[stk.top()] > nums[i]) |
| stk.emplace(i); |
| } |
| |
| |
| for (int i = nums.size() - 1; i >= 0; i--) { |
| |
| while (!stk.empty() && nums[stk.top()] <= nums[i]) { |
| |
| res = max(res, i - stk.top()); |
| stk.pop(); |
| } |
| } |
| return res; |
| } |
| }; |
| #include <iostream> |
| #include <vector> |
| #include <unordered_map> |
| #include <unordered_set> |
| #include <stack> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| string removeDuplicateLetters(string s) { |
| |
| unordered_map<char, int> freq; |
| |
| unordered_set<char> enter; |
| |
| stack<char> stk; |
| |
| for (auto& ch : s) freq[ch]++; |
| |
| for (auto& ch : s) { |
| |
| if (enter.find(ch) == end(enter)) { |
| while (!stk.empty() && stk.top() > ch && freq[stk.top()] > 0) { |
| |
| enter.erase(stk.top()); |
| stk.pop(); |
| } |
| |
| stk.emplace(ch); |
| enter.insert(ch); |
| } |
| |
| freq[ch]--; |
| } |
| |
| string res; |
| while (!stk.empty()) { |
| res.insert(0, 1, stk.top()); |
| stk.pop(); |
| } |
| return res; |
| } |
| }; |
| #include <iostream> |
| #include <vector> |
| #include <stack> |
| |
| using namespace std; |
| |
| int main() { |
| int len; |
| cin >> len; |
| vector<int> arr(len); |
| for (int i = 0; i < len; ++i) cin >> arr[i]; |
| |
| |
| stack<pair<int, int>> stk; |
| int res = 0; |
| |
| for (int i = len - 1; i >= 0; i--) { |
| int curTurns = 0; |
| while (!stk.empty() && stk.top().first < arr[i]) { |
| curTurns = max(curTurns + 1, stk.top().second); |
| stk.pop(); |
| } |
| stk.emplace(make_pair(arr[i], curTurns)); |
| res = max(res, curTurns); |
| } |
| |
| cout << res; |
| } |
| #include <iostream> |
| #include <vector> |
| #include <stack> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| int countFromBottom(stack<int> &stk, vector<int> &heights, int columns) { |
| int res = 0; |
| for (int i = 0; i < columns; ++i) { |
| while (!stk.empty() && heights[stk.top()] >= heights[i]) { |
| int cur = stk.top(); |
| stk.pop(); |
| if (heights[cur] > heights[i]) { |
| |
| |
| |
| int left = stk.empty() ? -1 : stk.top(); |
| int len = i - left - 1; |
| int bottom = max(left == -1 ? 0 : heights[left], heights[i]); |
| res += (heights[cur] - bottom) * len * (len + 1) / 2; |
| } |
| } |
| stk.emplace(i); |
| } |
| |
| while (!stk.empty()) { |
| int cur = stk.top(); |
| stk.pop(); |
| int left = stk.empty() ? -1 : stk.top(); |
| int len = columns - left - 1; |
| int down = left == -1 ? 0 : heights[left]; |
| res += (heights[cur] - down) * len * (len + 1) / 2; |
| } |
| return res; |
| } |
| |
| int numSubmat(vector<vector<int>> &mat) { |
| int res = 0; |
| int rows = mat.size(); |
| int columns = mat.at(0).size(); |
| stack<int> stk; |
| vector<int> heights(columns, 0); |
| for (int i = 0; i < rows; ++i) { |
| |
| for (int j = 0; j < columns; ++j) |
| heights[j] = mat[i][j] == 1 ? heights[j] + 1 : 0; |
| res += countFromBottom(stk, heights, columns); |
| } |
| |
| return res; |
| } |
| }; |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/18390871
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步