1476. Subrectangle Queries Medium

@problem@discussion
#Array#Design#Matrix



1/**
2 * [1476] Subrectangle Queries
3 *
4 * Implement the class SubrectangleQueries which receives a rows x cols rectangle as a matrix of integers in the constructor and supports two methods:
5 * 1. updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)
6 * 
7 * 	Updates all values with newValue in the subrectangle whose upper left coordinate is (row1,col1) and bottom right coordinate is (row2,col2).
8 * 
9 * 2. getValue(int row, int col)
10 * 
11 * 	Returns the current value of the coordinate (row,col) from the rectangle.
12 * 
13 *  
14 * Example 1:
15 * 
16 * Input
17 * ["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue","getValue"]
18 * [[[[1,2,1],[4,3,4],[3,2,1],[1,1,1]]],[0,2],[0,0,3,2,5],[0,2],[3,1],[3,0,3,2,10],[3,1],[0,2]]
19 * Output
20 * [null,1,null,5,5,null,10,5]
21 * Explanation
22 * SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]);  
23 * // The initial rectangle (4x3) looks like:
24 * // 1 2 1
25 * // 4 3 4
26 * // 3 2 1
27 * // 1 1 1
28 * subrectangleQueries.getValue(0, 2); // return 1
29 * subrectangleQueries.updateSubrectangle(0, 0, 3, 2, 5);
30 * // After this update the rectangle looks like:
31 * // 5 5 5
32 * // 5 5 5
33 * // 5 5 5
34 * // 5 5 5 
35 * subrectangleQueries.getValue(0, 2); // return 5
36 * subrectangleQueries.getValue(3, 1); // return 5
37 * subrectangleQueries.updateSubrectangle(3, 0, 3, 2, 10);
38 * // After this update the rectangle looks like:
39 * // 5   5   5
40 * // 5   5   5
41 * // 5   5   5
42 * // 10  10  10 
43 * subrectangleQueries.getValue(3, 1); // return 10
44 * subrectangleQueries.getValue(0, 2); // return 5
45 * 
46 * Example 2:
47 * 
48 * Input
49 * ["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue"]
50 * [[[[1,1,1],[2,2,2],[3,3,3]]],[0,0],[0,0,2,2,100],[0,0],[2,2],[1,1,2,2,20],[2,2]]
51 * Output
52 * [null,1,null,100,100,null,20]
53 * Explanation
54 * SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,1,1],[2,2,2],[3,3,3]]);
55 * subrectangleQueries.getValue(0, 0); // return 1
56 * subrectangleQueries.updateSubrectangle(0, 0, 2, 2, 100);
57 * subrectangleQueries.getValue(0, 0); // return 100
58 * subrectangleQueries.getValue(2, 2); // return 100
59 * subrectangleQueries.updateSubrectangle(1, 1, 2, 2, 20);
60 * subrectangleQueries.getValue(2, 2); // return 20
61 * 
62 *  
63 * Constraints:
64 * 
65 * 	There will be at most <font face="monospace">500</font> operations considering both methods: updateSubrectangle and getValue.
66 * 	1 <= rows, cols <= 100
67 * 	rows == rectangle.length
68 * 	cols == rectangle[i].length
69 * 	0 <= row1 <= row2 < rows
70 * 	0 <= col1 <= col2 < cols
71 * 	1 <= newValue, rectangle[i][j] <= 10^9
72 * 	0 <= row < rows
73 * 	0 <= col < cols
74 * 
75 */
76pub struct Solution {}
77
78// problem: https://leetcode.com/problems/subrectangle-queries/
79// discuss: https://leetcode.com/problems/subrectangle-queries/discuss/?currentPage=1&orderBy=most_votes&query=
80
81// submission codes start here
82
83struct SubrectangleQueries {
84        false
85    }
86
87
88/** 
89 * `&self` means the method takes an immutable reference.
90 * If you need a mutable reference, change it to `&mut self` instead.
91 */
92impl SubrectangleQueries {
93
94    fn new(rectangle: Vec<Vec<i32>>) -> Self {
95        
96    }
97    
98    fn update_subrectangle(&self, row1: i32, col1: i32, row2: i32, col2: i32, new_value: i32) {
99        
100    }
101    
102    fn get_value(&self, row: i32, col: i32) -> i32 {
103        
104    }
105}
106
107/**
108 * Your SubrectangleQueries object will be instantiated and called as such:
109 * let obj = SubrectangleQueries::new(rectangle);
110 * obj.update_subrectangle(row1, col1, row2, col2, newValue);
111 * let ret_2: i32 = obj.get_value(row, col);
112 */
113
114// submission codes end
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119
120    #[test]
121    fn test_1476() {
122    }
123}
124


Back
© 2025 bowen.ge All Rights Reserved.