Fancy Sequence LeetCode solution in JavaScript

1
var Fancy = function () {
2
this.sequence = [];
3
this.appliedOps = [];
4
this.ops = [];
5
this.modulo = Math.pow(10, 9) + 7;
6
};
7

8
/**
9
* @param {number} val
10
* @return {void}
11
*/
12
Fancy.prototype.append = function (val) {
13
this.sequence.push(val);
14
this.appliedOps.push(this.ops.length);
15
};
16

17
/**
18
* @param {number} inc
19
* @return {void}
20
*/
21
Fancy.prototype.addAll = function (inc) {
22
this.ops.push(["add", inc]);
23
};
24

25
/**
26
* @param {number} m
27
* @return {void}
28
*/
29
Fancy.prototype.multAll = function (m) {
30
this.ops.push(["mult", m]);
31
};
32

33
/**
34
* @param {number} idx
35
* @return {number}
36
*/
37
Fancy.prototype.getIndex = function (idx) {
38
if (idx >= this.sequence.length) {
39
return -1;
40
}
41

42
while (this.appliedOps[idx] < this.ops.length) {
43
const [operation, value] = this.ops[this.appliedOps[idx]];
44
this.appliedOps[idx]++;
45

46
if (operation === "mult") {
47
this.sequence[idx] = (this.sequence[idx] * value) % this.modulo;
48
}
49

50
if (operation === "add") {
51
this.sequence[idx] = (this.sequence[idx] + value) % this.modulo;
52
}
53
}
54

55
return this.sequence[idx];
56
};
57

58
/**
59
* Your Fancy object will be instantiated and called as such:
60
* var obj = new Fancy()
61
* obj.append(val)
62
* obj.addAll(inc)
63
* obj.multAll(m)
64
* var param_4 = obj.getIndex(idx)
65
*/

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0