Skip to content

Commit ab3bdc4

Browse files
committed
feat: add solutions to lc problem: No.0901.Online Stock Span
1 parent cabb394 commit ab3bdc4

File tree

6 files changed

+269
-2
lines changed

6 files changed

+269
-2
lines changed

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

+80-1
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,101 @@ S.next(85) 被调用并返回 6。
4848

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

51+
单调栈。
52+
53+
单调栈常见模型:找出每个数左/右边**离它最近的****比它大/小的数**。模板:
54+
55+
```python
56+
stk = []
57+
for i in range(n):
58+
while stk and check(stk[-1], i):
59+
stk.pop()
60+
stk.append(i)
61+
```
62+
5163
<!-- tabs:start -->
5264

5365
### **Python3**
5466

5567
<!-- 这里可写当前语言的特殊实现逻辑 -->
5668

5769
```python
58-
70+
class StockSpanner:
71+
72+
def __init__(self):
73+
self.stk = []
74+
75+
def next(self, price: int) -> int:
76+
res = 1
77+
while self.stk and self.stk[-1][0] <= price:
78+
_, t = self.stk.pop()
79+
res += t
80+
self.stk.append([price, res])
81+
return res
82+
83+
# Your StockSpanner object will be instantiated and called as such:
84+
# obj = StockSpanner()
85+
# param_1 = obj.next(price)
5986
```
6087

6188
### **Java**
6289

6390
<!-- 这里可写当前语言的特殊实现逻辑 -->
6491

6592
```java
93+
class StockSpanner {
94+
private Deque<int[]> stk;
95+
96+
public StockSpanner() {
97+
stk = new ArrayDeque<>();
98+
}
99+
100+
public int next(int price) {
101+
int res = 1;
102+
while (!stk.isEmpty() && stk.peek()[0] <= price) {
103+
int[] t = stk.pop();
104+
res += t[1];
105+
}
106+
stk.push(new int[]{price, res});
107+
return res;
108+
}
109+
}
110+
111+
/**
112+
* Your StockSpanner object will be instantiated and called as such:
113+
* StockSpanner obj = new StockSpanner();
114+
* int param_1 = obj.next(price);
115+
*/
116+
```
66117

118+
### **C++**
119+
120+
```cpp
121+
class StockSpanner {
122+
public:
123+
stack<pair<int, int>> stk;
124+
125+
StockSpanner() {
126+
}
127+
128+
int next(int price) {
129+
int res = 1;
130+
while (!stk.empty() && stk.top().first <= price)
131+
{
132+
pair<int, int> t = stk.top();
133+
stk.pop();
134+
res += t.second;
135+
}
136+
stk.push({price, res});
137+
return res;
138+
}
139+
};
140+
141+
/**
142+
* Your StockSpanner object will be instantiated and called as such:
143+
* StockSpanner* obj = new StockSpanner();
144+
* int param_1 = obj->next(price);
145+
*/
67146
```
68147
69148
### **...**

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

+99-1
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,111 @@ Note that (for example) S.next(75) returned 4, because the last 4 prices
8686
### **Python3**
8787

8888
```python
89-
89+
class StockSpanner:
90+
91+
def __init__(self):
92+
self.stk = []
93+
94+
def next(self, price: int) -> int:
95+
res = 1
96+
while self.stk and self.stk[-1][0] <= price:
97+
_, t = self.stk.pop()
98+
res += t
99+
self.stk.append([price, res])
100+
return res
101+
102+
# Your StockSpanner object will be instantiated and called as such:
103+
# obj = StockSpanner()
104+
# param_1 = obj.next(price)
90105
```
91106

92107
### **Java**
93108

