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
}
相关推荐
样例过了就是过了1 小时前
LeetCode热题100 柱状图中最大的矩形
数据结构·c++·算法·leetcode
wsoz1 小时前
Leetcode哈希-day1
算法·leetcode·哈希算法
阿Y加油吧1 小时前
LeetCode 二叉搜索树双神题通关!有序数组转平衡 BST + 验证 BST,小白递归一把梭
java·算法·leetcode
小肝一下3 小时前
每日两道力扣,day2
c++·算法·leetcode·职场和发展
米粒14 小时前
力扣算法刷题 Day 31 (贪心总结)
算法·leetcode·职场和发展
AlenTech4 小时前
647. 回文子串 - 力扣(LeetCode)
算法·leetcode·职场和发展
py有趣4 小时前
力扣热门100题之合并两个有序链表
算法·leetcode·链表
8Qi84 小时前
LeetCode热题100--45.跳跃游戏 II
java·算法·leetcode·贪心算法·编程
lucky九年5 小时前
GO语言模拟C++封装,继承,多态
开发语言·c++·golang
北顾笙9805 小时前
day12-数据结构力扣
数据结构·算法·leetcode