-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy path1622-fancy-sequence.js
80 lines (72 loc) · 2.02 KB
/
1622-fancy-sequence.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* 1622. Fancy Sequence
* https://leetcode.com/problems/fancy-sequence/
* Difficulty: Hard
*
* Write an API that generates fancy sequences using the append, addAll, and multAll operations.
*
* Implement the Fancy class:
* - Fancy() Initializes the object with an empty sequence.
* - void append(val) Appends an integer val to the end of the sequence.
* - void addAll(inc) Increments all existing values in the sequence by an integer inc.
* - void multAll(m) Multiplies all existing values in the sequence by an integer m.
* - int getIndex(idx) Gets the current value at index idx (0-indexed) of the sequence modulo
* 109 + 7. If the index is greater or equal than the length of the sequence, return -1.
*/
var Fancy = function() {
this.sequence = [];
this.additive = 0n;
this.multiplicative = 1n;
this.MOD = 1000000007n;
};
/**
* @param {number} val
* @return {void}
*/
Fancy.prototype.append = function(val) {
const valBig = BigInt(val);
const inverse = this.modInverse(this.multiplicative);
const adjustedVal = ((valBig - this.additive + this.MOD) * inverse) % this.MOD;
this.sequence.push(adjustedVal);
};
/**
* @param {number} inc
* @return {void}
*/
Fancy.prototype.addAll = function(inc) {
this.additive = (this.additive + BigInt(inc)) % this.MOD;
};
/**
* @param {number} m
* @return {void}
*/
Fancy.prototype.multAll = function(m) {
const mBig = BigInt(m);
this.additive = (this.additive * mBig) % this.MOD;
this.multiplicative = (this.multiplicative * mBig) % this.MOD;
};
/**
* @param {number} idx
* @return {number}
*/
Fancy.prototype.getIndex = function(idx) {
if (idx >= this.sequence.length) return -1;
return Number((this.sequence[idx] * this.multiplicative + this.additive) % this.MOD);
};
/**
* @param {number} a
* @return {number}
*/
Fancy.prototype.modInverse = function(a) {
let m = this.MOD;
const m0 = m;
let x = 1n;
let y = 0n;
a = a % m;
while (a > 1n) {
const q = a / m;
[a, m] = [m, a % m];
[x, y] = [y, x - q * y];
}
return x < 0n ? x + m0 : x;
};