본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] 겹치는 선분의 길이

by 현장 2025. 2. 21.

-Code

class Solution {
    public int solution(int[][] lines) {
        int answer = 0;
        int[] line = new int[201];

        for (int[] el : lines) {
            for (int i = el[0]; i < el[1] ; i++) {
                line[i + 100] += 1;
                if (line[i + 100] == 2) {
                    answer++;
                }
            }
        }

        return answer;
    }
}

처음에는 라인을 계산 다하고 한번더 확인하는 식으로 해서 맞추었지만 더 속도를 올릴 방법이 떠올라서 다시 작성해서 통과했습니다.