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
}
相关推荐
-海绵东东-27 分钟前
哈希表······················
算法·leetcode·散列表
菜鸡儿齐2 小时前
leetcode-全排列
算法·leetcode·深度优先
不想看见4042 小时前
Maximal Square 基本动态规划:二维--力扣101算法题解笔记
算法·leetcode·动态规划
夏乌_Wx2 小时前
LeetCode 160. 相交链表 | 三种解法吃透核心逻辑(哈希表 + 双指针 + 长度对齐)
leetcode·链表·哈希表
Hag_202 小时前
LeetCode Hot100 53.最大子数组和
数据结构·算法·leetcode
pursuit_csdn2 小时前
LeetCode 1461. Check If a String Contains All Binary Codes of Size K
算法·leetcode·职场和发展
Crazy________3 小时前
力扣113个mysql简单题解析(包含plus题目)
mysql·算法·leetcode·职场和发展
初次攀爬者4 小时前
力扣解题-无重复字符的最长子串
后端·算法·leetcode
圣保罗的大教堂4 小时前
leetcode 1461. 检查一个字符串是否包含所有长度为 K 的二进制子串 中等
leetcode
Trouvaille ~5 小时前
【动态规划篇】专题(二):路径问题——在网格图中的决策艺术
c++·算法·leetcode·青少年编程·动态规划