首页 > 其他分享 >【重要】LeetCode 901. 股票价格跨度

【重要】LeetCode 901. 股票价格跨度

时间:2022-10-23 09:55:14浏览次数:64  
标签:901 idx int price stk 股票价格 跨度 LeetCode

题目链接

股票价格跨度

注意事项

使用单调栈

代码

class StockSpanner {
public:
    StockSpanner() {

        this->stk.emplace(-1, INT_MAX);
        this->idx = -1;
    }

    int next(int price) {

        idx++;
        while (price >= stk.top().second) {
            stk.pop();
        }

        int ret = idx - stk.top().first;
        stk.emplace(idx, price);
        
        return ret;
    }

private:
    stack<pair<int, int>> stk;
    int idx;
};

标签:901,idx,int,price,stk,股票价格,跨度,LeetCode
From: https://www.cnblogs.com/shixuanliu/p/16817971.html

相关文章

  • leetcode-283-easy
    MoveZeroes思路一:用left指针标记求解数组最大下标+1,初始化的时候是0,随着往右遍历,left会一步步扩大。最后把left右边的数都置为0。这题的关键在于left永远......
  • leetcode-231-easy
    PowerOfTwo思路一:观察2的n次方的二进制,都只有一位1bit,遍历即可publicbooleanisPowerOfTwo(intn){if(n<=0)returnfalse;intcount=0;......
  • LeetCode 1730. Shortest Path to Get Food
    原题链接在这里:https://leetcode.com/problems/shortest-path-to-get-food/题目:Youarestarvingandyouwanttoeatfoodasquicklyaspossible.Youwanttofind......
  • [LeetCode] 1768. Merge Strings Alternately
    Youaregiventwostrings word1 and word2.Mergethestringsbyaddinglettersinalternatingorder,startingwith word1.Ifastringislongerthantheot......
  • 【leetcode_C++_哈希表_day5】242. 有效的字母异位词&&349. 两个数组的交集&&202.快乐
    C++知识补充:(不完全,仅针对本题用的知识点)1.C++类&对象关键字public确定了类成员的访问属性。在类对象作用域内,公共成员在类的外部是可访问的。您也可以指定类的成......
  • leetcode-169-easy
    MajorityElement思路一:mappublicintmajorityElement(int[]nums){if(nums.length==1)returnnums[0];Map<Integer,Integer>map=newHashMap<>(......
  • leetcode-347. 前 K 个高频元素
    347.前K个高频元素建立一个map集合第一个元素代表当前的数字,第二个元素代表出现的次数以元素出现次数作为排序标准建立小根堆遍历map加入到堆中,当堆的长度为k的时候......
  • leetcode-190-easy
    ReverseBits思路一:遍历32位bit,记录bit结果publicintreverseBits(intn){intresult=0;intx=32;while(x-->0){intbit=n......
  • 力扣 (LeetCode)算法入门——Day1
    704.二分查找题目:给定一个n个元素有序的(升序)整型数组nums和一个目标值target ,写一个函数搜索nums中的target,如果目标值存在返回下标,否则返回-1。classSolut......
  • leetcode(30)单调栈
    6077.巫师的总力量和注意:因为要求连续,所以不能用回溯的方法做496.下一个更大元素I子数组的最小值之和子数组最小乘积的最大值子数组范围和901.股票价格跨度用......