94109
```java
110+
class StockSpanner {
111+
private Deque<int[]> stk;
112+
113+
public StockSpanner() {
114+
stk = new ArrayDeque<>();
115+
}
116+
117+
public int next(int price) {
118+
int res = 1;
119+
while (!stk.isEmpty() && stk.peek()[0] <= price) {
120+
int[] t = stk.pop();
121+
res += t[1];
122+
}
123+
stk.push(new int[]{price, res});
124+
return res;
125+
}
126+
}
127+
128+
/**
129+
* Your StockSpanner object will be instantiated and called as such:
130+
* StockSpanner obj = new StockSpanner();
131+
* int param_1 = obj.next(price);
132+
*/
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
class StockSpanner {
139+
public:
140+
stack<pair<int, int>> stk;
141+
142+
StockSpanner() {
143+
}
144+
145+
int next(int price) {
146+
int res = 1;
147+
while (!stk.empty() && stk.top().first <= price)
148+
{
149+
pair<int, int> t = stk.top();
150+
stk.pop();
151+
res += t.second;
152+
}
153+
stk.push({price, res});
154+
return res;
155+
}
156+
};
157+
158+
/**
159+
* Your StockSpanner object will be instantiated and called as such:
160+
* StockSpanner* obj = new StockSpanner();
161+
* int param_1 = obj->next(price);
162+
*/
163+
```
95164
165+
### **Go**
166+
167+
```go
168+
type StockSpanner struct {
169+
stk [][]int
170+
}
171+
172+
func Constructor() StockSpanner {
173+
return StockSpanner{
174+
stk: make([][]int, 0),
175+
}
176+
}
177+
178+
func (this *StockSpanner) Next(price int) int {
179+
res := 1
180+
for len(this.stk) > 0 && this.stk[len(this.stk)-1][0] <= price {
181+
t := this.stk[len(this.stk)-1]
182+
res += t[1]
183+
this.stk = this.stk[:len(this.stk)-1]
184+
}
185+
this.stk = append(this.stk, []int{price, res})
186+
return res
187+
}
188+
189+
/**
190+
* Your StockSpanner object will be instantiated and called as such:
191+
* obj := Constructor();
192+
* param_1 := obj.Next(price);
193+
*/
96194
```
97195

98196
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class StockSpanner {
2+
public:
3+
stack<pair<int, int>> stk;
4+
5+
StockSpanner() {
6+
}
7+
8+
int next(int price) {
9+
int res = 1;
10+
while (!stk.empty() && stk.top().first <= price)
11+
{
12+
pair<int, int> t = stk.top();
13+
stk.pop();
14+
res += t.second;
15+
}
16+
stk.push({price, res});
17+
return res;
18+
}
19+
};
20+
21+
/**
22+
* Your StockSpanner object will be instantiated and called as such:
23+
* StockSpanner* obj = new StockSpanner();
24+
* int param_1 = obj->next(price);
25+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
type StockSpanner struct {
2+
stk [][]int
3+
}
4+
5+
func Constructor() StockSpanner {
6+
return StockSpanner{
7+
stk: make([][]int, 0),
8+
}
9+
}
10+
11+
func (this *StockSpanner) Next(price int) int {
12+
res := 1
13+
for len(this.stk) > 0 && this.stk[len(this.stk)-1][0] <= price {
14+
t := this.stk[len(this.stk)-1]
15+
res += t[1]
16+
this.stk = this.stk[:len(this.stk)-1]
17+
}
18+
this.stk = append(this.stk, []int{price, res})
19+
return res
20+
}
21+
22+
/**
23+
* Your StockSpanner object will be instantiated and called as such:
24+
* obj := Constructor();
25+
* param_1 := obj.Next(price);
26+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class StockSpanner {
2+
private Deque<int[]> stk;
3+
4+
public StockSpanner() {
5+
stk = new ArrayDeque<>();
6+
}
7+
8+
public int next(int price) {
9+
int res = 1;
10+
while (!stk.isEmpty() && stk.peek()[0] <= price) {
11+
int[] t = stk.pop();
12+
res += t[1];
13+
}
14+
stk.push(new int[]{price, res});
15+
return res;
16+
}
17+
}
18+
19+
/**
20+
* Your StockSpanner object will be instantiated and called as such:
21+
* StockSpanner obj = new StockSpanner();
22+
* int param_1 = obj.next(price);
23+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class StockSpanner:
2+
3+
def __init__(self):
4+
self.stk = []
5+
6+
def next(self, price: int) -> int:
7+
res = 1
8+
while self.stk and self.stk[-1][0] <= price:
9+
_, t = self.stk.pop()
10+
res += t
11+
self.stk.append([price, res])
12+
return res
13+
14+
# Your StockSpanner object will be instantiated and called as such:
15+
# obj = StockSpanner()
16+
# param_1 = obj.next(price)

0 commit comments

Comments
 (0)