算法训练第五十九天

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;
    }
};
相关推荐
进击的荆棘1 小时前
优选算法——双指针
数据结构·算法
魂梦翩跹如雨1 小时前
死磕排序算法:手撕快速排序的四种姿势(Hoare、挖坑、前后指针 + 非递归)
java·数据结构·算法
努力学算法的蒟蒻8 小时前
day61(1.20)——leetcode面试经典150
面试·职场和发展
夏鹏今天学习了吗8 小时前
【LeetCode热题100(87/100)】最小路径和
算法·leetcode·职场和发展
哈哈不让取名字8 小时前
基于C++的爬虫框架
开发语言·c++·算法
Lips61110 小时前
2026.1.20力扣刷题笔记
笔记·算法·leetcode
2501_9413297211 小时前
YOLOv8-LADH马匹检测识别算法详解与实现
算法·yolo·目标跟踪
洛生&11 小时前
Planets Queries II(倍增,基环内向森林)
算法
小郭团队11 小时前
1_6_五段式SVPWM (传统算法反正切+DPWM2)算法理论与 MATLAB 实现详解
嵌入式硬件·算法·matlab·dsp开发
小郭团队11 小时前
1_7_五段式SVPWM (传统算法反正切+DPWM3)算法理论与 MATLAB 实现详解
开发语言·嵌入式硬件·算法·matlab·dsp开发