Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9a12b1c

Browse files
authored
Merge pull requestneetcode-gh#2613 from aadil42/patch-59
Create 0901-online-stock-span.js
2 parents1f8cedc +c6c00e5 commit9a12b1c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎javascript/0901-online-stock-span.js

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp