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
}
相关推荐
96245614 小时前
Go EVM 链上数据平台实践:从 RPC Demo 到可演进架构
rpc·架构·golang
Nil20816 小时前
leetcode 49字母异位分组
算法·leetcode·哈希算法
lvwangshu18 小时前
字符串 题解
字符串·trie·题解·贪心·技巧·性质题·思维好题
银河技术20 小时前
从脚本到 Operator:用 Go 构建可靠的微服务自动扩缩容控制器
开发语言·微服务·golang
Nil2081 天前
leetcode 5最长回文子串
算法·leetcode·职场和发展
m0_674571971 天前
力扣 hot 100 python
python·算法·leetcode
圣保罗的大教堂1 天前
leetcode 3727. 最大交替平方和 中等
leetcode
Xin7701 天前
LeetCode 200. 岛屿数量(DFS 深度优先遍历法)
leetcode
alphaTao2 天前
LeetCode 每日一题 2026/10/12-2026/10/18
算法·leetcode
Cccp.1232 天前
【leetcode】(十三)大数据处理
数据结构·算法·leetcode