Skip to content

Commit 9f93b33

Browse files
authored
Merge pull request #1846 from AkifhanIlgaz/0901
Create: 0901-online-stock-span.rs
2 parents b38702a + 7685e73 commit 9f93b33

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

rust/0901-online-stock-span.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct StockSpanner {
2+
stack: Vec<(i32, i32)>, // (price, span)
3+
}
4+
5+
impl StockSpanner {
6+
fn new() -> Self {
7+
Self { stack: vec![] }
8+
}
9+
10+
fn next(&mut self, price: i32) -> i32 {
11+
let mut span = 1;
12+
13+
while !self.stack.is_empty() && self.stack.last().unwrap().0 <= price {
14+
span += self.stack.pop().unwrap().1;
15+
}
16+
self.stack.push((price, span));
17+
span
18+
}
19+
}

0 commit comments

Comments
 (0)