当前位置: 首页 > 编程日记 > 正文

[Swift]LeetCode901. 股票价格跨度 | Online Stock Span

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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  */ 

 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  */

 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

相关文章:

java基础===点餐系统

public class OrderMsg {public static void main(String[] args) throws Exception { /** * 订餐人姓名、选择菜品、送餐时间、送餐地址、订单状态、总金额 * 01.创建对应的数组 * 02.数组的初始化 * 03.显示菜单 * 04.根据用户的选择进去指定的模块 */ String[] names new S…

HTML页面中使两个div并排显示

在HTML中实现两个div并排显示&#xff0c;方法如下&#xff1a; 方法1&#xff1a;设置float浮动对需要并排显示的div设置样式&#xff1a;style"float:left;" <div style"float:left;">div1</div>方法2&#xff1a;设置div为行内样式对需要并…

备案网站管理系统是JSP做的

备案网站管理系统 http://www.miibeian.gov.cn/ 浪费了我一上午的时间没成功.靠!转载于:https://www.cnblogs.com/splyn/archive/2009/12/24/1631281.html

explorer.exe应用程序错误说明 0X000000该内存不能为read的解决方法

0X000000该内存不能为read的解决方法 出现这个现象有方面的&#xff0c;一是硬件&#xff0c;即内存方面有问题&#xff0c;二是软件&#xff0c;这就有多方面的问题了。 一&#xff1a;先说说硬件&#xff1a; 一般来说&#xff0c;电脑硬件是很不容易坏的。内存出现问题的可能…

CSS 选择符

选择符 selector 样式的基本规则——样式声明与关键字 声明块中有一个或多个声明。声明的格式是固定的&#xff0c;先是属性名&#xff0c;然后是冒号&#xff0c;后面再跟属性值和分号。冒号和分号后面可以有零个或多个空白。属性值几乎都是一个关键字或以空格分隔的多个关键…

CSS3快学笔记

在编写CSS3样式时&#xff0c;不同的浏览器可能需要不同的前缀。它表示该CSS属性或规则尚未成为W3C标准的一部分&#xff0c;是浏览器的私有属性&#xff0c;虽然目前较新版本的浏览器都是不需要前缀的&#xff0c;但为了更好的向前兼容前缀还是少不了的。 前缀 浏览器 -webk…

DOS批处理的字符串功能

DOS批处理的字符串功能 批处理有着具有非常强大的字符串处理能力&#xff0c;其功能绝不低于C语言里面的字符串函数集。批处理中可实现的字符串处理功能有&#xff1a;截取字符串内容、替换字符串特定字段、合并字符串、扩充字符串等功能。下面对这些功能一一进行讲解。  【 …

走进Java 7模块系统

笔者在观看过Devoxx关于Jigsaw的一段演示后&#xff0c;我很兴奋&#xff0c;觉得它应该会是针对复杂类路径版本问题和JAR陷阱等问题的解决方案。开发者最终能够使用他们所期望的任何Xalan版本&#xff0c;而无需被迫使用授权机制。不幸的是&#xff0c;通往更加有效的模块系统…

Chrome浏览器控制台报错NET::ERR_SSL_OBSOLETE_VERSION

问题描述&#xff1a;Chrome浏览器控制台报错NET::ERR_SSL_OBSOLETE_VERSION 原因&#xff1a; 服务器使用了TLS1.0 或 TLS1.1 版本&#xff0c;没有使用 TLS1.2 解决方法&#xff1a; 地址栏访问&#xff1a;chrome://flags/#legacy-tls-enforced&#xff1b;将Enforce depr…

关于矩形连线 (rectangle connect)

矩形连线问题&#xff0c;就是在两个矩形之间建立带可曲折的无覆盖的连线&#xff08;连线不覆盖图形&#xff09;&#xff0c;我的方法是这样的&#xff1a;CPoint pts[5];//输出连线的点列表int nPts;//输出点列表中点的数量void GetRectConnectLines&#xff08;CPoint * pt…

前端去掉空格的方法

/*** 去掉前端左右两边的字符空格* param str* 字符串* */function trim(str){//删除左右两端的空格return str.replace(/(^\s*)|(\s*$)/g, "");} /*** 去掉左边的空格* param str* returns*/function ltrim(str){ //删除左边的空格return str.replace(/(^\s*)/g,&q…

ARM 环境下使用azure powershell 从远程blob中拉去vhd 并创建虚拟机

最近需要从指定公共访问的blob中复制vhd到自己的订阅存储账户&#xff0c;并使用vhd创建AZURE ARM虚拟机(非经典版)&#xff0c;而且在portal.azure.cn中无法实现虚拟机映像创建等功能&#xff0c;于是自己使用azure powershell写了一个简单的脚本&#xff0c; 前期准备&#x…

读懂电脑系统(一)

addins文件夹这是系统附加文件夹&#xff0c;用来存放系统附加功能的文件。AppPatch文件夹这是应用程序修补备份文件夹&#xff0c;用来存放应用程序的修补文件。Config文件夹这是系统配置文件夹&#xff0c;用来存放系统的一些临时配置的文件。Connection Wizard文件夹看名字就…

java压缩解压缩类实例[转]

package com.yangxiaozuo.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.Deflater; import java.util.zip.Inflater; /** * ZLib压缩工具 * * author 梁栋 * version 1.0 * since 1.0 */ public abstract class ZL…

前端应用打印控件

前端应用打印控件1. Lodop打印控件1.1 官网地址1.2 控件介绍1.3 控件安装程序下载1.4 控件使用1.4.1 使用示例1.4.1.1 官网提供的使用示例1.4.1.2 ng-alain提供的Lodop打印示例1.4.2 打印说明2. Hiprint打印插件2.1 官网地址2.2 插件介绍2.3 插件下载2.4 插件使用相关网址1. Lo…

剑指Offer——平衡二叉树

题目描述&#xff1a; 输入一棵二叉树&#xff0c;判断该二叉树是否是平衡二叉树。 分析&#xff1a; 平衡二叉树&#xff08;Self-balancing binary search tree&#xff09;又被称为AVL树&#xff08;有别于AVL算法&#xff09;&#xff0c;且具有以下性质&#xff1a;它是一…

yii2框架原生的结合框架使用的图片上传

首先我们要从model层开始写起&#xff0c;主要是为了创建验证规则&#xff0c;还有图片上传的路径以及图片的命名规则&#xff08;UploadForm.php&#xff09; 接下来我们要在控制器层写好业务逻辑&#xff0c;就是什么情况下直接在调用model层进行上传&#xff0c;一般失败的时…

Windows Server 2003 : 服务器群集

服务器群集 是一组运行 Microsoft Windows Server 2003 Enterprise Edition 或 Microsoft Windows Server 2003 Enterprise Edition 的独立的计算机系统&#xff08;称为节点&#xff09;&#xff0c;不同节点像单个系统一样协同工作&#xff0c;从而确保执行关键任务的应用程序…

初学者易上手的SSH-hibernate04 一对一 一对多 多对多

这章我们就来学习下hibernate的关系关联&#xff0c;即一对一(one-to-one)&#xff0c;一对多(one-to-many)&#xff0c;多对多(many-to-many)。这章也将是hibernate的最后一章了&#xff0c;用于初学者可以了。 首先讲述一对一:就以一个人对应一张身份证为列子。 第一步:新建表…

Python爬虫入门教程 54-100 博客园等博客网站自动评论器

爬虫背景 爬虫最核心的问题就是解决重复操作&#xff0c;当一件事情可以重复的进行的时候&#xff0c;就可以用爬虫来解决这个问题&#xff0c;今天要实现的一个基本需求是完成“博客园“ 博客的自动评论&#xff0c;其实原理是非常简单的&#xff0c;提炼一下需求 基本需求 登…

T-SQL Convert转换时间类型

关键字: sql 时间 转化 SQL中CONVERT转化函数的用法 格式: CONVERT(data_type,expression[,style]) 说明: 此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar) 相互转换的时候才用到. 例子: SELECT CONVERT(varchar(30),getdate(),101) n…

解决Lodop 8443端口找不到CLodopfuncs.js文件问题

问题描述&#xff1a; GET https://localhost:8443/CLodopfuncs.js?nameCLODOP net::ERR_CERT_COMMON_NAME_INVALID 可能原因&#xff1a; https证书问题&#xff0c;通用名称不合法&#xff0c;地址栏访问https://localhost:8443&#xff0c;如下图所示 解决方法&#…

CString工作原理和常见问题分析

关于Cstring 类 版权所有©Stevencaobenq.com2003-11-6转自&#xff1a;http://blog.csdn.net/laiyiling/archive/2004/10/05/125216.aspx 看了很多人写的程序,包括我自己写的一些代码&#xff0c;发现很大的一部分bug是关于MFC类中的CString的错误用法的.出现这种错误的原…

javascript 学习三 语句

1、if 语句 if (condition){ do something else } condition 是条件语句&#xff0c;在这里&#xff0c;condition 可以是任意表达式&#xff0c;但结果不一定就是布尔值&#xff0c;但javascript 会调用 boolean&#xff08;&#xff09; 来把结果转换成布尔值。 2、do-while …

新建本地仓库,同步远程仓场景,出现git branch --set-upstream-to=origin/master master 解决方法...

1.本地创建一个本地仓库 2.关联远程端:git remote add origin gitgithub.com:用户名/远程库名.git3.同步远程仓库到本地git pull这个时候会报错If you wish to set tracking information for this branch you can do so with:git branch --set-upstream-toorigin/<branch>…

Git npm相关命令

Git 相关命令查看用户名和密码配置用户名和密码查看git项目远程地址添加git远程仓库查看提交记录查看已有tag打标签在某次提交记录上打标签推送标签到远程推送单个指定tag到远程推送多个tag到远程2. npm相关命令2.1 设置npm源2.2 查看npm源2.3 npm清缓存查看用户名和密码 $ gi…

2009年上半年网络工程师考试下午试卷参考答案(一)

试题一&#xff08;15分&#xff09;  阅读以下说明&#xff0c;回答问题1至问题4&#xff0c;将解答填入答题纸对应的解答栏内。【说明】某公司有1个总部和2个分部&#xff0c;各个部门都有自己的局域网。该公司申请了4个C类IP地址块202.114.10.0/24~202.114.13.0/24。公司各…

创建Silverlight自定义启动画面

每一款商业的Silverlight项目&#xff0c;为了体现项目个性化&#xff0c;都会有不同的界面设计&#xff0c;项目UI设计的第一步就是创建个性的自定义启动画面&#xff0c;本文将介绍如何创建Silverlight自定义启动画面&#xff0c;也就是经常说的Splash Screen. Silverlight初…

params.success params.success(res.data)

params.success && params.success(res.data)只有success 为真&#xff0c;才执行后边的代码转载于:https://www.cnblogs.com/qq254980080/p/10619413.html

有关 drop delete truncate 问题

drop 可以删除数据库 数据表 数据表中字段 delete 删除数据表中的行 而不删除数据表 可以删除一行&#xff1a; Delete from 表 where 列名称值 或是多行&#xff1a; Delete from 表 truncate 删除数据表中数据 而不删除数据表&#xff1a; truncate table 表 三者的删除速度 …