代码随想录day58 59 60单调栈集合 随便及一个单调队列

739每日温度

class Solution {
public:
    vector dailyTemperatures(vector& nums) {
        int n = nums.size();
        vector res(n);
        stacks;
        for (int i = n - 1; i >= 0; i--) 
        {
            while (!s.empty() && nums[s.top()] <= nums[i]) 
            {
                s.pop();
            }
            res[i] = s.empty() ? 0 : (s.top()-i);
            s.push(i);
        }
    return res;
    }
};

496下一个更大元素

class Solution {
public:
    vector nextGreaterElement(vector& nums1, vector& nums2) {
        int n = nums2.size();
        int res=0;
        stacks;
        unordered_mapump;
        for (int i = n - 1; i >= 0; i--) 
        {
            while (!s.empty() && s.top() <= nums2[i]) 
            {
                s.pop();
            }
            res = s.empty() ? -1 : s.top();
            s.push(nums2[i]);
            ump[nums2[i]]=res;
        }
        int m=nums1.size();
        vectorans(m,0);
        for(int i=0;i

503下一个更大元素2

class Solution {
public:
    vector nextGreaterElements(vector& nums) 
    {
        int n=nums.size();
        vectorres(n);
        stacks;
        for(int i=2*n-1;i>=0;i--)
        {
            while(!s.empty()&&s.top()<=nums[i%n])
            {
                s.pop();
            }
            res[i%n]=s.empty()?-1:s.top();
            s.push(nums[i%n]);
        }
        return res;
    }
};

42接雨水

class Solution {
public:
    stackst;
    int res=0;
    int trap(vector& h) 
    {
        for(int i=0;i& height) 
    {
        int left = 0, right = height.size() - 1;
        int l_max = 0, r_max = 0;

        int res = 0;
        while (left < right) 
        {
            l_max = max(l_max, height[left]);
            r_max = max(r_max, height[right]);

            if (l_max < r_max) 
            {
                res += l_max - height[left];
                left++;
            } 
            else {
                res += r_max - height[right];
                right--;
            }
        }
        return res;
    }
};

84柱状图中的最大矩形

class Solution {
public:
    int largestRectangleArea(vector& heights) 
    {
        stackst;
        int result = 0;
        heights.insert(heights.begin(), 0); 
        heights.push_back(0);
        st.push(0);
        for(int i=1;i

11盛水最多的容器

class Solution {
public:
    int maxArea(vector& height) 
    {
        int left = 0, right = height.size() - 1;
        int res = 0;
        while (left < right) 
        {
            int cur_area = min(height[left], height[right]) * (right - left);
            res = max(res, cur_area);

            if (height[left] < height[right]) 
            {
                left++;
            } 
            else 
            {
                right--;
            }
        }
        return res;
    }
};

239滑动窗口最大值

class Solution {
public:
    vector maxSlidingWindow(vector& nums, int k) 
    {
        int n=nums.size();
        vectorres;
        dequeq;
        for(int i=0;iq.front()) q.pop_front();
            while(!q.empty()&&nums[q.back()]= k-1) res.push_back(nums[que.front()]);
        }
        return res;
    }
};

你可能感兴趣的:(算法,leetcode,职场和发展)