★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10607919.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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. 

Note:

  1. Calls to StockSpanner.next(int price) will have 1 <= price <= 10^5.
  2. There will be at most 10000 calls to StockSpanner.next per test case.
  3. There will be at most 150000 calls to StockSpanner.next across all test cases.
  4. The total time limit for this problem has been reduced by 75% for C++, and 50% for all other languages.

编写一个 StockSpanner 类,它收集某些股票的每日报价,并返回该股票当日价格的跨度。

今天股票价格的跨度被定义为股票价格小于或等于今天价格的最大连续日数(从今天开始往回数,包括今天)。

例如,如果未来7天股票的价格是 [100, 80, 60, 70, 60, 75, 85],那么股票跨度将是 [1, 1, 1, 2, 1, 4, 6]

示例:

输入:["StockSpanner","next","next","next","next","next","next","next"], [[],[100],[80],[60],[70],[60],[75],[85]]
输出:[null,1,1,1,2,1,4,6]
解释:
首先,初始化 S = StockSpanner(),然后:
S.next(100) 被调用并返回 1,
S.next(80) 被调用并返回 1,
S.next(60) 被调用并返回 1,
S.next(70) 被调用并返回 2,
S.next(60) 被调用并返回 1,
S.next(75) 被调用并返回 4,
S.next(85) 被调用并返回 6。注意 (例如) S.next(75) 返回 4,因为截至今天的最后 4 个价格
(包括今天的价格 75) 小于或等于今天的价格。 

提示:

  1. 调用 StockSpanner.next(int price) 时,将有 1 <= price <= 10^5
  2. 每个测试用例最多可以调用  10000 次 StockSpanner.next
  3. 在所有测试用例中,最多调用 150000 次 StockSpanner.next
  4. 此问题的总时间限制减少了 50%。

Runtime: 840 ms
Memory Usage: 23 MB
 1 class StockSpanner {
 2     var spans:[Int]
 3     var prices:[Int]
 4     var index:Int
 5
 6     init() {
 7         spans = [Int](repeating:0,count:10_000)
 8         prices = [Int](repeating:0,count:10000)
 9         index = -1
10     }
11
12     func next(_ price: Int) -> Int {
13         index += 1
14         prices[index] = price
15         if index == 0 || price < prices[index - 1]
16         {
17             spans[index] = 1
18             return 1
19         }
20         var previousIndex:Int = index - 1
21         var span:Int = 1
22         while (previousIndex >= 0 && price >= prices[previousIndex])
23         {
24             span += spans[previousIndex]
25             previousIndex -= spans[previousIndex]
26         }
27         spans[index] = span
28         return span
29     }
30 }
31
32 /**
33  * Your StockSpanner object will be instantiated and called as such:
34  * let obj = StockSpanner()
35  * let ret_1: Int = obj.next(price)
36  */


892ms

 1 class StockSpanner {
 2
 3     private var s = [(Int, Int)]()
 4     init() {
 5
 6     }
 7
 8     func next(_ price: Int) -> Int {
 9         var sum = 1
10         while !s.isEmpty, s.last!.0 <= price {
11             sum += s.removeLast().1
12         }
13         s.append((price, sum))
14         return sum
15     }
16 }


928ms

 1 class StockSpanner {
 2
 3     init() {
 4
 5     }
 6
 7     struct PriceSpan {
 8         let price: Int
 9         let span: Int
10     }
11
12     var stack = [PriceSpan]()
13
14     func next(_ price: Int) -> Int {
15         guard stack.count > 0 else {
16             stack.append(PriceSpan(price: price, span: 1))
17             return 1
18         }
19
20         var span = 1
21         while stack.last != nil && stack.last!.price <= price {
22             span += stack.last!.span
23             stack.removeLast()
24         }
25
26         stack.append(PriceSpan(price: price, span: span))
27         return span
28     }
29 }
30
31 /**
32  * Your StockSpanner object will be instantiated and called as such:
33  * let obj = StockSpanner()
34  * let ret_1: Int = obj.next(price)
35  */


1036ms

 1 class StockSpanner {
 2     private var span: [Int] = []
 3     private var stack: [StockSpan] = []
 4     init() {
 5
 6     }
 7
 8     func next(_ price: Int) -> Int {
 9         var stockSpan = StockSpan(price:price, span: 1)
10         while !stack.isEmpty && stack.last!.price <= stockSpan.price {
11             let removed = stack.removeLast()
12             stockSpan.span += removed.span
13         }
14         stack.append(stockSpan)
15         return stockSpan.span
16     }
17
18     struct StockSpan {
19         let price: Int
20         var span: Int
21     }
22 }
23
24 /**
25  * Your StockSpanner object will be instantiated and called as such:
26  * let obj = StockSpanner()
27  * let ret_1: Int = obj.next(price)
28  */ 


20764 kb

 1 class StockSpanner {
 2     var prices: [Int] = []
 3     var days: [Int] = []
 4     init() {
 5
 6     }
 7
 8     func next(_ price: Int) -> Int {
 9         if prices.isEmpty || prices.last! > price {
10             prices.append(price)
11             days.append(1)
12             return 1
13         }
14         var index = prices.count - 1
15         var res = 1
16         while index >= 0 && prices[index] <= price {
17             res += days[index]
18             index -= days[index]
19         }
20         prices.append(price)
21         days.append(res)
22         return res
23     }
24 }
25
26 /**
27  * Your StockSpanner object will be instantiated and called as such:
28  * let obj = StockSpanner()
29  * let ret_1: Int = obj.next(price)
30  */


