-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrange-sum-query-immutable.rs
60 lines (56 loc) · 1.53 KB
/
range-sum-query-immutable.rs
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
// 303. Range Sum Query - Immutable
// 🟢 Easy
//
// https://leetcode.com/problems/range-sum-query-immutable/
//
// Tags: Array - Design - Prefix Sum
struct NumArray {
prefix_sums: Vec<i32>,
}
/**
* Create an array of sums. When a range sum is requested, fetch it from
* prefix_sums(right - left).
*
* Time complexity: O(n) - To create the prefix sum array we iterate over
* the entire input array, then O(1) for the sum_range method.
* Space complexity: O(n) - We store an array of prefix sums of the same
* size as the input array.
*
* Runtime 6 ms Beats 77.78%
* Memory 8.4 MB Beats 90.48%
*/
impl NumArray {
/**
* The constructor creates a vector of prefix sums.
*/
fn new(nums: Vec<i32>) -> Self {
NumArray {
prefix_sums: nums
.iter()
.scan(0, |state, &x| {
*state = *state + x;
Some(*state)
})
.collect(),
}
}
/**
* Use the prefix sums to compute the result in O(1)
*/
fn sum_range(&self, left: i32, right: i32) -> i32 {
self.prefix_sums[right as usize]
- if left > 0 {
self.prefix_sums[left as usize - 1]
} else {
0
}
}
}
// Tests.
fn main() {
let num_array = NumArray::new(vec![-2, 0, 3, -5, 2, -1]);
assert_eq!(num_array.sum_range(0, 2), 1);
assert_eq!(num_array.sum_range(2, 5), -1);
assert_eq!(num_array.sum_range(0, 5), -3);
println!("All tests passed!")
}