알고리즘

99클럽 코테 스터디 26일차 TIL Subrectangle Queries

ernest45 2024. 6. 14. 19:01

https://leetcode.com/problems/subrectangle-queries/description/

 

 

 

 

 

 

 

1. 문제 및 접근

 

 

 

1476. Subrectangle Queries

 

총 3가지 메서드

 

1. 들어온대로 배열 만들기

2. row,col을 들어오면 그 좌표 값 반환

3. row1 co1 부터 row2 col2까지의 수를 들어온 newValue 초기화

 

Constraints:

  • There will be at most 500 operations considering both methods: updateSubrectangle and getValue.
  • 1 <= rows, cols <= 100
  • rows == rectangle.length
  • cols == rectangle[i].length
  • 0 <= row1 <= row2 < rows
  • 0 <= col1 <= col2 < cols
  • 1 <= newValue, rectangle[i][j] <= 10^9
  • 0 <= row < rows
  • 0 <= col < cols

 

 

 

2. 풀이

 

public class subrectangleQueries {

    //1476. Subrectangle Queries

    //총 3가지 메서드,
    // 1. 들어온대로 배열 만들기
    // 2. row,col을 들어오면 그 좌표 값 반환
    // 3. row1 co1 부터 row2 col2까지의 수를 들어온 newvalue로 초기화

    private int[][] rectangle1;

    public static void main(String[] args) {
        int[][] arr = new int[][]
                       {{1, 2, 1},
                        {4, 3, 4},
                        {3, 2, 1},
                        {1, 1, 1}};
        subrectangleQueries main = new subrectangleQueries(arr);
        main.getValue(0, 2);
        main.updateSubrectangle(0,0,3,2,5);
        main.getValue(0, 2);
        main.getValue(3, 1);
        main.updateSubrectangle(3, 0, 3, 2, 10);
        main.getValue(3, 1);
        main.getValue(0, 2);




    }

    public subrectangleQueries(int[][] rectangle) {

       this.rectangle1 = 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++) {
                rectangle1[i][j] = newValue;
            }
        }

    }

    public int getValue(int row, int col) {
        System.out.println(rectangle1[row][col]);

        return rectangle1[row][col];

    }
}

 

전체풀이

 

 

 

 

2-1 생성자

 

 

 

 

전역변수인 Instance변수 선언

 

 

 

생성자 호출 시 초기화

 

 

 

 

 

 

2-2 UpdateSubRectangle

 

 

 

들어온 row1 부터, row2까지,

들어온 col1 부터, col2까지  newValue로 바꿔주면 된다.

 

 

 

 

현재 배열은

왼에서 오른쪽으로

 

 

이렇게 있고 예를 들어

 

 

updateSubrectangle(0,0,3,2,5)을 호출하게 되면

 

옆 배열로 업데이트 됨

 

 

 

 

 

 

2-3.  getValue

 

 

 

 

 

들어온 row와 col 값의 좌표의 값을 return 해주면 끝이다..

 

 

 

 

 

 

 

아니 이게 어떻게 미들러 ?

 

 

 

 

 

 

3. 마무리

 

 

간단한 구현 문제이다.

 

이게 어떻게 미들러인지 모르겠지만, 내 자존감이 올라갔으니 너는 미들러가 확실하다고 생각할꺼다.

 

요새 구현할 일이 잘 없어서 spring에 대한 감각이 떨어 지는 것 같다..

 

다시 다른 강의들 끊고, 아니면 하나의 주제를 정해서 혼자라도 돌아가는 프로그램을 만들어보자

 

웹으로 만들어야할텐데..

무신사도 보니 뭐 웹은 MAU 처참해서 닫았던데

 앱을 새로 공부해서 만들어봐야하나 ㅠㅠㅠㅠ