20820 kb

 1 class StockSpanner {
 2
 3     private var elements : [(price : Int, conquered : Int)] = []
 4     init() {
 5
 6     }
 7
 8     func next(_ price: Int) -> Int {
 9         var conquered : Int = 1
10         while !elements.isEmpty && elements.last!.price <= price {
11             let removed = elements.removeLast()
12             conquered += removed.conquered
13         }
14
15         elements.append((price: price, conquered: conquered))
16         return elements.last!.conquered
17     }
18 }
19
20 /**
21  * Your StockSpanner object will be instantiated and called as such:
22  * let obj = StockSpanner()
23  * let ret_1: Int = obj.next(price)
24  */

转载于:https://www.cnblogs.com/strengthen/p/10607919.html

[Swift]LeetCode901. 股票价格跨度 | Online Stock Span相关推荐

  1. C语言Stock span 库存跨度问题(附完整源码)

    C语言Stock span 库存跨度问题 C语言Stock span 库存跨度问题完整源码(定义,实现,main函数测试) Stock span 库存跨度 C语言Stock span 库存跨度问题完整 ...

  2. 5月挑战Day19-Online Stock Span(Medium)

    Day19-Online Stock Span(Medium) 问题描述: Write a class StockSpanner which collects daily price quotes f ...

  3. 股票价格跨度--单调栈

    leetcode 901:力扣 今天做这道题时,第一次了解到单调栈这种方法,就是用栈维护一个单调递增或单调递减的序列 本题,需要找到的是一个股票价格的跨度,常规想法就是从后往前查找,对小于等于当前股票 ...

  4. 【数据结构与算法】之深入解析“股票价格跨度”的求解思路与算法示例

    一.题目要求 编写一个 StockSpanner 类,它收集某些股票的每日报价,并返回该股票当日价格的跨度. 今天股票价格的跨度被定义为股票价格小于或等于今天价格的最大连续日数(从今天开始往回数,包括 ...

  5. LeetCode 901. 股票价格跨度(单调栈)

    1. 题目 编写一个 StockSpanner 类,它收集某些股票的每日报价,并返回该股票当日价格的跨度. 今天股票价格的跨度被定义为股票价格小于或等于今天价格的最大连续日数(从今天开始往回数,包括今 ...

  6. ​【文末有福利】股票跨度——真实世界的算法

    设想你可以获得一只股票的每日报价.也就是说,你得到一个数值序列,每个数表示一只给定股票在某天的收盘价.这些收盘价已按时间顺序排列好.股票市场关闭的日子没有对应的报价. >>>> ...

  7. 研究股票?我们偷偷告诉你一个算法

    设想你可以获得一只股票的每日报价.也就是说,你得到一个数值序列,每个数表示一只给定股票在某天的收盘价.这些收盘价已按时间顺序排列好.股票市场关闭的日子没有对应的报价. >>>> ...

  8. 大数据Spark实战第五集 Spark股票交易实时价格分析

    统一批处理与流处理:Dataflow 在本模块前面的课时中,我们已经学习了 Spark Streaming 的架构.原理.用法以及生产环境中需要考虑的问题.对于 Spark Streaming 的学习 ...

  9. C和C++算法完整教程专栏完整目录

    C和C++算法完整教程专栏完整目录 专栏说明如下 完整专栏目录如下 专栏说明如下 内容:C和C++算法完整教程 数量:680篇博文(2023年2月15日截止) 更新时间至:2023年2月15日(后续加 ...

最新文章

  1. 转】windows下使用批处理脚本实现多个版本的JDK切换
  2. 报错解决:ERROR: While executing gem ... (Gem::CommandLineError)
  3. oracle的scn增量备份,【Oracle】基于SCN的增量备份修复DataGuard GAP
  4. Android开发之如何在debug模式下打出release正式包
  5. jvm gc停顿_在JVM中记录世界停顿
  6. Python中的全局变量与局部变量2
  7. java批处理_Java内存模型你应该知道
  8. 理解Dubbo的调用流程与Dubbo多协议解析
  9. android Camera 中的相关概念
  10. C typedef用途小结
  11. kafka-windows10中测试使用
  12. oracle怎么下载安装,Oracle数据库下载与安装的完整步骤
  13. Linux 下查看局域网内所有主机IP和MAC
  14. 2020年CFA从入门到备考攻略
  15. Java怎样实现验证码?
  16. 【模型选择】从0到1的数据价值实现需要数据分析师做些什么?
  17. github搭建与遇到的问题
  18. 浩辰CAD机械 2021,正式发布!
  19. 深度爬取网易Lofter的爬虫
  20. 城市公交站点及换乘方案设计

热门文章

  1. 【转】常见系统中文字体的英文名
  2. 《帝企鹅日记》观后感
  3. 在CISCO路由器上配置DHCP与DHCP中继
  4. 域中计算机设定重启,域中添加计算机设定的步骤
  5. 抽象工厂模式 java实例 tclhaier_Unity常用的设计模式_工厂模式系列之抽象工厂模式...
  6. 计算机图形学 区域填充,计算机图形学 区域填充算法的实现
  7. yii2 模型中set_Day184:人脸识别中open-set与close-set
  8. centos 配置bond_Linux CentOS 7 多网卡配置bond模式 bond1 bond5 bond6
  9. echarts词云第一次出现不了数据要刷新才能出现_红米K30 4G版评测:1599元的120Hz屏幕刷新率...
  10. java飞机大战爆炸效果_Java飞机大战游戏设计与实现