-
[연습 문제] 숫자 게임코딩 테스트/프로그래머스 2023. 4. 5. 18:55
문제 설명
https://school.programmers.co.kr/learn/courses/30/lessons/12987
문제 풀이
- b가 최대 승점을 얻었을 때의 경기를 출력하는게 아닌 승점만 출력하는 문제
- a의 순서를 바꿔도 상관 x (b가 그것에 맞춰 경기하는 방식이 되기 때문)
- a와 b를 내림차순으로 정렬
- idx 를 0 으로 잡은 후, a[idx]와 b[0]의 최대 값을 계속 비교
- b[0]가 a[idx]보다 크다면 a[idx]보다 작은 값들은 모두 b[0]보다 큰 값이므로 이후 비교 의미x
- b[0]>a[idx] 라면, b[0]는 b에서 pop 해주고 answer+=1 해줌.
- b[0]<=a[idx]라면, b[0]는 그 다음 a[idx+1]과 비교 해야함.
소스 코드
from collections import deque def solution(a, b): answer = 0 n = len(a) a.sort(reverse=True) b.sort(reverse=True) b=deque(b) idx = 0 while(b): cur = b.popleft() if(idx==n): break if a[idx]<cur: answer+=1 idx+=1 else: idx+=1 b.appendleft(cur) return answer print(solution([5,1,2,7],[2,2,6,8]))
'코딩 테스트 > 프로그래머스' 카테고리의 다른 글
[2019카카오] 불량 사용자 (0) 2023.04.07 [해시] 베스트 앨범 (0) 2023.04.06 [우선순위 큐] 야근 지수 *** (0) 2023.04.03 [그리디] 단속 카메라 (0) 2023.04.03 [동적 계획법] 등굣길 (0) 2023.03.31