Skip to content

Commit

Permalink
programmers/고득점kit/level3/greedy/speed_camera(단속카메라)
Browse files Browse the repository at this point in the history
  • Loading branch information
bong6981 committed Mar 4, 2022
1 parent 27d8d51 commit 6c99f08
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
24 changes: 24 additions & 0 deletions programmers/java/src/level3/greedy/SpeedCamera.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package level3.greedy;

import java.util.*;
//https://programmers.co.kr/learn/courses/30/lessons/42884?language=java
public class SpeedCamera {

public static void main(String[] args) {
SpeedCamera sc = new SpeedCamera();
System.out.println(sc.solution(new int[][]{{-20, -15}, {-14, -5}, {-18, -13}, {-5, -3}}));
}

public int solution(int[][] routes) {
Arrays.sort(routes, Comparator.comparing(route -> route[1]));
int end = routes[0][1];
int cnt = 1;
for(int[] route : routes) {
if(route[0] > end) {
cnt++;
end = route[1];
}
}
return cnt;
}
}
17 changes: 17 additions & 0 deletions programmers/python/level3/greedy/speed_camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# https://programmers.co.kr/learn/courses/30/lessons/42884
def solution(routes):
routes.sort(key=lambda x : (x[0], x[1]))
cnt = 1
end = routes[0][1]
for route in routes:
print(f'route : {route}')
if route[0] > end:
cnt += 1
end = route[1]
elif route[1] < end :
end = route[1]
print(f'cnt = {cnt}')
print(f'end = {end}')
return cnt

print(solution([[-20,-15], [-14,-5], [-18,-13], [-5,-3]]))

0 comments on commit 6c99f08

Please sign in to comment.