|
| 1 | +--- |
| 2 | +id: fancy-sequence |
| 3 | +title: Fancy Sequence |
| 4 | +sidebar_label: 1622 Fancy Sequence |
| 5 | +tags: |
| 6 | +- Math |
| 7 | +- Design |
| 8 | +- Segment Tree |
| 9 | +description: "This document provides a solution where we need to Write an API that generates fancy sequences using the append, addAll, and multAll operations. " |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem |
| 13 | + |
| 14 | +Write an API that generates fancy sequences using the $append$, $addAll$, and $multAll$ operations. |
| 15 | + |
| 16 | +Implement the $Fancy$ class: |
| 17 | + |
| 18 | +- Fancy() Initializes the object with an empty sequence. |
| 19 | + |
| 20 | +- void append(val) Appends an integer val to the end of the sequence. |
| 21 | + |
| 22 | +- void addAll(inc) Increments all existing values in the sequence by an integer inc. |
| 23 | + |
| 24 | +- void multAll(m) Multiplies all existing values in the sequence by an integer m. |
| 25 | + |
| 26 | +- int getIndex(idx) Gets the current value at index $idx$ (0-indexed) of the sequence **modulo** $10^9$ + $7$. If the index is greater or equal than the length of the sequence, return $-1$. |
| 27 | + |
| 28 | +### Examples |
| 29 | + |
| 30 | +**Example 1:** |
| 31 | + |
| 32 | +``` |
| 33 | +Input: "["Fancy", "append", "addAll", "append", "multAll", "getIndex", "addAll", "append", "multAll", "getIndex", "getIndex", "getIndex"] |
| 34 | +[[], [2], [3], [7], [2], [0], [3], [10], [2], [0], [1], [2]]" |
| 35 | +
|
| 36 | +Output: "[null, null, null, null, null, 10, null, null, null, 26, 34, 20]" |
| 37 | +
|
| 38 | +Explanation: |
| 39 | +
|
| 40 | +Fancy fancy = new Fancy(); |
| 41 | +fancy.append(2); // fancy sequence: [2] |
| 42 | +fancy.addAll(3); // fancy sequence: [2+3] -> [5] |
| 43 | +fancy.append(7); // fancy sequence: [5, 7] |
| 44 | +fancy.multAll(2); // fancy sequence: [5*2, 7*2] -> [10, 14] |
| 45 | +fancy.getIndex(0); // return 10 |
| 46 | +fancy.addAll(3); // fancy sequence: [10+3, 14+3] -> [13, 17] |
| 47 | +fancy.append(10); // fancy sequence: [13, 17, 10] |
| 48 | +fancy.multAll(2); // fancy sequence: [13*2, 17*2, 10*2] -> [26, 34, 20] |
| 49 | +fancy.getIndex(0); // return 26 |
| 50 | +fancy.getIndex(1); // return 34 |
| 51 | +fancy.getIndex(2); // return 20 |
| 52 | +``` |
| 53 | + |
| 54 | +### Constraints |
| 55 | + |
| 56 | +- `1 <= val, inc, m <= 100` |
| 57 | +- `0 <= idx <= 10^5` |
| 58 | +- At most $10^5$ calls total will be made to $append$, $addAll$, $multAll$, and $getIndex$. |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## Approach |
| 63 | + |
| 64 | +To solve the problem, we need to understand the nature of the allowed moves: |
| 65 | + |
| 66 | +1. **Using Lazy Operations**: |
| 67 | + |
| 68 | + - Instead of updating the entire sequence for $addAll$ and $multAll$, we maintain cumulative addition and multiplication factors. This way, we can defer the actual updates to when we need to access a particular element. |
| 69 | + |
| 70 | +2. **Tracking Inverse Operations**: |
| 71 | + |
| 72 | + - When appending a new value, we need to account for the current cumulative operations, so we store values in a form that can be easily adjusted when retrieving them. |
| 73 | + |
| 74 | +3. **Efficient Index Retrieval**: |
| 75 | + |
| 76 | + - By maintaining cumulative operations and only applying them when accessing an index, we ensure that our operations are efficient and avoid unnecessary updates to the sequence. |
| 77 | + |
| 78 | +## Solution for Fancy Sequence |
| 79 | + |
| 80 | +The problem involves three types of operations on a sequence of numbers: appending a value, adding a value to all elements, and multiplying all elements by a value. A naive approach would directly modify the sequence for each operation, but this would be inefficient given the constraints. Instead, we can use lazy propagation-like techniques to efficiently handle the operations. |
| 81 | + |
| 82 | +#### Code in Java |
| 83 | + |
| 84 | +```java |
| 85 | +import java.util.ArrayList; |
| 86 | +import java.util.List; |
| 87 | + |
| 88 | +class Fancy { |
| 89 | + private static final int MOD = 1000000007; |
| 90 | + private List<Long> sequence; |
| 91 | + private long add; |
| 92 | + private long mult; |
| 93 | + |
| 94 | + public Fancy() { |
| 95 | + sequence = new ArrayList<>(); |
| 96 | + add = 0; |
| 97 | + mult = 1; |
| 98 | + } |
| 99 | + |
| 100 | + public void append(int val) { |
| 101 | + // Apply the inverse of the current add and mult to keep the value as its original form |
| 102 | + long adjustedVal = ((val - add) * modInverse(mult, MOD)) % MOD; |
| 103 | + if (adjustedVal < 0) adjustedVal += MOD; |
| 104 | + sequence.add(adjustedVal); |
| 105 | + } |
| 106 | + |
| 107 | + public void addAll(int inc) { |
| 108 | + add = (add + inc) % MOD; |
| 109 | + } |
| 110 | + |
| 111 | + public void multAll(int m) { |
| 112 | + add = (add * m) % MOD; |
| 113 | + mult = (mult * m) % MOD; |
| 114 | + } |
| 115 | + |
| 116 | + public int getIndex(int idx) { |
| 117 | + if (idx >= sequence.size()) return -1; |
| 118 | + long result = (sequence.get(idx) * mult + add) % MOD; |
| 119 | + return (int) result; |
| 120 | + } |
| 121 | + |
| 122 | + // Function to compute the modular inverse using Fermat's Little Theorem |
| 123 | + private long modInverse(long a, int mod) { |
| 124 | + return power(a, mod - 2, mod); |
| 125 | + } |
| 126 | + |
| 127 | + // Function to compute (x^y) % mod |
| 128 | + private long power(long x, int y, int mod) { |
| 129 | + if (y == 0) return 1; |
| 130 | + long p = power(x, y / 2, mod) % mod; |
| 131 | + p = (p * p) % mod; |
| 132 | + return (y % 2 == 0) ? p : (x * p) % mod; |
| 133 | + } |
| 134 | + |
| 135 | + public static void main(String[] args) { |
| 136 | + Fancy fancy = new Fancy(); |
| 137 | + fancy.append(2); |
| 138 | + fancy.addAll(3); |
| 139 | + fancy.append(7); |
| 140 | + fancy.multAll(2); |
| 141 | + System.out.println(fancy.getIndex(0)); |
| 142 | + fancy.addAll(3); |
| 143 | + fancy.append(10); |
| 144 | + fancy.multAll(2); |
| 145 | + System.out.println(fancy.getIndex(0)); |
| 146 | + System.out.println(fancy.getIndex(1)); |
| 147 | + System.out.println(fancy.getIndex(2)); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +``` |
| 152 | + |
| 153 | +### Complexity Analysis |
| 154 | + |
| 155 | +#### Time Complexity: $O(1)$ |
| 156 | + |
| 157 | +> **Reason**: Time Complexity is $O(1)$, because append involves adding an element to the list, Whereas addAll only updates a cumulative variable, multAll updates cumulative variables and lastly getIndex, it basically involves getIndex calculating the result using the cumulative variables. |
| 158 | +
|
| 159 | +#### Space Complexity: $O(n)$ |
| 160 | + |
| 161 | +> **Reason**: $O(n)$, where n is the number of elements in the sequence, as we store each element. |
| 162 | +
|
| 163 | +# References |
| 164 | + |
| 165 | +- **LeetCode Problem:** [Fancy Sequence](https://leetcode.com/problems/fancy-sequence/description/) |
| 166 | +- **Solution Link:** [Fancy Sequence Solution on LeetCode](https://leetcode.com/problems/fancy-sequence/solutions/) |
| 167 | +- **Authors LeetCode Profile:** [Vivek Vardhan](https://leetcode.com/u/vivekvardhan43862/) |
0 commit comments