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
}
相关推荐
Yeats_Liao43 分钟前
Go Web 编程快速入门 18 - 附录B:查询与扫描
开发语言·前端·后端·golang
小八四爱吃甜食1 小时前
【R语言】构建GO、KEGG相关不同物种的R包
开发语言·golang·r语言
Kuo-Teng12 小时前
Leetcode438. 找到字符串中所有字母异位词
java·算法·leetcode
赵文宇(温玉)12 小时前
构建内网离线的“github.com“,完美解决内网Go开发依赖
开发语言·golang·github
墨染点香13 小时前
LeetCode 刷题【138. 随机链表的复制】
算法·leetcode·链表
草明14 小时前
Go 的 IO 多路复用
开发语言·后端·golang
Shinom1ya_19 小时前
算法 day 41
数据结构·算法·leetcode
一匹电信狗20 小时前
【C++】红黑树详解(2w字详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
仰泳的熊猫1 天前
LeetCode:72. 超级次方
数据结构·c++·算法·leetcode
_dindong1 天前
牛客101:递归/回溯
数据结构·c++·笔记·学习·算法·leetcode·深度优先