본문 바로가기

Algorithm/Programmers

탐욕법 06 - 단속카메라, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정, Leetcode, 릿코드, 코딩테스트, Tech interview, search, greedy

// Process

// 1. Input routes (in, out) array

// 2. Make routeList and sort using out position

// 3. Iterate all

//  3.1. Check if i's in position is before current picked out position

//   3.1.1. If not -> change out position to i's out position, and add cameraCount

// 4. Return cameraCount

 

import java.util.*;



class Solution {

    public int solution(int[][] routes) {

        int cameraCount = 0;

        

        // 2.

        List<Route> routesList = new ArrayList<>();

        for (int i = 0; i < routes.length; ++i)

            routesList.add(new Route(routes[i][0], routes[i][1]));

        Collections.sort(routesList);



        // 3.

        int out = -30000;

        for (int i = 0; i < routesList.size(); ++i) {

            if (routesList.get(i).in > out) {

                ++cameraCount;

                out = routesList.get(i).out;

            }

        }



        return cameraCount;

    }

}



class Route implements Comparable<Route> {

    int in;

    int out;



    public Route(int in, int out) {

        this.in = in;

        this.out = out;

    }



    @Override

    public int compareTo(Route cmp) {

        if (this.out > cmp.out)

            return 1;

        else

            return -1;

    }

}

 

2021.03.15 - [Algorithm/Programmers] - 완전탐색 03 - 카펫, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이과정, Leetcode, 릿코드, 코딩테스트, Tech interview, Heap, 힙, 정렬, sort, search

 

완전탐색 03 - 카펫, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀이

# Process # 1. Input brown, yellow numbers # 2. Check the number of edges (brown) #  2.1. If so -> Check the number of inner sector (yellow) #   2.1.1. If so -> appe..

itdar.tistory.com

2019.10.06 - [Algorithm/Leet Code] - LeetCode #1137 N-thTribonacciNumber. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

 

LeetCode #1137 N-thTribonacciNumber. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예

LeetCode #1137 N-thTribonacciNumber. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩..

itdar.tistory.com

2019.08.27 - [Programming] - 개발자 선배들에게서 배운 것들. Things I Learnt from a Senior Software Engineer. 코딩 잘하는 방법, how to code well, 소프트웨어,프로그래머,programmer

 

개발자 선배들에게서 배운 것들. Things I Learnt from a Senior Software Engineer. 코딩 잘하는 방법, how to cod

 

itdar.tistory.com