알고리즘

프로그래머스 정사각형으로 만들기 (JAVA)

박카스마시며코딩 2023. 5. 26. 22:03

https://school.programmers.co.kr/learn/courses/30/lessons/181830

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

저는 구현을 통해 문제를 해결했습니다.

먼저 긴변을 찾고 그 길이보다 길다면 다 0을 넣도록 구현했습니다.

 

class Solution {
    private static final int EMPTY = 0;
    public int[][] solution(int[][] arr) {
        int size = arr.length;
        for(int i = 0 ; i < arr.length ; i++){
            size = Math.max(size,arr[i].length);
        }
        int[][] answer = new int[size][size];
        for(int i = 0 ; i < size ; i++){
            for(int j = 0 ; j < size ; j++){
                if(arr.length <= i){
                    answer[i][j] = EMPTY;
                    continue;
                }
                if(arr[i].length <= j){
                    answer[i][j] = EMPTY;
                    continue;
                }
                answer[i][j] = arr[i][j];
            }
        }
        
        return answer;
    }
}