Golang | Leetcode Golang题解之第354题俄罗斯套娃信封问题

题目:

题解:

Go 复制代码
func maxEnvelopes(envelopes [][]int) int {
    n := len(envelopes)
    if n == 0 {
        return 0
    }

    sort.Slice(envelopes, func(i, j int) bool {
        a, b := envelopes[i], envelopes[j]
        return a[0] < b[0] || a[0] == b[0] && a[1] > b[1]
    })

    f := make([]int, n)
    for i := range f {
        f[i] = 1
    }
    for i := 1; i < n; i++ {
        for j := 0; j < i; j++ {
            if envelopes[j][1] < envelopes[i][1] {
                f[i] = max(f[i], f[j]+1)
            }
        }
    }
    return max(f...)
}

func max(a ...int) int {
    res := a[0]
    for _, v := range a[1:] {
        if v > res {
            res = v
        }
    }
    return res
}
相关推荐
爱学习的小邓同学5 小时前
Golang --- (1)第一个Golang程序
开发语言·后端·golang
Xin7709 小时前
LeetCode 23.合并 K 个升序链表(分治递归)
leetcode
路多辛11 小时前
全能型 Agent covo-agent v0.0.7 发布:TUI 新增 LLM 内联自动补全
golang
圣殿骑士-Khtangc11 小时前
Gin优雅关停与平滑重启从零停机到热更新
golang·gin
程序员z712 小时前
深入理解 GMP 模型:Go 协程调度器的设计与调度场景全解析
服务器·网络·golang
Nil20812 小时前
leetcode 105从前序和中序遍历序列构造二叉树
算法·leetcode·职场和发展
爱学习的小邓同学12 小时前
Golang语言入门
开发语言·后端·golang
Nil20813 小时前
leetcode 114二叉树展开为链表
leetcode·链表·深度优先
cz071013 小时前
hot100_搜索二维矩阵 II
算法·leetcode
路多辛14 小时前
全能型 Go Agent 框架 covonaut v1.0.8 发布:新增行内补全与多后端可观测性
开发语言·golang·agent