Skip to content

Commit 5c95b83

Browse files
authored
Create 0901-online-stock-span.js
Added solution for online-stock-span in JS.
1 parent 5f4f024 commit 5c95b83

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

javascript/0901-online-stock-span.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//https://leetcode.com/problems/online-stock-span/
2+
class StockSpanner {
3+
constructor() {
4+
this.stack = [];
5+
}
6+
7+
// Time O(1) | Space O(1)
8+
isEmpty() {
9+
return this.stack.length === 0;
10+
}
11+
12+
// Time O(1) | Space O(1)
13+
peek() {
14+
return this.stack[this.stack.length - 1];
15+
}
16+
17+
// Time O(1) | Space O(1)
18+
push(val) {
19+
return this.stack.push(val);
20+
}
21+
22+
// Time O(1) | Space O(1)
23+
pop() {
24+
return this.stack.pop();
25+
}
26+
27+
// Time O(n) | Space O(1)
28+
next(price) {
29+
let currunt = 1;
30+
while (this.peek() && this.peek()[0] <= price) {
31+
currunt += this.pop()[1];
32+
}
33+
this.push([price, currunt]);
34+
return this.peek()[1];
35+
}
36+
}

0 commit comments

Comments
 (0)