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
}
相关推荐
名字还没想好☜12 小时前
Go 的 time.After 在 select 循环里内存泄漏:定时器堆积原理与 timer.Reset 正确姿势
java·数据库·golang·go·goroutine
alphaTao13 小时前
LeetCode 每日一题 2026/7/27-2026/8/2
python·算法·leetcode
ttwuai14 小时前
GoFrame 后台日志清空失败:无 WHERE 删除为什么被拦住
前端·golang
进击的程序猿~15 小时前
Go 并发底层原理面试学习指南
开发语言·面试·golang
Hi李耶20 小时前
【LeetCode】9-回文数
算法·leetcode·职场和发展
geovindu1 天前
go:loghelper
开发语言·后端·golang
海绵天哥1 天前
LeetCode Hot 100 | 链表(下)· 分组翻转与设计(C++ 题解)
c++·leetcode·链表
会编程的土豆1 天前
GoWeb 处理请求详解:请求行、请求头、请求参数与给客户端响应
开发语言·http·golang
tkevinjd2 天前
力扣131-分割回文串
算法·leetcode·深度优先
zander2582 天前
LeetCode 78. 子集
算法·leetcode·深度优先