일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- FIT XR
- Daily Challenge
- 잡생각
- 30분
- 쓰릴오브파이트
- 월간
- 영어원서읽기
- 파비최
- Problem Solving
- 개발자
- 화상영어
- 괜찮음
- 3줄정리
- 운동
- 스탭퍼
- 사이드
- realclass
- English
- 10분
- 뭐든
- 미드시청
- 프로젝트
- 매일
- 읽기
- leetcode
- Writing
- 만화도
- 리얼 클래스
- 링피트
- 영어공부
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/bag-of-tokens
Bag of Tokens - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
탐욕법으로 풀면 됨.
class Solution:
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
tokens.sort()
deq = collections.deque(tokens)
ans, bns = 0, 0
while deq and (power >= deq[0] or bns):
while deq and power >= deq[0]:
power -= deq.popleft()
bns += 1
ans = max(ans, bns)
if deq and bns:
power += deq.pop()
bns -= 1
return ans
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Today's Challenge (2) | 2022.09.14 |
---|---|
Today's Challenge (0) | 2022.09.13 |
Today's Challenge (0) | 2022.09.11 |
Today's Challenge (0) | 2022.09.10 |
Today's Challenge (0) | 2022.09.09 |
Comments