首页 > 其他分享 >利用栈求递增子字符串长度

利用栈求递增子字符串长度

时间:2022-10-26 21:38:31浏览次数:94  
标签:span called 递增 next 字符串 returns 栈求 StockSpanner price


901. Online Stock Span

Medium

24552FavoriteShare

Write a class ​​StockSpanner​​ which collects daily price quotes for some stock, and returns the span of that stock's price for the current day.

The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price.

For example, if the price of a stock over the next 7 days were ​​[100, 80, 60, 70, 60, 75, 85]​​​, then the stock spans would be ​​[1, 1, 1, 2, 1, 4, 6]​​.

 

Example 1:


Input: ["StockSpanner","next","next","next","next","next","next","next"], [[],[100],[80],[60],[70],[60],[75],[85]] Output: [null,1,1,1,2,1,4,6] Explanation: First, S = StockSpanner() is initialized. Then: S.next(100) is called and returns 1, S.next(80) is called and returns 1, S.next(60) is called and returns 1, S.next(70) is called and returns 2, S.next(60) is called and returns 1, S.next(75) is called and returns 4, S.next(85) is called and returns 6. Note that (for example) S.next(75) returned 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price.


 

class StockSpanner {
private:
stack<pair<int,int>> S;
public:
StockSpanner() {

}

int next(int price) {
int span = 1;
while(!S.empty() && price>= S.top().first){
span = span + S.top().second;
S.pop();
}
S.push(make_pair(price,span));
return span;
}
};

/**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner* obj = new StockSpanner();
* int param_1 = obj->next(price);
*/

 

标签:span,called,递增,next,字符串,returns,栈求,StockSpanner,price
From: https://blog.51cto.com/u_13121994/5798561

相关文章

  • 连续子数组的最大和+如何处理以字符为分隔符的字符串
    题目描述一个非空整数数组,选择其中的两个位置,使得两个位置之间的数和最大。如果最大的和为正数,则输出这个数;如果最大的和为负数或0,则输出0输入描述:3,-5,7,-2,8输出描述:......
  • 一个字符串用空格作为分隔符,可以用while(cin>>Input)进行输入
    题目描述给定一个句子(只包含字母和空格),将句子中的单词位置反转,单词用空格分割,单词之间只有一个空格,前后没有空格。比如:(1)“helloxiaomi”->“mixiaohello”输入描......
  • 字符串--移除k个数使得剩下的数最大
    有一十进制正整数,移除其中的K个数,使剩下的数字是所有可能中最大的。假设:字符串的长度一定大于等于K字符串不会以0开头 输入描述:一行由正整数组成的数字字符串,和......
  • DP--字符串变换
    给定两个字符串,已知可以使用三种方式进行变换1.插入一个字符2.删除一个字符3.更改一个字符请设计一个算法,找到两个字符串之间的经历几次最小变换,可以字符串1转换成字......
  • DFS--同一个方向找出所有子字符串的个数
     字符迷阵是一种经典的智力游戏。玩家需要在给定的矩形的字符迷阵中寻找特定的单词。在这题的规则中,单词是如下规定的:1.在字符迷阵中选取一个字符作为单词的开头;2.选取......
  • 字符串--字符串替换模板
    请你实现一个简单的字符串替换函数。原串中需要替换的占位符为"%s",请按照参数列表的顺序一一替换占位符。若参数列表的字符数大于占位符个数。则将剩下的参数字符添加到字......
  • 手撕代码——最长不包含重复字符的字符串长度
    3. LongestSubstringWithoutRepeatingCharactersMedium6059348FavoriteShareGivenastring,findthelengthofthe longestsubstring withoutrepeatingcharact......
  • 最长递增子序列
    题目描述给定一个序列An=a1,a2, ...,an,找出最长的子序列使得对所有i<j,ai<aj。求出这个子序列的长度输入描述:输入的序列输出描述:最长递增子序列的长度示......
  • 字符串“同素异形体”可以用key-value的unordered_map存储
    49. GroupAnagramsMedium1896123FavoriteShareGivenanarrayofstrings,groupanagramstogether.Example:Input:​​["eat","tea","tan","ate","nat","bat"]​......
  • 字符串--通配符*匹配
    44. WildcardMatchingHard120177FavoriteShareGivenaninputstring(​​s​​​)andapattern(​​p​​​),implementwildcardpatternmatchingwithsupportf......