Golang | Leetcode Golang题解之第486题预测赢家

题目:

题解:

Go 复制代码
func PredictTheWinner(nums []int) bool {
    return total(nums, 0, len(nums) - 1, 1) >= 0
}

func total(nums []int, start, end int, turn int) int {
    if start == end {
        return nums[start] * turn
    }
    scoreStart := nums[start] * turn + total(nums, start + 1, end, -turn)
    scoreEnd := nums[end] * turn + total(nums, start, end - 1, -turn)
    return max(scoreStart * turn, scoreEnd * turn) * turn
}

func max(x, y int) int {
    if x > y {
        return x
    }
    return y
}
相关推荐
xlp666hub11 小时前
Leetcode第五题:用C++解决盛最多水的容器问题
linux·c++·leetcode
xlp666hub1 天前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
xlp666hub1 天前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
xlp666hub2 天前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
花酒锄作田12 天前
Gin 框架中的规范响应格式设计与实现
golang·gin
琢磨先生David12 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
超级大福宝12 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
Charlie_lll12 天前
力扣解题-88. 合并两个有序数组
后端·算法·leetcode
菜鸡儿齐12 天前
leetcode-最小栈
java·算法·leetcode
Frostnova丶12 天前
LeetCode 1356. 根据数字二进制下1的数目排序
数据结构·算法·leetcode