We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5f4f024 commit 5c95b83Copy full SHA for 5c95b83
javascript/0901-online-stock-span.js
@@ -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
13
+ peek() {
14
+ return this.stack[this.stack.length - 1];
15
16
17
18
+ push(val) {
19
+ return this.stack.push(val);
20
21
22
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