1. 문제
코딩테스트 연습 - 요격 시스템 | 프로그래머스 스쿨 (programmers.co.kr)
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
2. 설명
간단하게 각 미사일의 끝 점을 기준으로 정렬을 하고, 미사일의 가장 끝에서 요격을 하면 된다.
요격한 지점에 포함되는 미사일들은 요격된 것으로 처리하고, 요격 횟수를 올리지 않는다.
3. 코드
public class Roket {
public static void main(String[] args) {
int[][] test = {{4,5},{4,8},{10,14},{11,13},{5,12},{3,7},{1,4}};
System.out.println(solution(test));
}
public static int solution(int[][] targets) {
Arrays.sort(targets, Comparator.comparingInt(a -> a[1]));
int endPoint = targets[0][1] - 1;
int result = 1;
for (int i = 1; i < targets.length; i++) {
if(targets[i][0] <= endPoint) continue;
result++;
endPoint = targets[i][1] - 1;
}
return result;
}
}
반응형
'Algorithm > - Coding Test' 카테고리의 다른 글
괄호 짝 맞추기 (0) | 2024.05.08 |
---|---|
석유 시추 (0) | 2024.04.24 |
Donut (0) | 2024.02.01 |