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
}
相关推荐
愣头不青29 分钟前
96.不同的二叉搜索树
数据结构·算法·leetcode
golang学习记2 小时前
Go 实时批处理:让数据库少挨点打 [特殊字符]
开发语言·数据库·golang
alphaTao3 小时前
LeetCode 每日一题 2026/3/23-2026/3/29
服务器·windows·leetcode
不光头强3 小时前
力扣78子集题解
算法·leetcode·深度优先
Magic--3 小时前
经典概率题:飞机座位分配问题(LeetCode 1227)超详细解析
算法·leetcode·职场和发展
hutengyi4 小时前
go测试问题记录
开发语言·后端·golang
KevinCyao4 小时前
Go短信营销接口示例代码:Golang高并发调用营销短信接口的实现方案与代码分享
android·前端·网络·golang·前端框架
We་ct5 小时前
LeetCode 4. 寻找两个正序数组的中位数:二分优化思路详解
前端·数据结构·算法·leetcode·typescript·二分
精神小伙就是猛5 小时前
使用go-zero快速搭建一个微服务(一)
开发语言·后端·微服务·golang
不会聊天真君6475 小时前
基础语法·下(golang笔记第三期)
开发语言·笔记·golang