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

김준영·2023년 3월 11일
1

코딩테스트

목록 보기
8/22
import java.util.HashMap;
import java.util.Map;
class Solution {
    public int solution(int[][] lines) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int i=0; i<lines.length; i++) {
            int min = Math.min(lines[i][0], lines[i][1]);
            int max = Math.max(lines[i][0], lines[i][1]);

            for (int j=min; j<max; j++) {
                map.put(j, map.getOrDefault(j, 0) + 1);
            }
        }

        int answer = 0;

        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() >= 2) {
                answer++;
            }
        }
        return answer;
    }
}
profile
ㅎㅎ

0개의 댓글