[99클럽] 99클럽 코테 스터디 26일차 TIL - 배열 (Array)

2024. 6. 14. 19:47·코딩테스트 연습/99클럽
728x90

 

오늘의 문제

LeetCode - Shuffle the Array, Subrectangle Queries (출처 하단 기재)

비기너 문제를 풀 때 얼마 걸리지 않아 미들러 문제를 풀었는데, 미들러 문제도 쉬운 편이었다.

 

비기너

문제

Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

Return the array in the form [x1,y1,x2,y2,...,xn,yn].

 

제한사항

  • 1 <= n <= 500
  • nums.length == 2n
  • 1 <= nums[i] <= 10^3

입출력 예

nums n return
[2,5,1,3,4,7] 3 [2,3,5,4,1,7]
[1,2,3,4,4,3,2,1] 4 [1,4,2,3,3,2,4,1]
[1,1,2,2] 2 [1,2,1,2]

 

풀이

class Solution {
    public int[] shuffle(int[] nums, int n) {
        // 배열의 절반을 기준으로 다시 배열
        int[] answer = new int[nums.length];
        
        int x = 0;
        int y = n;
        int i = 0;
        
        while(x < n) {
            answer[i] = nums[x];
            answer[i+1] = nums[y];
            
            x++;
            y++;
            i += 2;
        }
        
        return answer;
    }
}

 

배열의 절반을 기준으로 왼쪽은 x, 오른쪽은 y로 구분하여 answer 배열에 x1, y1, ... 형식으로 저장하는 문제였다. 투 포인터를 사용하여 x와 y를 각각 구한 다음 배열에 차례대로 넣는 방식으로 구현하였다.

 

 

미들러

문제

Implement the class SubrectangleQueries which receives a rows x cols rectangle as a matrix of integers in the constructor and supports two methods:

1. updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)

  • Updates all values with newValue in the subrectangle whose upper left coordinate is (row1,col1) and bottom right coordinate is (row2,col2).

2. getValue(int row, int col)

  • Returns the current value of the coordinate (row,col) from the rectangle.

제한사항

  • 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

 

풀이

class SubrectangleQueries {
    
    int[][] rectangle;

    public SubrectangleQueries(int[][] rectangle) {
        this.rectangle = 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++)
                rectangle[i][j] = newValue;
        }
    }
    
    public int getValue(int row, int col) {
        return rectangle[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);
 */

 

rectangle 배열의 원소를 업데이트하는 updateSubrectangle 메소드와 원소의 값을 반환하는 getValue 메소드를 구현하는 문제였다. 클래스에 배열을 선언하고 해당 배열을 이용하여 메소드를 구현하는 간단한 문제였다.

 

 

비기너 문제 출처: LeetCode, https://leetcode.com/problems/shuffle-the-array/
미들러 문제 출처: LeetCode, https://leetcode.com/problems/subrectangle-queries/
728x90
저작자표시 비영리 변경금지 (새창열림)

'코딩테스트 연습 > 99클럽' 카테고리의 다른 글

[99클럽] 99클럽 코테 스터디 28일차 TIL - 배열 (Array)  (0) 2024.06.16
[99클럽] 99클럽 코테 스터디 27일차 TIL - 배열 (Array)  (1) 2024.06.15
[99클럽] 99클럽 코테 스터디 25일차 TIL - 그래프 (Graph)  (0) 2024.06.13
[99클럽] 99클럽 코테 스터디 24일차 TIL - 그래프 (Graph)  (0) 2024.06.12
[99클럽] 99클럽 코테 스터디 23일차 TIL - 이분탐색 (Binary Search)  (0) 2024.06.11
'코딩테스트 연습/99클럽' 카테고리의 다른 글
  • [99클럽] 99클럽 코테 스터디 28일차 TIL - 배열 (Array)
  • [99클럽] 99클럽 코테 스터디 27일차 TIL - 배열 (Array)
  • [99클럽] 99클럽 코테 스터디 25일차 TIL - 그래프 (Graph)
  • [99클럽] 99클럽 코테 스터디 24일차 TIL - 그래프 (Graph)
hxxzz
hxxzz
개발새발 안 되게 개발 노력 중
  • hxxzz
    개발새발
    hxxzz
  • 전체
    오늘
    어제
    • 분류 전체보기 (104)
      • Java (3)
      • Back-End (9)
        • Spring Boot (7)
        • DevOps (1)
        • Redis (1)
      • Computer Scrience (4)
        • Data Structrue (4)
        • Algorithm (0)
      • SQLD (3)
      • 코딩테스트 연습 (85)
        • Programmers (30)
        • 백준 (15)
        • etc. (0)
        • 99클럽 (40)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    개발자 취업
    LeetCode
    redission
    BFS
    N+1 문제
    dfs
    SQLD
    java
    프로그래머스
    SpringBoot
    코딩테스트 준비
    jpa
    자료구조
    스택
    til
    99클럽
    Stack
    백준
    Spring Boot
    SQL
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
hxxzz
[99클럽] 99클럽 코테 스터디 26일차 TIL - 배열 (Array)
상단으로

티스토리툴바