Skip to content

Commit 2cbd68a

Browse files
committed
feat: update solutions to lc problem: No.0901
No.0901.Online Stock Span
1 parent 204eab0 commit 2cbd68a

File tree

7 files changed

+175
-148
lines changed

7 files changed

+175
-148
lines changed

solution/0900-0999/0901.Online Stock Span/README.md

+82-51
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,19 @@ S.next(85) 被调用并返回 6。
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50-
单调栈
50+
**方法一:单调栈**
5151

52-
单调栈常见模型:找出每个数左/右边**离它最近的****比它大/小的数**。模板:
52+
根据题目描述,我们可以知道,对于当日价格 `price`,从这个价格开始往前找,找到第一个比这个价格大的价格,这两个价格的下标差 `cnt` 就是当日价格的跨度。
5353

54-
```python
55-
stk = []
56-
for i in range(n):
57-
while stk and check(stk[-1], i):
58-
stk.pop()
59-
stk.append(i)
60-
```
54+
这实际上是经典的单调栈模型,找出左侧第一个比当前元素大的元素。
55+
56+
我们维护一个从栈底到栈顶单调递减的栈,栈中每个元素存放的是 `(price, cnt)` 数据对,其中 `price` 表示价格,`cnt` 表示当前价格的跨度。
57+
58+
出现价格 `price` 时,我们将其与栈顶元素进行比较,如果栈顶元素的价格小于等于 `price`,则将当日价格的跨度 `cnt` 加上栈顶元素的跨度,然后将栈顶元素出栈,直到栈顶元素的价格大于 `price`,或者栈为空为止。
59+
60+
最后将 `(price, cnt)` 入栈,返回 `cnt` 即可。
61+
62+
时间复杂度 $O(n)$,其中 $n$ 为 `next` 函数的调用次数。
6163

6264
<!-- tabs:start -->
6365

@@ -67,17 +69,16 @@ for i in range(n):
6769

6870
```python
6971
class StockSpanner:
70-
7172
def __init__(self):
7273
self.stk = []
7374

7475
def next(self, price: int) -> int:
75-
res = 1
76+
cnt = 1
7677
while self.stk and self.stk[-1][0] <= price:
77-
_, t = self.stk.pop()
78-
res += t
79-
self.stk.append([price, res])
80-
return res
78+
cnt += self.stk.pop()[1]
79+
self.stk.append((price, cnt))
80+
return cnt
81+
8182

8283
# Your StockSpanner object will be instantiated and called as such:
8384
# obj = StockSpanner()
@@ -90,20 +91,19 @@ class StockSpanner:
9091

9192
```java
9293
class StockSpanner {
93-
private Deque<int[]> stk;
94+
private Deque<int[]> stk = new ArrayDeque<>();
9495

9596
public StockSpanner() {
96-
stk = new ArrayDeque<>();
97+
9798
}
9899

99100
public int next(int price) {
100-
int res = 1;
101+
int cnt = 1;
101102
while (!stk.isEmpty() && stk.peek()[0] <= price) {
102-
int[] t = stk.pop();
103-
res += t[1];
103+
cnt += stk.pop()[1];
104104
}
105-
stk.push(new int[] {price, res});
106-
return res;
105+
stk.push(new int[] {price, cnt});
106+
return cnt;
107107
}
108108
}
109109

