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
}
相关推荐
Tisfy2 分钟前
LeetCode 1888.使二进制字符串字符交替的最少反转次数:前缀和O(1)
算法·leetcode·字符串·题解
滴滴答滴答答1 小时前
机考刷题之 9 LeetCode 503 下一个更大元素 II
算法·leetcode·职场和发展
啊哦呃咦唔鱼1 小时前
LeetCode hot100-15 三数之和
数据结构·算法·leetcode
_日拱一卒1 小时前
LeetCode(力扣):杨辉三角||
算法·leetcode·职场和发展
小红卒2 小时前
Go语言安全开发学习笔记1:Windows反向TCP反弹Shell 原理与代码
golang
Nontee2 小时前
Leetcode Top100答案和解释 -- Python版本(链表)
算法·leetcode·链表
章小幽4 小时前
LeetCode-35.搜索插入位置
数据结构·算法·leetcode
x_xbx4 小时前
LeetCode:111. 二叉树的最小深度
算法·leetcode·职场和发展
滴滴答滴答答5 小时前
机考刷题之 10 LeetCode 200 岛屿数量
算法·leetcode·职场和发展
lisus20076 小时前
GO并发统计文件大小
开发语言·后端·golang