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
}
相关推荐
We་ct6 小时前
LeetCode 148. 排序链表:归并排序详解
前端·数据结构·算法·leetcode·链表·typescript·排序算法
x_xbx8 小时前
LeetCode:2. 两数相加
算法·leetcode·职场和发展
_日拱一卒9 小时前
LeetCode:最长连续序列
算法·leetcode·职场和发展
重生之后端学习9 小时前
287. 寻找重复数
数据结构·算法·leetcode·深度优先·图论
fy1216310 小时前
GO 快速升级Go版本
开发语言·redis·golang
实心儿儿11 小时前
算法7:两个数组的交集
算法·leetcode·职场和发展
sheeta199811 小时前
LeetCode 每日一题笔记 日期:2025.03.19 题目:3212.统计X和Y频数相等的子矩阵数量
笔记·leetcode·矩阵
Storynone11 小时前
【Day28】LeetCode:509. 斐波那契数,70. 爬楼梯,746. 使用最小花费爬楼梯
python·算法·leetcode
博风13 小时前
算法:双指针解:盛最多水的容器
算法·leetcode
童话ing13 小时前
【Golang】Golang Map数据结构底层原理
数据结构·golang·哈希算法