Skip to content

Commit f616dc1

Browse files
committed
901
1 parent d57b9c3 commit f616dc1

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

leetcode/901. Online Stock Span/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ Note that (for example) S.next(75) returned 4, because the last 4 prices
4545

4646
## Solution 1.
4747

48+
Consider input `[5,3,1,2,4]`. When we scan `4`, we take a look at the previous value `2` which is smaller than `4` so we should continue scanning. We don't need to scan leftwards one by one if we store the index of the previous larger number.
49+
50+
```
51+
index: 0 1 2 3 4
52+
val: 5 3 1 2 4
53+
prev: -1 0 1 1 0
54+
```
55+
56+
So when scanning `4`, we take a look at `2` first, and since it's smaller than `4`, we can jump to `prev[3] = 1`. The value at `1` is `3` which is again smaller so we continue to jump to `prev[1] = 0` and we see `5` which is greater, so we can know that the span is `4 - 0 = 4`.
57+
58+
```cpp
59+
// OJ: https://leetcode.com/problems/online-stock-span/
60+
// Author: github.com/lzl124631x
61+
// Time: O(1) amortized
62+
// Space: O(N)
63+
class StockSpanner {
64+
vector<int> val = {INT_MAX}, prev = {-1};
65+
public:
66+
StockSpanner() {}
67+
68+
int next(int price) {
69+
int i = val.size() - 1;
70+
while (val[i] <= price) i = prev[i];
71+
val.push_back(price);
72+
prev.push_back(i);
73+
return val.size() - i - 1;
74+
}
75+
};
76+
```
77+
78+
## Solution 1. Monotonic Stack
79+
80+
We don't need to store those intermediate smaller values since they will be skipped by jumping with the `prev` values.
81+
4882
```cpp
4983
// OJ: https://leetcode.com/problems/online-stock-span/
5084
// Author: github.com/lzl124631x

leetcode/901. Online Stock Span/s1.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
// Time: O(1) amortized
44
// Space: O(N)
55
class StockSpanner {
6-
stack<pair<int, int>> s; // val, index
7-
int i = 0;
6+
vector<int> val = {INT_MAX}, prev = {-1};
87
public:
9-
StockSpanner() {
10-
s.emplace(INT_MAX, -1);
11-
}
8+
StockSpanner() {}
9+
1210
int next(int price) {
13-
while (s.top().first <= price) s.pop();
14-
int span = i - s.top().second;
15-
s.emplace(price, i++);
16-
return span;
11+
int i = val.size() - 1;
12+
while (val[i] <= price) i = prev[i];
13+
val.push_back(price);
14+
prev.push_back(i);
15+
return val.size() - i - 1;
1716
}
1817
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// OJ: https://leetcode.com/problems/online-stock-span/
2+
// Author: github.com/lzl124631x
3+
// Time: O(1) amortized
4+
// Space: O(N)
5+
class StockSpanner {
6+
stack<pair<int, int>> s; // val, index
7+
int i = 0;
8+
public:
9+
StockSpanner() {
10+
s.emplace(INT_MAX, -1);
11+
}
12+
int next(int price) {
13+
while (s.top().first <= price) s.pop();
14+
int span = i - s.top().second;
15+
s.emplace(price, i++);
16+
return span;
17+
}
18+
};

0 commit comments

Comments
 (0)