1622. Fancy Sequence Hard

@problem@discussion
#Math#Design#Segment Tree



1/**
2 * [1622] Fancy Sequence
3 *
4 * Write an API that generates fancy sequences using the append, addAll, and multAll operations.
5 * Implement the Fancy class:
6 * 
7 * 	Fancy() Initializes the object with an empty sequence.
8 * 	void append(val) Appends an integer val to the end of the sequence.
9 * 	void addAll(inc) Increments all existing values in the sequence by an integer inc.
10 * 	void multAll(m) Multiplies all existing values in the sequence by an integer m.
11 * 	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.
12 * 
13 *  
14 * Example 1:
15 * 
16 * Input
17 * ["Fancy", "append", "addAll", "append", "multAll", "getIndex", "addAll", "append", "multAll", "getIndex", "getIndex", "getIndex"]
18 * [[], [2], [3], [7], [2], [0], [3], [10], [2], [0], [1], [2]]
19 * Output
20 * [null, null, null, null, null, 10, null, null, null, 26, 34, 20]
21 * Explanation
22 * Fancy fancy = new Fancy();
23 * fancy.append(2);   // fancy sequence: [2]
24 * fancy.addAll(3);   // fancy sequence: [2+3] -> [5]
25 * fancy.append(7);   // fancy sequence: [5, 7]
26 * fancy.multAll(2);  // fancy sequence: [5*2, 7*2] -> [10, 14]
27 * fancy.getIndex(0); // return 10
28 * fancy.addAll(3);   // fancy sequence: [10+3, 14+3] -> [13, 17]
29 * fancy.append(10);  // fancy sequence: [13, 17, 10]
30 * fancy.multAll(2);  // fancy sequence: [13*2, 17*2, 10*2] -> [26, 34, 20]
31 * fancy.getIndex(0); // return 26
32 * fancy.getIndex(1); // return 34
33 * fancy.getIndex(2); // return 20
34 * 
35 *  
36 * Constraints:
37 * 
38 * 	1 <= val, inc, m <= 100
39 * 	0 <= idx <= 10^5
40 * 	At most 10^5 calls total will be made to append, addAll, multAll, and getIndex.
41 * 
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/fancy-sequence/
46// discuss: https://leetcode.com/problems/fancy-sequence/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50struct Fancy {
51        false
52    }
53
54
55/** 
56 * `&self` means the method takes an immutable reference.
57 * If you need a mutable reference, change it to `&mut self` instead.
58 */
59impl Fancy {
60
61    fn new() -> Self {
62        
63    }
64    
65    fn append(&self, val: i32) {
66        
67    }
68    
69    fn add_all(&self, inc: i32) {
70        
71    }
72    
73    fn mult_all(&self, m: i32) {
74        
75    }
76    
77    fn get_index(&self, idx: i32) -> i32 {
78        
79    }
80}
81
82/**
83 * Your Fancy object will be instantiated and called as such:
84 * let obj = Fancy::new();
85 * obj.append(val);
86 * obj.add_all(inc);
87 * obj.mult_all(m);
88 * let ret_4: i32 = obj.get_index(idx);
89 */
90
91// submission codes end
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn test_1622() {
99    }
100}
101


Back
© 2025 bowen.ge All Rights Reserved.