-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path901-online-stock-span.js
More file actions
46 lines (35 loc) · 929 Bytes
/
Copy path901-online-stock-span.js
File metadata and controls
46 lines (35 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// https://leetcode.com/problems/online-stock-span/
var StockSpanner = function () {
this.prices = []
this.differences = []
this.span = []
};
/**
* @param {number} price
* @return {number}
*/
StockSpanner.prototype.next = function (price) {
let pointer = 0
while (pointer < this.prices.length) {
const index = this.prices.length - (pointer + 1)
const _price = this.prices[index]
const difference = price - _price
if (difference < 0) {
const span = pointer + 1
this.prices.push(price)
this.span.push(span)
this.differences.push(difference)
return span
}
pointer += this.span[index]
}
this.prices.push(price)
this.differences.push(price)
this.span.push(this.prices.length)
return this.prices.length
};
/**
* Your StockSpanner object will be instantiated and called as such:
* var obj = new StockSpanner()
* var param_1 = obj.next(price)
*/