Skip to content

Commit ec8f4bb

Browse files
committed
feat: add solutions to lc problem: No.1476. Subrectangle Queries
1 parent 3a35bba commit ec8f4bb

File tree

13 files changed

+3882
-3351
lines changed

13 files changed

+3882
-3351
lines changed

solution/1400-1499/1476.Subrectangle Queries/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,72 @@ subrectangleQueries.getValue(2, 2); // 返回 20
9393

9494
<!-- 这里可写通用的实现逻辑 -->
9595

96+
用历史记录列表保存修改历史。
97+
9698
<!-- tabs:start -->
9799

98100
### **Python3**
99101

100102
<!-- 这里可写当前语言的特殊实现逻辑 -->
101103

102104
```python
105+
class SubrectangleQueries:
106+
107+
def __init__(self, rectangle: List[List[int]]):
108+
self.rec = rectangle
109+
self.history = []
110+
111+
def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:
112+
self.history.append((row1, col1, row2, col2, newValue))
103113

114+
def getValue(self, row: int, col: int) -> int:
115+
for row1, col1, row2, col2, newValue in self.history[::-1]:
116+
if row >= row1 and row <= row2 and col >= col1 and col <= col2:
117+
return newValue
118+
return self.rec[row][col]
119+
120+
121+
# Your SubrectangleQueries object will be instantiated and called as such:
122+
# obj = SubrectangleQueries(rectangle)
123+
# obj.updateSubrectangle(row1,col1,row2,col2,newValue)
124+
# param_2 = obj.getValue(row,col)
104125
```
105126

106127
### **Java**
107128

108129
<!-- 这里可写当前语言的特殊实现逻辑 -->
109130

110131
```java
111-
132+
class SubrectangleQueries {
133+
private int[][] rec;
134+
private List<int[]> history;
135+
136+
public SubrectangleQueries(int[][] rectangle) {
137+
rec = rectangle;
138+
history = new ArrayList<>();
139+
}
140+
141+
public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
142+
history.add(new int[]{row1, col1, row2, col2, newValue});
143+
}
144+
145+
public int getValue(int row, int col) {
146+
for (int i = history.size() - 1; i >= 0; --i) {
147+
int[] record = history.get(i);
148+
if (row >= record[0] && row <= record[2] && col >= record[1] && col <= record[3]) {
149+
return record[4];
150+
}
151+
}
152+
return rec[row][col];
153+
}
154+
}
155+
156+
/**
157+
* Your SubrectangleQueries object will be instantiated and called as such:
158+
* SubrectangleQueries obj = new SubrectangleQueries(rectangle);
159+
* obj.updateSubrectangle(row1,col1,row2,col2,newValue);
160+
* int param_2 = obj.getValue(row,col);
161+
*/
112162
```
113163

114164
### **...**

solution/1400-1499/1476.Subrectangle Queries/README_EN.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,68 @@ subrectangleQueries.getValue(2, 2); // return 20
154154

155155
## Solutions
156156

157+
Use history list to save the updated record.
158+
157159
<!-- tabs:start -->
158160

159161
### **Python3**
160162

161163
```python
164+
class SubrectangleQueries:
165+
166+
def __init__(self, rectangle: List[List[int]]):
167+
self.rec = rectangle
168+
self.history = []
169+
170+
def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:
171+
self.history.append((row1, col1, row2, col2, newValue))
162172

173+
def getValue(self, row: int, col: int) -> int:
174+
for row1, col1, row2, col2, newValue in self.history[::-1]:
175+
if row >= row1 and row <= row2 and col >= col1 and col <= col2:
176+
return newValue
177+
return self.rec[row][col]
178+
179+
180+
# Your SubrectangleQueries object will be instantiated and called as such:
181+
# obj = SubrectangleQueries(rectangle)
182+
# obj.updateSubrectangle(row1,col1,row2,col2,newValue)
183+
# param_2 = obj.getValue(row,col)
163184
```
164185

165186
### **Java**
166187

