코딩공작소

[프로그래머스] 요격 시스템 본문

알고리즘/그리디&완탐

[프로그래머스] 요격 시스템

안잡아모찌 2023. 11. 16. 13:02

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 :: 시간 복잡도에 대해서 고려를 해보고 알고리즘을 설계하면 더 좋을 것 같다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
 
class Solution {
 
    public int solution(int[][] targets) {
        int answer = 1;
        
        Arrays.sort(targets, ((x,y) -> x[0- y[0]));
        
        int preStart = targets[0][0];
        int preEnd = targets[0][1];
        
        for(int[]target : targets){
            int curStart = target[0];
            int curEnd = target[1];
            
            if(preStart <= curStart && curStart < preEnd){
                preStart = curStart;
                preEnd = Math.min(preEnd, curEnd);
                //System.out.println("@@@ " + preStart + "  " + preEnd + "@@@");
            }else{
                preStart = curStart;
                preEnd = curEnd;
                answer++;
                //System.out.println(">>> " + curStart + "  " + curEnd + ">>>");
            }
            
        }
        
        return answer;
    }
}
cs

 

 

 

'알고리즘 > 그리디&완탐' 카테고리의 다른 글

[프로그래머스]타켓넘버  (0) 2019.10.25
[SWEA]벌꿀채취  (0) 2019.10.12
[SWEA]디저트카페(2)  (0) 2019.10.10
[백준]사다리조작(second)  (0) 2019.09.05
[백준]일곱난쟁이  (0) 2019.08.20