Golang | Leetcode Golang题解之第312题戳气球

题目:

题解:

Go 复制代码
func maxCoins(nums []int) int {
    n := len(nums)
    rec := make([][]int, n + 2)
    for i := 0; i < n + 2; i++ {
        rec[i] = make([]int, n + 2)
    }
    val := make([]int, n + 2)
    val[0], val[n+1] = 1, 1
    for i := 1; i <= n; i++ {
        val[i] = nums[i-1]
    }
    for i := n - 1; i >= 0; i-- {
        for j := i + 2; j <= n + 1; j++ {
            for k := i + 1; k < j; k++ {
                sum := val[i] * val[k] * val[j]
                sum += rec[i][k] + rec[k][j]
                rec[i][j] = max(rec[i][j], sum)
            }
        }
    }
    return rec[0][n+1]
}

func max(x, y int) int {
    if x > y {
        return x
    }
    return y
}
相关推荐
Swift社区8 小时前
LeetCode 421 - 数组中两个数的最大异或值
算法·leetcode·职场和发展
Kuo-Teng11 小时前
LeetCode 206: Reverse Linked List
java·算法·leetcode·职场和发展
做怪小疯子15 小时前
LeetCode 热题 100——哈希——最长连续序列
算法·leetcode·哈希算法
Dream it possible!16 小时前
LeetCode 面试经典 150_二叉树_二叉树展开为链表(74_114_C++_中等)
c++·leetcode·链表·面试·二叉树
做怪小疯子16 小时前
LeetCode 热题 100——双指针——三数之和
算法·leetcode·职场和发展
sin_hielo16 小时前
leetcode 2536
数据结构·算法·leetcode
flashlight_hi16 小时前
LeetCode 分类刷题:203. 移除链表元素
算法·leetcode·链表
py有趣16 小时前
LeetCode算法学习之数组中的第K个最大元素
学习·算法·leetcode
吗~喽16 小时前
【LeetCode】将 x 减到 0 的最小操作数
算法·leetcode
stand_forever18 小时前
PHP客户端调用由Go服务端GRPC接口
rpc·golang·php