-
12. 그리디 알고리즘(탐욕법)코딩테스트 2024. 3. 22. 16:35반응형
개미 군단 - https://school.programmers.co.kr/tryouts/85949/challenges
def solution(hp): x1, r = hp//5, hp%5 x2, r = r//3, r%3 x3 = r//1 return x1+x2+x3
체육복 - https://school.programmers.co.kr/tryouts/85950/challenges
def solution(n, lost, reserve): lost, reserve = set(lost), set(reserve) real_lost = lost - reserve real_reserve = reserve-lost cur_sum = n - len(real_lost) for cur_num in real_lost: if cur_num - 1 in real_reserve: cur_sum += 1 real_reserve.remove(cur_num-1) elif cur_num + 1 in real_reserve: cur_sum += 1 real_reserve.remove(cur_num+1) return cur_sum
단속카메라 - https://school.programmers.co.kr/tryouts/85951/challenges
def solution(routes): routes.sort(key=lambda x:(x[1])) camera = routes[0][1] ans = 1 for route in routes: if route[0] > camera: ans += 1 camera = route[1] return ans
반응형'코딩테스트' 카테고리의 다른 글
13. 다이내믹 프로그래밍(DP) (4) 2024.03.22 11. 정렬 (1) 2024.03.22 10. 그래프 - 너비 우선 탐색(BFS, Breadth First Search) (3) 2024.03.22 9. 그래프 - 깊이 우선 탐색(DFS, Depth First Search) (2) 2024.03.22 8. 힙(heap) (4) 2024.03.21