|
| 1 | +# [1476. 子矩形查询](https://leetcode.cn/problems/subrectangle-queries/) |
| 2 | + |
| 3 | +- 标签:设计、数组、矩阵 |
| 4 | +- 难度:中等 |
| 5 | + |
| 6 | +## 题目链接 |
| 7 | + |
| 8 | +- [1476. 子矩形查询 - 力扣](https://leetcode.cn/problems/subrectangle-queries/) |
| 9 | + |
| 10 | +## 题目大意 |
| 11 | + |
| 12 | +**要求**:实现一个类 SubrectangleQueries,它的构造函数的参数是一个 $rows \times cols $的矩形(这里用整数矩阵表示),并支持以下两种操作: |
| 13 | + |
| 14 | +1. `updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)`:用 $newValue$ 更新以 $(row1,col1)$ 为左上角且以 $(row2,col2)$ 为右下角的子矩形。 |
| 15 | + |
| 16 | +2. `getValue(int row, int col)`:返回矩形中坐标 (row,col) 的当前值。 |
| 17 | + |
| 18 | +**说明**: |
| 19 | + |
| 20 | +- 最多有 $500$ 次 `updateSubrectangle` 和 `getValue` 操作。 |
| 21 | +- $1 <= rows, cols <= 100$。 |
| 22 | +- $rows == rectangle.length$。 |
| 23 | +- $cols == rectangle[i].length$。 |
| 24 | +- $0 <= row1 <= row2 < rows$。 |
| 25 | +- $0 <= col1 <= col2 < cols$。 |
| 26 | +- $1 <= newValue, rectangle[i][j] <= 10^9$。 |
| 27 | +- $0 <= row < rows$。 |
| 28 | +- $0 <= col < cols$。 |
| 29 | + |
| 30 | +**示例**: |
| 31 | + |
| 32 | +- 示例 1: |
| 33 | + |
| 34 | +```python |
| 35 | +输入: |
| 36 | +["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue","getValue"] |
| 37 | +[[[[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]] |
| 38 | +输出: |
| 39 | +[null,1,null,5,5,null,10,5] |
| 40 | +解释: |
| 41 | +SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,2,1],[4,3,4],[3,2,1],[1,1,1]]); |
| 42 | +// 初始的 (4x3) 矩形如下: |
| 43 | +// 1 2 1 |
| 44 | +// 4 3 4 |
| 45 | +// 3 2 1 |
| 46 | +// 1 1 1 |
| 47 | +subrectangleQueries.getValue(0, 2); // 返回 1 |
| 48 | +subrectangleQueries.updateSubrectangle(0, 0, 3, 2, 5); |
| 49 | +// 此次更新后矩形变为: |
| 50 | +// 5 5 5 |
| 51 | +// 5 5 5 |
| 52 | +// 5 5 5 |
| 53 | +// 5 5 5 |
| 54 | +subrectangleQueries.getValue(0, 2); // 返回 5 |
| 55 | +subrectangleQueries.getValue(3, 1); // 返回 5 |
| 56 | +subrectangleQueries.updateSubrectangle(3, 0, 3, 2, 10); |
| 57 | +// 此次更新后矩形变为: |
| 58 | +// 5 5 5 |
| 59 | +// 5 5 5 |
| 60 | +// 5 5 5 |
| 61 | +// 10 10 10 |
| 62 | +subrectangleQueries.getValue(3, 1); // 返回 10 |
| 63 | +subrectangleQueries.getValue(0, 2); // 返回 5 |
| 64 | +``` |
| 65 | + |
| 66 | +- 示例 2: |
| 67 | + |
| 68 | +```python |
| 69 | +输入: |
| 70 | +["SubrectangleQueries","getValue","updateSubrectangle","getValue","getValue","updateSubrectangle","getValue"] |
| 71 | +[[[[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]] |
| 72 | +输出: |
| 73 | +[null,1,null,100,100,null,20] |
| 74 | +解释: |
| 75 | +SubrectangleQueries subrectangleQueries = new SubrectangleQueries([[1,1,1],[2,2,2],[3,3,3]]); |
| 76 | +subrectangleQueries.getValue(0, 0); // 返回 1 |
| 77 | +subrectangleQueries.updateSubrectangle(0, 0, 2, 2, 100); |
| 78 | +subrectangleQueries.getValue(0, 0); // 返回 100 |
| 79 | +subrectangleQueries.getValue(2, 2); // 返回 100 |
| 80 | +subrectangleQueries.updateSubrectangle(1, 1, 2, 2, 20); |
| 81 | +subrectangleQueries.getValue(2, 2); // 返回 20 |
| 82 | + |
| 83 | +``` |
| 84 | + |
| 85 | +## 解题思路 |
| 86 | + |
| 87 | +### 思路 1:暴力 |
| 88 | + |
| 89 | +矩形最大为 $row \times col == 100 \times 100$,则每次更新最多需要更新 $10000$ 个值,更新次数最多为 $500$ 次。 |
| 90 | + |
| 91 | +用暴力更新的方法最多需要更新 $5000000$ 次,我们可以尝试一下用暴力更新的方法解决本题(提交后发现可以通过)。 |
| 92 | + |
| 93 | +### 思路 1:代码 |
| 94 | + |
| 95 | +```Python |
| 96 | +class SubrectangleQueries: |
| 97 | + |
| 98 | + def __init__(self, rectangle: List[List[int]]): |
| 99 | + self.rectangle = rectangle |
| 100 | + |
| 101 | + |
| 102 | + def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None: |
| 103 | + for row in range(row1, row2 + 1): |
| 104 | + for col in range(col1, col2 + 1): |
| 105 | + self.rectangle[row][col] = newValue |
| 106 | + |
| 107 | + |
| 108 | + def getValue(self, row: int, col: int) -> int: |
| 109 | + return self.rectangle[row][col] |
| 110 | +``` |
| 111 | + |
| 112 | +### 思路 1:复杂度分析 |
| 113 | + |
| 114 | +- **时间复杂度**:$O(row \times col \times 500)$。 |
| 115 | +- **空间复杂度**:$O(row \times col)$。 |
0 commit comments