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.
 

Runtime: 225 ms, faster than 32.65% of Java online submissions for Online Stock Span.

class StockSpanner {
private Stack<Integer> s;
private List<Integer> alist;
private int cnt;
public StockSpanner() {
s = new Stack<>();
alist = new ArrayList<>();
cnt = ;
} public int next(int price) {
while( !s.empty() && alist.get(s.peek()) <= price ){
s.pop();
}
int tmpval;
if(!s.empty()) tmpval = s.peek();
else tmpval = -;
s.add(cnt);
alist.add(price);
cnt++;
return s.peek() - tmpval;
}
}

another solution

class StockSpanner {
List<Stock> stocks = new ArrayList<>();
public StockSpanner() {
// prices = new ArrayList<>();
// counts = new ArrayList<>();
} public int next(int price) { // Need to search backwards for the next highest price
int count = ;
// int countIndex = size-1; // int countIndex = size-1;
while(stocks.size() >= count) {
Stock s = stocks.get(stocks.size() - count);
if (s.price > price) {
break;
} else {
count += s.count;
}
}
stocks.add(new Stock(price, count));
// counts.add(count);
return count;
} class Stock {
int price;
int count; public Stock(final int price, final int count) {
this.price = price;
this.count = count;
}
} }

LC 901. Online Stock Span的更多相关文章

  1. [LeetCode] 901. Online Stock Span 股票价格跨度

    Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...

  2. leetcode 901. Online Stock Span

    Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...

  3. [LeetCode] 901. Online Stock Span 线上股票跨度

    Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...

  4. 【LeetCode】901. Online Stock Span 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leet ...

  5. 901. Online Stock Span [短于线性的时间统计单个元素的Span ]

    Span 指这个元素之前连续的小于这个元素的值有多少个 原理: 维护递减栈 这个栈内的元素是递减的序列 新到一个元素x 依次出栈比x小的(也就是这个元素的Span) 这种问题的关键在于 新来的元素如果 ...

  6. 【leetcode】901. Online Stock Span

    题目如下: 解题思路:和[leetcode]84. Largest Rectangle in Histogram的核心是一样的,都是要找出当前元素之前第一个大于自己的元素. 代码如下: class S ...

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

    Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...

  8. 单调队列 Monotonic Queue / 单调栈 Monotonic Stack

    2018-11-16 22:45:48 一.单调队列 Monotone Queue 239. Sliding Window Maximum 问题描述: 问题求解: 本题是一个经典的可以使用双端队列或者 ...

  9. 算法与数据结构基础 - 堆栈(Stack)

    堆栈基础 堆栈(stack)具有“后进先出”的特性,利用这个特性我们可以用堆栈来解决这样一类问题:后续的输入会影响到前面的阶段性结果.线性地遍历输入并用stack处理,这类问题较简单,求解时间复杂度一 ...

随机推荐

  1. 3.web开发入门知识

    /*web入门*/ /*互联网上常用的协议以及它的端口*/ http 80 http://localhost/    相当于    http://localhost:80/         http协 ...

  2. windows 8/10 彻底删除脱机文件

    先关闭脱机文件选项 控制面板 > 同步中心 > 管理脱机文件 > 查看脱机文件 > 点击 计算机 > ... > 脱机同步的文件夹右键,取消同步 可能需要重启 如果 ...

  3. npm 安装指定版本的包

    使用 包名@版本号 指定, 例如,安装 Express 3.21.2, $ npm

  4. okhttp拦截器之ConnectInterceptor解析

    主流程分析: 继续分析okhttp的拦截器,继上次分析了CacheInterceptor缓存拦截器之后,接下来到连接拦截器啦,如下: 打开看一下它的javadoc: 而整个它的实现不长,如下: 也就是 ...

  5. tomcat 配置https协议

    开发的人脸识别功能,在本地localhost是可以访问,换成IP地址不能访问,通过不了浏览器的安全协议, 要把http协议,转成https协议,才能正常访问 方案有二种 1.在项目springboot ...

  6. GET /static/plugins/bootstrap/css/bootstrap.css HTTP/1.1" 404 1718

    引用的Bootstrap一直不出来,页面中的静态资源无法加载, 报这个错的原因,是因为配置setting时候没有配置好. 后面在setting里面添加下面这段就好了 STATICFILES_DIRS ...

  7. 调用WebService接口返回字符串

    Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddr ...

  8. JS 定时器-setInterval、clearInterval、setTimeout

    在微信小程序里写的: // pages/splash/splash.js const app = getApp() Page({ data: { remainSecond: }, // 页面渲染完成后 ...

  9. Eclipse 导入项目

  10. ASP.NET大文件分片上传

    文件夹数据库处理逻辑 public class DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject() ...