|
| 1 | +/** |
| 2 | + * [304] Range Sum Query 2D - Immutable |
| 3 | + * |
| 4 | + * Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). |
| 5 | + * |
| 6 | + * |
| 7 | + * <img src="/static/images/courses/range_sum_query_2d.png" border="0" alt="Range Sum Query 2D" /><br /> |
| 8 | + * <small>The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.</small> |
| 9 | + * |
| 10 | + * |
| 11 | + * Example:<br> |
| 12 | + * |
| 13 | + * Given matrix = [ |
| 14 | + * [3, 0, 1, 4, 2], |
| 15 | + * [5, 6, 3, 2, 1], |
| 16 | + * [1, 2, 0, 1, 5], |
| 17 | + * [4, 1, 0, 1, 7], |
| 18 | + * [1, 0, 3, 0, 5] |
| 19 | + * ] |
| 20 | + * |
| 21 | + * sumRegion(2, 1, 4, 3) -> 8 |
| 22 | + * sumRegion(1, 1, 2, 2) -> 11 |
| 23 | + * sumRegion(1, 2, 2, 4) -> 12 |
| 24 | + * |
| 25 | + * |
| 26 | + * |
| 27 | + * Note:<br> |
| 28 | + * <ol> |
| 29 | + * You may assume that the matrix does not change. |
| 30 | + * There are many calls to sumRegion function. |
| 31 | + * You may assume that row1 ≤ row2 and col1 ≤ col2. |
| 32 | + * </ol> |
| 33 | + * |
| 34 | + */ |
| 35 | +pub struct Solution {} |
| 36 | + |
| 37 | +// submission codes start here |
| 38 | + |
| 39 | +struct NumMatrix { |
| 40 | + cache: Vec<Vec<i32>> |
| 41 | +} |
| 42 | + |
| 43 | +/** region[2, 2, 3, 4] = |
| 44 | + * x x \ \ \ . square[3,4] - square[1,4] - square[3,1] + square[1,1] |
| 45 | + * x x \ \ \ . |
| 46 | + * / / o o o . |
| 47 | + * / / o o o . |
| 48 | + * . . . . . . |
| 49 | + * . . . . . . |
| 50 | + */ |
| 51 | +impl NumMatrix { |
| 52 | + fn new(matrix: Vec<Vec<i32>>) -> Self { |
| 53 | + if matrix.is_empty() || matrix[0].is_empty() { |
| 54 | + return NumMatrix{cache: vec![vec![]]} |
| 55 | + } |
| 56 | + let (x_max, y_max) = (matrix.len(), matrix[0].len()); |
| 57 | + let mut cache = vec![vec![0; y_max]; x_max]; |
| 58 | + for x in 0..x_max { |
| 59 | + for y in 0..y_max { |
| 60 | + cache[x][y] = matrix[x][y] + |
| 61 | + if y > 0 { cache[x][y-1] } else { 0 } + |
| 62 | + if x > 0 { cache[x-1][y] } else { 0 } - |
| 63 | + if x > 0 && y > 0 { cache[x-1][y-1] } else { 0 } |
| 64 | + } |
| 65 | + } |
| 66 | + NumMatrix{cache: cache} |
| 67 | + } |
| 68 | + |
| 69 | + fn sum_region(&self, row1: i32, col1: i32, row2: i32, col2: i32) -> i32 { |
| 70 | + let (row1,col1,row2,col2) = (row1 as usize, col1 as usize, row2 as usize, col2 as usize); |
| 71 | + self.cache[row2][col2] - |
| 72 | + if row1 > 0 { self.cache[row1-1][col2] } else { 0 } - |
| 73 | + if col1 > 0 { self.cache[row2][col1-1] } else { 0 } + |
| 74 | + if row1 > 0 && col1 > 0 { self.cache[row1-1][col1-1]} else { 0 } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Your NumMatrix object will be instantiated and called as such: |
| 80 | + * let obj = NumMatrix::new(matrix); |
| 81 | + * let ret_1: i32 = obj.sum_region(row1, col1, row2, col2); |
| 82 | + */ |
| 83 | + |
| 84 | +// submission codes end |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use super::*; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_304() { |
| 92 | + let matrix = NumMatrix::new( |
| 93 | + vec![ |
| 94 | + vec![3, 0, 1, 4, 2], |
| 95 | + vec![5, 6, 3, 2, 1], |
| 96 | + vec![1, 2, 0, 1, 5], |
| 97 | + vec![4, 1, 0, 1, 7], |
| 98 | + vec![1, 0, 3, 0, 5] |
| 99 | + ] |
| 100 | + ); |
| 101 | + assert_eq!(matrix.sum_region(1, 1, 2, 2), 11); |
| 102 | + assert_eq!(matrix.sum_region(2, 1, 4, 3), 8); |
| 103 | + assert_eq!(matrix.sum_region(1, 2, 2, 4), 12); |
| 104 | + } |
| 105 | +} |
0 commit comments