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
}
相关推荐
6Hzlia5 小时前
【Hot 100 刷题计划】 LeetCode 48. 旋转图像 | C++ 矩阵变换题解
c++·leetcode·矩阵
Morwit6 小时前
【力扣hot100】 1. 两数之和
数据结构·c++·算法·leetcode·职场和发展
py有趣7 小时前
力扣热门100题之岛屿的数量(DFS/BFS经典题)
leetcode·深度优先·宽度优先
qinian_ztc8 小时前
frida 14.2.18 安装报错解决
算法·leetcode·职场和发展
Wenweno0o8 小时前
Eino - 错误处理与稳定性
golang·智能体·eino
田梓燊9 小时前
2026/4/11 leetcode 3741
数据结构·算法·leetcode
王码码20359 小时前
Go语言中的Elasticsearch操作:olivere实战
后端·golang·go·接口
Tomhex10 小时前
Go语言import用法详解
golang·go
小肝一下11 小时前
每日两道力扣,day8
c++·算法·leetcode·哈希算法·hot100
Tomhex11 小时前
Golang空白导入的真正用途
golang·go