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
}
相关推荐
爱coding的橙子2 小时前
每日算法刷题Day2 5.10:leetcode数组1道题3种解法,用时40min
算法·leetcode
阳洞洞2 小时前
leetcode 18. 四数之和
leetcode·双指针
Kidddddult3 小时前
力扣刷题Day 48:盛最多水的容器(283)
算法·leetcode·力扣
Cxzzzzzzzzzz5 小时前
Kafka Go客户端--Sarama
中间件·golang·kafka·linq
小南家的青蛙5 小时前
LeetCode面试题 01.09 字符串轮转
java·leetcode
元亓亓亓6 小时前
LeetCode热题100--240.搜索二维矩阵--中等
算法·leetcode·矩阵
川川籽7 小时前
hashicorp/raft模块实现的raft集群存在节点跨集群身份冲突问题
golang·go-raft
Asus.Blogs8 小时前
为什么 import _ “github.com/go-sql-driver/mysql“ 要导入但不使用?_ 是什么意思?
sql·golang·github
周Echo周9 小时前
20、map和set、unordered_map、un_ordered_set的复现
c语言·开发语言·数据结构·c++·算法·leetcode·list
程序员爱钓鱼10 小时前
跳转语句:break、continue、goto -《Go语言实战指南》
开发语言·后端·golang·go1.19