[Programmers / Level 2]42842. 카펫 (Java)

이하얀·2025년 1월 5일
0

🕊️ 프로그래머스

목록 보기
86/97

💡 Info




제한 사항




입출력 예시




문제 이해


  • brown과 yellow 값을 통해 카펫의 가로와 세로 길이를 구하는 문제


알고리즘


풀이 시간 : 1시간 12분

  • 총 면적 계산
    • 카펫의 전체 면적은 brown + yellow
    • width * height 값 구하기
  • width, height
    • width는 3부터 시작
    • width가 totalArea러 나누어떨어지면 -> height 계산
  • 내부
    • width - 2, height - 2 -> yellow 영역 만들기가 되는지 확인
class Solution {
    public int[] solution(int brown, int yellow) {
        int totalArea = brown + yellow;
        
        for (int width = 3; width <= totalArea / 3; width++) {
            if (totalArea % width == 0) {
                int height = totalArea / width;
                
                if ((width - 2) * (height - 2) == yellow) {
                    return new int[] {Math.max(width, height), Math.min(width, height)};
                }
            }
        }
        return null;
    }
}


결과

profile
언젠가 내 코드로 세상에 기여할 수 있도록, BE&Data Science 개발 기록 노트☘️

0개의 댓글

관련 채용 정보