167188
```java
168-
189+
class SubrectangleQueries {
190+
private int[][] rec;
191+
private List<int[]> history;
192+
193+
public SubrectangleQueries(int[][] rectangle) {
194+
rec = rectangle;
195+
history = new ArrayList<>();
196+
}
197+
198+
public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
199+
history.add(new int[]{row1, col1, row2, col2, newValue});
200+
}
201+
202+
public int getValue(int row, int col) {
203+
for (int i = history.size() - 1; i >= 0; --i) {
204+
int[] record = history.get(i);
205+
if (row >= record[0] && row <= record[2] && col >= record[1] && col <= record[3]) {
206+
return record[4];
207+
}
208+
}
209+
return rec[row][col];
210+
}
211+
}
212+
213+
/**
214+
* Your SubrectangleQueries object will be instantiated and called as such:
215+
* SubrectangleQueries obj = new SubrectangleQueries(rectangle);
216+
* obj.updateSubrectangle(row1,col1,row2,col2,newValue);
217+
* int param_2 = obj.getValue(row,col);
218+
*/
169219
```
170220

171221
### **...**

solution/1400-1499/1476.Subrectangle Queries/Solution.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
class SubrectangleQueries {
2-
3-
int[][] matrix;
2+
private int[][] rec;
3+
private List<int[]> history;
44

55
public SubrectangleQueries(int[][] rectangle) {
6-
matrix = new int[rectangle.length][rectangle[0].length];
7-
for (int i = 0; i < rectangle.length; i++) {
8-
for (int j = 0; j < rectangle[0].length; j++) {
9-
matrix[i][j] = rectangle[i][j];
10-
}
11-
}
6+
rec = rectangle;
7+
history = new ArrayList<>();
128
}
13-
9+
1410
public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
15-
if (row1 > row2 || col1 > col2) {
16-
return;
17-
}
18-
for (int i = row1; i <= row2; i++) {
19-
for (int j = col1; j <= col2; j++) {
20-
matrix[i][j] = newValue;
21-
}
22-
}
11+
history.add(new int[]{row1, col1, row2, col2, newValue});
2312
}
24-
13+
2514
public int getValue(int row, int col) {
26-
return matrix[row][col];
15+
for (int i = history.size() - 1; i >= 0; --i) {
16+
int[] record = history.get(i);
17+
if (row >= record[0] && row <= record[2] && col >= record[1] && col <= record[3]) {
18+
return record[4];
19+
}
20+
}
21+
return rec[row][col];
2722
}
2823
}
2924

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class SubrectangleQueries:
2+
3+
def __init__(self, rectangle: List[List[int]]):
4+
self.rec = rectangle
5+
self.history = []
6+
7+
def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:
8+
self.history.append((row1, col1, row2, col2, newValue))
9+
10+
def getValue(self, row: int, col: int) -> int:
11+
for row1, col1, row2, col2, newValue in self.history[::-1]:
12+
if row >= row1 and row <= row2 and col >= col1 and col <= col2:
13+
return newValue
14+
return self.rec[row][col]
15+
16+
17+
# Your SubrectangleQueries object will be instantiated and called as such:
18+
# obj = SubrectangleQueries(rectangle)
19+
# obj.updateSubrectangle(row1,col1,row2,col2,newValue)
20+
# param_2 = obj.getValue(row,col)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# [1907. Count Salary Categories](https://leetcode-cn.com/problems/count-salary-categories)
2+
3+
[English Version](/solution/1900-1999/1907.Count%20Salary%20Categories/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Accounts</code></p>
10+
11+
<pre>
12+
+-------------+------+
13+
| Column Name | Type |
14+
+-------------+------+
15+
| account_id | int |
16+
| income | int |
17+
+-------------+------+
18+
account_id is the primary key for this table.
19+
Each row contains information about the monthly income for one bank account.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
24+
<p>Write an SQL query to report the number of bank accounts of each salary category. The salary categories are:</p>
25+
26+
<ul>
27+
<li><code>&quot;Low Salary&quot;</code>: All the salaries <strong>strictly less</strong> than <code>$20000</code>.</li>
28+
<li><code>&quot;Average Salary&quot;</code>: All the salaries in the <strong>inclusive</strong> range <code>[$20000, $50000]</code>.</li>
29+
<li><code>&quot;High Salary&quot;</code>: All the salaries <strong>strictly greater</strong> than <code>$50000</code>.</li>
30+
</ul>
31+
32+
<p>The result table <strong>must</strong> contain all three categories. If there are no accounts in a category, then report <code>0</code>. Return the result table in <strong>any order</strong>.</p>
33+
34+
<p>The query result format is in the following example.</p>
35+
36+
<p>&nbsp;</p>
37+
38+
<pre>
39+
Accounts table:
40+
+------------+--------+
41+
| account_id | income |
42+
+------------+--------+
43+
| 3 | 108939 |
44+
| 2 | 12747 |
45+
| 8 | 87709 |
46+
| 6 | 91796 |
47+
+------------+--------+
48+
49+
Result table:
50+
+----------------+----------------+
51+
| category | accounts_count |
52+
+----------------+----------------+
53+
| Low Salary | 1 |
54+
| Average Salary | 0 |
55+
| High Salary | 3 |
56+
+----------------+----------------+
57+
58+
Low Salary: Account 2.
59+
Average Salary: No accounts.
60+
High Salary: Accounts 3, 6, and 8.
61+
</pre>
62+
63+
64+
## 解法
65+
66+
<!-- 这里可写通用的实现逻辑 -->
67+
68+
<!-- tabs:start -->
69+
70+
### **SQL**
71+
72+
<!-- 这里可写当前语言的特殊实现逻辑 -->
73+
74+
```sql
75+
76+
```
77+
78+
<!-- tabs:end -->
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [1907. Count Salary Categories](https://leetcode.com/problems/count-salary-categories)
2+
3+
[中文文档](/solution/1900-1999/1907.Count%20Salary%20Categories/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Accounts</code></p>
8+
9+
<pre>
10+
+-------------+------+
11+
| Column Name | Type |
12+
+-------------+------+
13+
| account_id | int |
14+
| income | int |
15+
+-------------+------+
16+
account_id is the primary key for this table.
17+
Each row contains information about the monthly income for one bank account.
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
22+
<p>Write an SQL query to report the number of bank accounts of each salary category. The salary categories are:</p>
23+
24+
<ul>
25+
<li><code>&quot;Low Salary&quot;</code>: All the salaries <strong>strictly less</strong> than <code>$20000</code>.</li>
26+
<li><code>&quot;Average Salary&quot;</code>: All the salaries in the <strong>inclusive</strong> range <code>[$20000, $50000]</code>.</li>
27+
<li><code>&quot;High Salary&quot;</code>: All the salaries <strong>strictly greater</strong> than <code>$50000</code>.</li>
28+
</ul>
29+
30+
<p>The result table <strong>must</strong> contain all three categories. If there are no accounts in a category, then report <code>0</code>. Return the result table in <strong>any order</strong>.</p>
31+
32+
<p>The query result format is in the following example.</p>
33+
34+
<p>&nbsp;</p>
35+
36+
<pre>
37+
Accounts table:
38+
+------------+--------+
39+
| account_id | income |
40+
+------------+--------+
41+
| 3 | 108939 |
42+
| 2 | 12747 |
43+
| 8 | 87709 |
44+
| 6 | 91796 |
45+
+------------+--------+
46+
47+
Result table:
48+
+----------------+----------------+
49+
| category | accounts_count |
50+
+----------------+----------------+
51+
| Low Salary | 1 |
52+
| Average Salary | 0 |
53+
| High Salary | 3 |
54+
+----------------+----------------+
55+
56+
Low Salary: Account 2.
57+
Average Salary: No accounts.
58+
High Salary: Accounts 3, 6, and 8.
59+
</pre>
60+
61+
62+
## Solutions
63+
64+
<!-- tabs:start -->
65+
66+
### **SQL**
67+
68+
```sql
69+
70+
```
71+
72+
<!-- tabs:end -->

0 commit comments

Comments
 (0)