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
}
相关推荐
YoungHong19927 小时前
面试经典150题[072]:从前序与中序遍历序列构造二叉树(LeetCode 105)
leetcode·面试·职场和发展
im_AMBER7 小时前
Leetcode 78 识别数组中的最大异常值 | 镜像对之间最小绝对距离
笔记·学习·算法·leetcode
LYFlied8 小时前
【每日算法】LeetCode 25. K 个一组翻转链表
算法·leetcode·链表
LYFlied11 小时前
【每日算法】LeetCode 19. 删除链表的倒数第 N 个结点
算法·leetcode·链表
zfj32112 小时前
vscode是js开发的,为什么能支持golang java等各种语言开发
javascript·vscode·golang
努力学算法的蒟蒻12 小时前
day35(12.16)——leetcode面试经典150
算法·leetcode·面试
serendipity_hky13 小时前
【go语言 | 第5篇】channel——多个goroutine之间通信
开发语言·后端·golang
LYFlied13 小时前
【每日算法】LeetCode 234. 回文链表详解
算法·leetcode·链表
源代码•宸13 小时前
分布式缓存-GO(简历写法、常见面试题)
服务器·开发语言·经验分享·分布式·后端·缓存·golang