cjql / algorithm

https://cjql.github.io/algorithm/
1 stars 1 forks source link

LeetCode_Dynamic: stone-game-ii #9

Open cjql opened 4 years ago

cjql commented 4 years ago

https://leetcode.com/problems/stone-game-ii/submissions/

C++

Java

Python

class Solution:
    def stoneGameII(self, piles: List[int]) -> int:
        N = len(piles)
        for i in range(N - 2, -1, -1):
            piles[i] += piles[i + 1]
        from functools import lru_cache
        @lru_cache(None)
        def dp(i, m):
            if i + 2 * m >= N: return piles[i]
            return piles[i] - min(dp(i + x, max(m, x)) for x in range(1, 2 * m + 1))
        return dp(0, 1)

C

C

JavaScript

Ruby

Swift

Go

Scala

Kotlin

Rust

PHP