프로그래머스 - 삼각형의 완성 조건(2)

남궁진 (jinvicky)·2026년 4월 9일

Problem


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

Solution


👉 정수 범위 개수 구하기
✅ 기본형: L ≤ x ≤ R → 개수 = R - L + 1
✅ 너 문제처럼 (부등호 strict): L < x < R → 개수 = (R - 1) - (L + 1) + 1
👉 정리하면: 개수 = R - L - 1

🎯 한 줄 요약
👉

  • ≤ 포함이면 → +1
  • < (미포함) → -1

기본 공식

class Solution {
    public int solution(int[] sides) {
        int a = sides[0];
        int b = sides[1];
        
        return (a + b) - Math.abs(a - b) - 1;
    }
}

Code


class Solution {
    public int solution(int[] sides) {
        int min = Math.min(sides[0], sides[1]); // 나머지 한 변이 제일 길 때 최소는 min 초과여야함.
        int max = Math.max(sides[0], sides[1]);
        
        int total = sides[0] + sides[1] - 1; // x가 가질 수 있는 최대값
        // 나머지 한변이 제일 긺
        int caseB = max - min; // x가 될 수 없는 최소 구간 (버려야 할 개수)
        
        return total - caseB; // 가능한 개수 = 전체 개수 - 불가능한 개수
    }
    
}
profile
문제를 차근차근 하나씩 해결하려고 합니다:)

0개의 댓글