算法训练第五十九天

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;
    }
};
相关推荐
leobertlan21 小时前
好玩系列:用20元实现快乐保存器
android·人工智能·算法
青梅橘子皮21 小时前
C语言---指针的应用以及一些面试题
c语言·开发语言·算法
_Evan_Yao21 小时前
技术成长周记06|面试中看清差距,新项目点燃热情
面试·职场和发展
_深海凉_1 天前
LeetCode热题100-有效的括号
linux·算法·leetcode
被开发耽误的大厨1 天前
1、==、equals、hashCode底层原理?重写场景?
算法·哈希算法
haina20191 天前
《品牌观察》专访海纳AI:引领AI面试测评新时代
人工智能·面试·职场和发展
WolfGang0073211 天前
代码随想录算法训练营 Day38 | 动态规划 part11
算法·动态规划
Raink老师1 天前
【AI面试临阵磨枪】什么是 MCP(Model Control Protocol)、A2A(Agent-to-Agent)协议?
人工智能·面试·职场和发展·ai 面试
松☆1 天前
C++ 算法竞赛题解:P13569 [CCPC 2024 重庆站] osu!mania —— 浮点数精度陷阱与 `eps` 的深度解析
开发语言·c++·算法
久菜盒子工作室1 天前
面试|产品经理|为什么从 xxx(主要是实习 、工作)离职?
面试·职场和发展·产品经理