99클럽 코테 스터디 26일차 TIL: 배열

이주희·2024년 6월 14일
0

99클럽 코테 스터디

목록 보기
17/20
post-thumbnail

배열을 활용한 알고리즘 문제풀이

오늘 푼 문제: 1476. Subrectangle Queries

입출력

  • 입력: 매서드 명을 저장한 String[]과 해당 매서드의 인자를 담은 int[][]가 주어집니다.
  • 출력: 각 매서드의 return값을 반환합니다.

예제 코드

class SubrectangleQueries {
    int[][] array;

    public SubrectangleQueries(int[][] rectangle) {
        this.array = rectangle;
    }
    
    public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
        for (int x = row1; x <= row2; x++) {
            for (int y = col1; y <= col2; y++) {
                array[x][y] = newValue;
            }
        }
    }
    
    public int getValue(int row, int col) {
        return array[row][col];
    }
}

/**
 * Your SubrectangleQueries object will be instantiated and called as such:
 * SubrectangleQueries obj = new SubrectangleQueries(rectangle);
 * obj.updateSubrectangle(row1,col1,row2,col2,newValue);
 * int param_2 = obj.getValue(row,col);
 */
  • 주어진 SubrectagleQueries의 필드로 이차원 배열 array를 선언합니다.
  • update의 경우 이중 포문을 돌며 값을 수정합니다.

회고

  • 오늘 문제는 가벼웠을지도...?
profile
공릉동 감자

0개의 댓글

관련 채용 정보