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
}
相关推荐
F_D_Z35 分钟前
最长连续序列(Longest Consecutive Sequence)
数据结构·算法·leetcode
flashlight_hi2 小时前
LeetCode 分类刷题:199. 二叉树的右视图
javascript·算法·leetcode
LYFlied2 小时前
【每日算法】LeetCode 46. 全排列
前端·算法·leetcode·面试·职场和发展
小信啊啊2 小时前
Go语言数组与切片的区别
开发语言·后端·golang
云霄IT3 小时前
docker使用教程之部署第一个go项目
docker·容器·golang
LYFlied3 小时前
【每日算法】131. 分割回文串
前端·数据结构·算法·leetcode·面试·职场和发展
长安er3 小时前
LeetCode 300/152/416/32 动态规划进阶题型总结(最长递增子序列→最长有效括号)
数据结构·算法·leetcode·动态规划·剪枝
季远迩4 小时前
LeetCode 热题 100 Python3易懂题解(更新中)
算法·leetcode·哈希算法
Tony Bai4 小时前
Go 1.26 新特性前瞻:从 Green Tea GC 到语法糖 new(expr),性能与体验的双重进化
开发语言·后端·golang
Dream it possible!4 小时前
LeetCode 面试经典 150_回溯_组合(99_77_C++_中等)
c++·leetcode·面试·回溯