일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Daily Challenge
- 영어공부
- 스탭퍼
- Writing
- 화상영어
- 영어원서읽기
- realclass
- 사이드
- Problem Solving
- 링피트
- 뭐든
- 괜찮음
- FIT XR
- 읽기
- 10분
- 미드시청
- leetcode
- 잡생각
- 파비최
- 쓰릴오브파이트
- 3줄정리
- 만화도
- English
- 개발자
- 매일
- 리얼 클래스
- 월간
- 운동
- 30분
- 프로젝트
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.05.26 Today's Challenge 본문
https://leetcode.com/problems/stone-game-ii/
Stone Game II - LeetCode
Can you solve this real interview question? Stone Game II - Alice and Bob continue their games with piles of stones. There are a number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]. The objective of the
leetcode.com
근래에 본 DP 문제 중 제일 복잡했던 듯.
class Solution:
def stoneGameII(self, piles: List[int]) -> int:
n = len(piles)
dp = [[[-1] * (n+1) for i in range(n+1)] for p in range(0, 2)]
def f(p, i, m):
if i == n:
return 0
if dp[p][i][m] != -1:
return dp[p][i][m]
res = -1
if p == 1:
res = 1000000
s = 0
for x in range(1, min(2*m, n-i) + 1):
s += piles[i+x-1]
if p == 0:
res = max(res, s + f(1, i+x, max(m, x)))
else:
res = min(res, f(0, i+x, max(m, x)))
dp[p][i][m] = res
return res
return f(0, 0, 1)
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.05.28 Today's Challenge (0) | 2023.05.28 |
---|---|
2023.05.27 Today's Challenge (0) | 2023.05.27 |
2023.05.25 Today's Challenge (0) | 2023.05.25 |
2023.05.24 Today's Challenge (0) | 2023.05.24 |
2023.05.23 Today's Challenge (0) | 2023.05.23 |
Comments