算法训练第五十九天

503. 下一个更大元素 II - 力扣(LeetCode)

代码:

cpp 复制代码
class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        
        vector<int> nums1(nums.begin(), nums.end());
        nums.insert(nums.end(), nums1.begin(), nums1.end());
        // 用新的nums大小来初始化result
        vector<int> result(nums.size(), -1);
        if (nums.size() == 0) return result;

        // 开始单调栈
        stack<int> st;
        st.push(0);
        for (int i = 1; i < nums.size(); i++) { 
            if (nums[i] < nums[st.top()]) st.push(i); 
            else if (nums[i] == nums[st.top()]) st.push(i);
            else { 
                while (!st.empty() && nums[i] > nums[st.top()]) {
                    result[st.top()] = nums[i];
                    st.pop();
                }
                st.push(i);
            }
        }
        // 最后再把结果集即result数组resize到原数组大小
        result.resize(nums.size() / 2);
        return result;
    }
};

42. 接雨水 - 力扣(LeetCode)

代码:

cpp 复制代码
class Solution {
public:
    int trap(vector<int>& height) {
        stack<int> st;
        st.push(0);
        int area = 0;
        for(int i = 1;i < height.size();i++)
        {
            if(height[i] <= height[st.top()])
            st.push(i);
            else 
            {
                while(!st.empty() && height[i] > height[st.top()])
                {
                    int mid = st.top();
                    st.pop();
                    if(!st.empty())
                    {
                        int h = min(height[i],height[st.top()]) - height[mid];
                        int w = i - st.top() - 1;
                        area += h * w;
                    }
                }
                st.push(i);
            }
        }
        return area;
    }
};

84. 柱状图中最大的矩形 - 力扣(LeetCode)

代码:

cpp 复制代码
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        heights.insert(heights.begin(),0);
        heights.push_back(0);
        stack<int> st;
        st.push(0);
        int area = 0;
        for(int i = 1;i < heights.size();i++)
        {
            if(heights[i] >= heights[st.top()])
            st.push(i);
            else
            {
                while(!st.empty() && heights[i] < heights[st.top()])
                {
                    int mid = st.top();
                    st.pop();
                    if(!st.empty())
                    {
                        int h = heights[mid];
                        int w = i - st.top() - 1;
                        area = max(area,h * w);
                    }
                }
                st.push(i);
            }
        }
        return area;
    }
};
相关推荐
hetao17338372 分钟前
2025-12-12~14 hetao1733837的刷题笔记
数据结构·c++·笔记·算法
鲨莎分不晴1 小时前
强化学习第五课 —— A2C & A3C:并行化是如何杀死经验回放
网络·算法·机器学习
搞科研的小刘选手2 小时前
【ISSN/ISBN双刊号】第三届电力电子与人工智能国际学术会议(PEAI 2026)
图像处理·人工智能·算法·电力电子·学术会议
拉姆哥的小屋2 小时前
从混沌到秩序:条件扩散模型在图像转换中的哲学与技术革命
人工智能·算法·机器学习
Sammyyyyy2 小时前
DeepSeek v3.2 正式发布,对标 GPT-5
开发语言·人工智能·gpt·算法·servbay
sin_hielo2 小时前
leetcode 2110
数据结构·算法·leetcode
Jay20021113 小时前
【机器学习】33 强化学习 - 连续状态空间(DQN算法)
人工智能·算法·机器学习
铭哥的编程日记3 小时前
后端面试通关笔记:从真题到思路(五)
面试·职场和发展
panzer_maus3 小时前
归并排序的简单介绍
java·数据结构·算法
cici158743 小时前
二值化断裂裂缝的智能拼接算法
人工智能·算法·计算机视觉