코딩테스트 풀이/JAVA
[LeetCode/Medium] 1476. Subrectangle Queries
무지맘
2022. 8. 16. 21:31
목표: SubrectangleQueries 클래스 내부 구현
- 생성자
- updateSubrectangle 메서드
- getValue 메서드
1. Input
1) 생성자: 2차원 배열
2) updateSubrectangle: int 변수 row1, col1, row2, col2, newValue
- 왼쪽 위 좌표 (row1, col1)부터 오른쪽 아래 좌표 (row2, col2) 까지 newValue로 값 변경
3) getValue: int 변수 row, col
2. Output
1) updateSubrectangle: void
2) getValue: 해당 좌표의 int 값
3. Constraint
1) 1 <= rows, cols <= 100
- rows == rectangle.length
- cols == rectangle[i].length
2) 0 <= row1 <= row2 < rows
3) 0 <= col1 <= col2 < cols
4) 1 <= newValue, rectangle[i][j] <= 10^9
5) 0 <= row < rows
6) 0 <= col < cols
4. Example
SubrectangleQueries sq = new SubrectangleQueries([[1,2,1],[4,3,4]]);
// sq의 초기화된 2*3 배열
// 1 2 1
// 4 3 4
sq.getValue(0, 2); // 1
sq.updateSubrectangle(0, 1, 2, 2, 7);
// 업데이트 된 sq의 배열
// 5 7 7
// 5 7 7
sq.getValue(0, 2); // 7
5. Code
class SubrectangleQueries {
int[][] rec;
public SubrectangleQueries(int[][] rectangle) {
rec = rectangle;
}
public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
for(int i=row1 ; i<=row2 ; i++){
for(int j=col1 ; j<=col2 ; j++){
rec[i][j] = newValue;
}
}
}
public int getValue(int row, int col) {
return rec[row][col];
}
}