ABOUT ME

💻

  • [Java] 카펫
    Algorithm/프로그래머스 2023. 4. 4. 11:24

    1. 문제 📚

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

    2. 입출력 예 📋

    3. 알고리즘 ✅

    노란색의 개수에 따라서 갈색의 개수가 달라지기때문에 노란색의 개수가 우선이라고 생각했다! 

    조건에서 가로의 개수가 더 길다고 나와있었으므로 조건절에 넣어서 노란색의 격자를 구해줬다.

    1. 노란색 가로 세로 구하기 

    2. 갈색 가로 세로는 각각 노란색가로 + 2 노란색세로 +2 이다.

    3. 갈색 가로 * 갈색 세로가 파라미터로 주어진 노란색 + 갈색 과 같은지 체크 (중요)

    4. 소스코드 💻

    import java.util.*;
    class Solution {
        static int width, height;
        
        public int[] solution(int brown, int yellow) {
            int[] answer = new int[2];
            
            // 1. 노란 격자 부터 구하기
            int yellowWidth = yellow;
            int yellowHeight = 1;
            for (int i = 1; i <= yellow; i++) {
                if (yellow % i != 0) continue;
                
                int tempHeight = i;
                int tempWidth = yellow / tempHeight;
    
                // 카펫의 가로 길이는 세로 길이와 같거나, 세로 길이보다 깁니다.
                if (tempWidth >= tempHeight) {
                    if ((tempWidth + 2) * (tempHeight + 2) != (brown + yellow)) continue;
                    yellowWidth = tempWidth;
                    yellowHeight = tempHeight;
                } 
            }
            
            answer[0] = yellowWidth + 2;
            answer[1] = yellowHeight + 2;
            
            return answer;
        }
    }

    굿

    'Algorithm > 프로그래머스' 카테고리의 다른 글

    [Java] H-index  (0) 2023.04.11
    [Python] 주차 요금 계산  (0) 2022.11.04
    [Python] 단어 변환  (0) 2022.10.20
    [Python] 입국심사  (0) 2021.06.03
    [Python] 로또의 최고 순위와 최저 순위  (0) 2021.05.05

    댓글

Designed by Tistory.