当前位置: 首页 > news >正文

算法训练第五十九天

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

代码:

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)

代码:

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)

代码:

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;
    }
};

相关文章:

  • CSS:实现文字溢出显示省略号且悬浮显示tooltip完整信息
  • vue-elementPlus自动按需导入和主题定制
  • 【深度学习】 Python 和 NumPy 系列教程(一):Python基本数据类型:1、数字(整数、浮点数)及相关运算;2、布尔值
  • 将函数实现放到CPP报“无法解析的外部符号...”,系VS Bug
  • Flink从入门到精通之-05 DataStream API
  • 命名空间 namespace
  • 前端开发——Javascript的定义函数、事件处理、Math对象、Date对象
  • GPT-4老板:AI可能会杀死人类,已经出现我们无法解释的推理能力
  • CSS(三)
  • 华为nat配置实验:内网能够访问外网,内网服务器80端口映射出去
  • Linux使用:环境变量指南和CPU和GPU利用情况查看
  • 《C++那些事》Step By Step上手学习
  • CentOS8提高篇22:制作crontab计划任务,at一次性计划任务
  • Java云电子病历系统源码,提供电子病历在线制作、管理和使用的一体化电子病历
  • 【ansible】实施任务控制
  • 【愚人节专场】Java实现定时发送小情话
  • 基于SpringBoot实现CSGO游戏赛事管理系统演示【附项目源码】分享
  • 一个年薪40w软件测试员的职业规划,写给还在迷茫中的朋友
  • 使用Xarray解码GFS气象源文件和pip国内源
  • Beats:在 Docker 中同时部署 Metricbeat 和 Elasticsearch
  • 北京顺义:做好潮白河大桥事故善后处置,举一反三排查风险
  • 山西省朔州市政府党组成员、副市长李润军接受审查调查
  • 上海小朋友喜欢读什么书?来看这份“少年儿童阅读报告”
  • 全国人大常委会调研组在宁波调研,张庆伟带队钟山易炼红参加
  • 医学泰斗客死他乡?AI小作文批量如何炮制?对话已被抓获的网络水军成员
  • 2025年上海车展后天开幕,所有进境展品已完成通关手续