Fancy Sequence Problem
Description
LeetCode Problem 1622.
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 integervalto the end of the sequence.void addAll(inc)Increments all existing values in the sequence by an integerinc.void multAll(m)Multiplies all existing values in the sequence by an integerm.int getIndex(idx)Gets the current value at indexidx(0-indexed) of the sequence modulo10^9 + 7. If the index is greater or equal than the length of the sequence, return-1.
Example 1:
Input
["Fancy", "append", "addAll", "append", "multAll", "getIndex", "addAll", "append", "multAll", "getIndex", "getIndex", "getIndex"]
[[], [2], [3], [7], [2], [0], [3], [10], [2], [0], [1], [2]]
Output
[null, null, null, null, null, 10, null, null, null, 26, 34, 20]
Explanation
Fancy fancy = new Fancy();
fancy.append(2); // fancy sequence: [2]
fancy.addAll(3); // fancy sequence: [2+3] -> [5]
fancy.append(7); // fancy sequence: [5, 7]
fancy.multAll(2); // fancy sequence: [5*2, 7*2] -> [10, 14]
fancy.getIndex(0); // return 10
fancy.addAll(3); // fancy sequence: [10+3, 14+3] -> [13, 17]
fancy.append(10); // fancy sequence: [13, 17, 10]
fancy.multAll(2); // fancy sequence: [13*2, 17*2, 10*2] -> [26, 34, 20]
fancy.getIndex(0); // return 26
fancy.getIndex(1); // return 34
fancy.getIndex(2); // return 20
Constraints:
1 <= val, inc, m <= 1000 <= idx <= 10^5- At most
10^5calls total will be made toappend,addAll,multAll, andgetIndex.
Solution
Approach: One Global Linear Transform and Modular Inverses
Applying addAll or multAll to every element would cost O(n) per call. But both operations act on all elements present at that moment, so for any element the operations after it was appended combine into one function x -> mul · x + add. Keep that function globally, as two numbers mul and add:
addAll(inc):add += inc.multAll(m):(mul · x + add) · m = (mul · m) · x + add · m, so multiply bothmulandaddbym.getIndex(idx): the real value ismul · stored[idx] + add.
The catch is append: a new element must not be affected by the earlier operations, yet getIndex applies the whole global function to it. So instead of val, store the x that the function turns into val: mul · x + add = val, so x = (val - add) / mul.
All values are taken modulo 109 + 7, so "divide by mul" means "multiply by the modular inverse of mul". The modulus is prime and mul is never 0 (every m >= 1 is smaller than the modulus), so by Fermat's little theorem the inverse is mul^(MOD-2) mod MOD, computed with fast exponentiation.
Keep products in 64-bit integers: two numbers below 109 + 7 multiply to about 1018. JavaScript numbers are exact only up to 253, so the JavaScript version multiplies in two 16-bit halves.
Complexity: O(log MOD) time for append and O(1) for the other operations; O(n) space for the stored values.
MOD = 10**9 + 7
class Fancy:
def __init__(self):
# The real value at index i is (mul * stored[i] + add) % MOD.
self.stored = []
self.mul = 1
self.add = 0
def append(self, val: int) -> None:
# Store x with mul * x + add == val, so the new value ignores the earlier operations.
inv = pow(self.mul, MOD - 2, MOD) # MOD is prime, so this is 1 / mul
self.stored.append((val - self.add) * inv % MOD)
def addAll(self, inc: int) -> None:
self.add = (self.add + inc) % MOD
def multAll(self, m: int) -> None:
self.mul = self.mul * m % MOD
self.add = self.add * m % MOD
def getIndex(self, idx: int) -> int:
if idx >= len(self.stored):
return -1
return (self.mul * self.stored[idx] + self.add) % MOD