@@ -114,59 +114,90 @@ class StockSpanner {
114114
*/
115115
```
116116

117-
### **TypeScript**
117+
### **C++**
118118

119-
```ts
119+
```cpp
120120
class StockSpanner {
121-
stack: number[][];
122-
constructor() {
123-
this.stack = [];
121+
public:
122+
StockSpanner() {
123+
124124
}
125125

126-
next(price: number): number {
127-
let ans = 1;
128-
while (this.stack.length > 0 && this.stack[0][0] <= price) {
129-
let [p, c] = this.stack.shift();
130-
ans += c;
126+
int next(int price) {
127+
int ans = 1;
128+
while (!stk.empty() && stk.top().first <= price) {
129+
ans += stk.top().second;
130+
stk.pop();
131131
}
132-
this.stack.unshift([price, ans]);
132+
stk.push({price, ans});
133133
return ans;
134134
}
135+
136+
private:
137+
stack<pair<int, int>> stk;
138+
};
139+
140+
/**
141+
* Your StockSpanner object will be instantiated and called as such:
142+
* StockSpanner* obj = new StockSpanner();
143+
* int param_1 = obj->next(price);
144+
*/
145+
```
146+
147+
### **Go**
148+
149+
```go
150+
type StockSpanner struct {
151+
stk []pair
152+
}
153+
154+
func Constructor() StockSpanner {
155+
return StockSpanner{[]pair{}}
156+
}
157+
158+
func (this *StockSpanner) Next(price int) int {
159+
cnt := 1
160+
for len(this.stk) > 0 && this.stk[len(this.stk)-1].price <= price {
161+
cnt += this.stk[len(this.stk)-1].cnt
162+
this.stk = this.stk[:len(this.stk)-1]
163+
}
164+
this.stk = append(this.stk, pair{price, cnt})
165+
return cnt
135166
}
136167
168+
type pair struct{ price, cnt int }
169+
137170
/**
138171
* Your StockSpanner object will be instantiated and called as such:
139-
* var obj = new StockSpanner()
140-
* var param_1 = obj.next(price)
172+
* obj := Constructor();
173+
* param_1 := obj.Next(price);
141174
*/
142175
```
143176

144-
### **C++**
177+
### **TypeScript**
145178

146-
```cpp
179+
```ts
147180
class StockSpanner {
148-
public:
149-
stack<pair<int, int>> stk;
181+
stk: number[][];
150182

151-
StockSpanner() {
183+
constructor() {
184+
this.stk = [];
152185
}
153186

154-
int next(int price) {
155-
int res = 1;
156-
while (!stk.empty() && stk.top().first <= price) {
157-
pair<int, int> t = stk.top();
158-
stk.pop();
159-
res += t.second;
187+
next(price: number): number {
188+
let cnt = 1;
189+
while (this.stk.length && this.stk[this.stk.length - 1][0] <= price) {
190+
cnt += this.stk.pop()[1];
160191
}
161-
stk.push({price, res});
162-
return res;
192+
this.stk.push([price, cnt]);
193+
return cnt;
163194
}
164-
};
195+
}
165196

166197
/**
167198
* Your StockSpanner object will be instantiated and called as such:
168-
* StockSpanner* obj = new StockSpanner();
169-
* int param_1 = obj->next(price);
199+
* var obj = new StockSpanner()
200+
* var param_1 = obj.next(price)
170201
*/
171202
```
172203

solution/0900-0999/0901.Online Stock Span/README_EN.md

+56-58
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,11 @@ class StockSpanner:
6060
self.stk = []
6161

6262
def next(self, price: int) -> int:
63-
res = 1
63+
cnt = 1
6464
while self.stk and self.stk[-1][0] <= price:
65-
_, t = self.stk.pop()
66-
res += t
67-
self.stk.append([price, res])
68-
return res
65+
cnt += self.stk.pop()[1]
66+
self.stk.append((price, cnt))
67+
return cnt
6968

7069

7170
# Your StockSpanner object will be instantiated and called as such:
@@ -77,20 +76,19 @@ class StockSpanner:
7776

7877
```java
7978
class StockSpanner {
80-
private Deque<int[]> stk;
79+
private Deque<int[]> stk = new ArrayDeque<>();
8180

8281
public StockSpanner() {
83-
stk = new ArrayDeque<>();
84-
}
8582

83+
}
84+
8685
public int next(int price) {
87-
int res = 1;
86+
int cnt = 1;
8887
while (!stk.isEmpty() && stk.peek()[0] <= price) {
89-
int[] t = stk.pop();
90-
res += t[1];
88+
cnt += stk.pop()[1];
9189
}
92-
stk.push(new int[] {price, res});
93-
return res;
90+
stk.push(new int[] {price, cnt});
91+
return cnt;
9492
}
9593
}
9694

@@ -101,53 +99,27 @@ class StockSpanner {
10199
*/
102100
```
103101

104-
### **TypeScript**
105-
106-
```ts
107-
class StockSpanner {
108-
stack: number[][];
109-
constructor() {
110-
this.stack = [];
111-
}
112-
113-
next(price: number): number {
114-
let ans = 1;
115-
while (this.stack.length > 0 && this.stack[0][0] <= price) {
116-
let [p, c] = this.stack.shift();
117-
ans += c;
118-
}
119-
this.stack.unshift([price, ans]);
120-
return ans;
121-
}
122-
}
123-
124-
/**
125-
* Your StockSpanner object will be instantiated and called as such:
126-
* var obj = new StockSpanner()
127-
* var param_1 = obj.next(price)
128-
*/
129-
```
130-
131102
### **C++**
132103

133104
```cpp
134105
class StockSpanner {
135106
public:
136-
stack<pair<int, int>> stk;
137-
138107
StockSpanner() {
139-
}
140108

109+
}
110+
141111
int next(int price) {
142-
int res = 1;
112+
int cnt = 1;
143113
while (!stk.empty() && stk.top().first <= price) {
144-
pair<int, int> t = stk.top();
114+
cnt += stk.top().second;
145115
stk.pop();
146-
res += t.second;
147116
}
148-
stk.push({price, res});
149-
return res;
117+
stk.push({price, cnt});
118+
return cnt;
150119
}
120+
121+
private:
122+
stack<pair<int, int>> stk;
151123
};
152124

153125
/**
@@ -161,33 +133,59 @@ public:
161133
162134
```go
163135
type StockSpanner struct {
164-
stk [][]int
136+
stk []pair
165137
}
166138
167139
func Constructor() StockSpanner {
168-
return StockSpanner{
169-
stk: make([][]int, 0),
170-
}
140+
return StockSpanner{[]pair{}}
171141
}
172142
173143
func (this *StockSpanner) Next(price int) int {
174-
res := 1
175-
for len(this.stk) > 0 && this.stk[len(this.stk)-1][0] <= price {
176-
t := this.stk[len(this.stk)-1]
177-
res += t[1]
144+
cnt := 1
145+
for len(this.stk) > 0 && this.stk[len(this.stk)-1].price <= price {
146+
cnt += this.stk[len(this.stk)-1].cnt
178147
this.stk = this.stk[:len(this.stk)-1]
179148
}
180-
this.stk = append(this.stk, []int{price, res})
181-
return res
149+
this.stk = append(this.stk, pair{price, cnt})
150+
return cnt
182151
}
183152
153+
type pair struct{ price, cnt int }
154+
184155
/**
185156
* Your StockSpanner object will be instantiated and called as such:
186157
* obj := Constructor();
187158
* param_1 := obj.Next(price);
188159
*/
189160
```
190161

162+
### **TypeScript**
163+
164+
```ts
165+
class StockSpanner {
166+
stk: number[][];
167+
168+
constructor() {
169+
this.stk = [];
170+
}
171+
172+
next(price: number): number {
173+
let cnt = 1;
174+
while (this.stk.length && this.stk[this.stk.length - 1][0] <= price) {
175+
cnt += this.stk.pop()[1];
176+
}
177+
this.stk.push([price, cnt]);
178+
return cnt;
179+
}
180+
}
181+
182+
/**
183+
* Your StockSpanner object will be instantiated and called as such:
184+
* var obj = new StockSpanner()
185+
* var param_1 = obj.next(price)
186+
*/
187+
```
188+
191189
### **...**
192190

193191
```

0 commit comments

Comments
 (0)