-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstoneGameV.py
More file actions
30 lines (26 loc) · 899 Bytes
/
stoneGameV.py
File metadata and controls
30 lines (26 loc) · 899 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/stone-game-v/
# Author: Miao Zhang
# Date: 2021-05-16
class Solution:
def stoneGameV(self, stoneValue: List[int]) -> int:
n = len(stoneValue)
sums = [0] * (n + 1)
for i in range(n):
sums[i + 1] = sums[i] + stoneValue[i]
@lru_cache(None)
def dp(l, r):
if l == r: return 0
res = 0
for k in range(l, r):
suml = sums[k + 1] - sums[l]
sumr = sums[r + 1] - sums[k + 1]
if suml < sumr:
res = max(res, suml + dp(l, k))
elif sumr < suml:
res = max(res, sumr + dp(k + 1, r))
else:
res = max(res, suml + max(dp(l, k), dp(k + 1, r)))
return res
return dp(0, n - 1)