Skip to content

Commit 20e5b98

Browse files
authored
feat: add typescript solution to lc problem: No.1476.Subrectangle Queries (doocs#478)
1 parent ec8f4bb commit 20e5b98

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,40 @@ class SubrectangleQueries {
161161
*/
162162
```
163163

164+
### **TypeScript**
165+
166+
```ts
167+
class SubrectangleQueries {
168+
grid: number[][];
169+
history: number[][];
170+
constructor(rectangle: number[][]) {
171+
this.grid = rectangle;
172+
this.history = [];
173+
}
174+
175+
updateSubrectangle(row1: number, col1: number, row2: number, col2: number, newValue: number): void {
176+
this.history.push([row1, col1, row2, col2, newValue]);
177+
}
178+
179+
getValue(row: number, col: number): number {
180+
for (let i = this.history.length - 1; i >= 0; --i) {
181+
let [row1, col1, row2, col2, newValue] = this.history[i];
182+
if (row >= row1 && row <= row2 && col >= col1 && col <= col2) {
183+
return newValue;
184+
}
185+
}
186+
return this.grid[row][col];
187+
}
188+
}
189+
190+
/**
191+
* Your SubrectangleQueries object will be instantiated and called as such:
192+
* var obj = new SubrectangleQueries(rectangle)
193+
* obj.updateSubrectangle(row1,col1,row2,col2,newValue)
194+
* var param_2 = obj.getValue(row,col)
195+
*/
196+
```
197+
164198
### **...**
165199

166200
```

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,40 @@ class SubrectangleQueries {
218218
*/
219219
```
220220

221+
### **TypeScript**
222+
223+
```ts
224+
class SubrectangleQueries {
225+
grid: number[][];
226+
history: number[][];
227+
constructor(rectangle: number[][]) {
228+
this.grid = rectangle;
229+
this.history = [];
230+
}
231+
232+
updateSubrectangle(row1: number, col1: number, row2: number, col2: number, newValue: number): void {
233+
this.history.push([row1, col1, row2, col2, newValue]);
234+
}
235+
236+
getValue(row: number, col: number): number {
237+
for (let i = this.history.length - 1; i >= 0; --i) {
238+
let [row1, col1, row2, col2, newValue] = this.history[i];
239+
if (row >= row1 && row <= row2 && col >= col1 && col <= col2) {
240+
return newValue;
241+
}
242+
}
243+
return this.grid[row][col];
244+
}
245+
}
246+
247+
/**
248+
* Your SubrectangleQueries object will be instantiated and called as such:
249+
* var obj = new SubrectangleQueries(rectangle)
250+
* obj.updateSubrectangle(row1,col1,row2,col2,newValue)
251+
* var param_2 = obj.getValue(row,col)
252+
*/
253+
```
254+
221255
### **...**
222256

223257
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class SubrectangleQueries {
2+
grid: number[][];
3+
history: number[][];
4+
constructor(rectangle: number[][]) {
5+
this.grid = rectangle;
6+
this.history = [];
7+
}
8+
9+
updateSubrectangle(row1: number, col1: number, row2: number, col2: number, newValue: number): void {
10+
this.history.push([row1, col1, row2, col2, newValue]);
11+
}
12+
13+
getValue(row: number, col: number): number {
14+
for (let i = this.history.length - 1; i >= 0; --i) {
15+
let [row1, col1, row2, col2, newValue] = this.history[i];
16+
if (row >= row1 && row <= row2 && col >= col1 && col <= col2) {
17+
return newValue;
18+
}
19+
}
20+
return this.grid[row][col];
21+
}
22+
}
23+
24+
/**
25+
* Your SubrectangleQueries object will be instantiated and called as such:
26+
* var obj = new SubrectangleQueries(rectangle)
27+
* obj.updateSubrectangle(row1,col1,row2,col2,newValue)
28+
* var param_2 = obj.getValue(row,col)
29+
*/

0 commit comments

Comments
 (0)