문제
https://school.programmers.co.kr/learn/courses/30/lessons/181188
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
우선 문제 예제를 살펴보자
targets
[[4,5],[4,8],[10,14],[11,13],[5,12],[3,7],[1,4]]
result
3
1. targets 는 입력 배열이다.
입력 배열을 살펴보면 무작위로 들어온 것을 확인할 수 있다.
그림 처럼 만들어 주기 위해 target[i][1]에 대해 sort를 해준다.
2. 처음 만나는 targets[i][1]에 대해 targetMaxValue로 갱신을 해준다.
이때 target[i][0]이 targetMaxValue 보다 작거나 같으면 미사일이 범위 내에 포함된다는 것으로 continue를 해주면 되고 이외 경우에만 targetMaxValue를 갱신하면 된다.
코드
import java.util.*;
class Solution {
public int solution(int[][] targets) {
int answer = 0;
Arrays.sort(targets, (o1, o2) -> o1[1] - o2[1]);
int targetMaxValue = -1;
for(int i=0;i<targets.length;i++){
if(targetMaxValue >= targets[i][0]) continue;
answer++;
targetMaxValue = targets[i][1] -1;
// System.out.println(targets[i][1]);
}
return answer;
}
}