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
}
相关推荐
xindon125 分钟前
go语言项目部署的makefile
开发语言·后端·golang
Lsk_Smion5 分钟前
力扣实训 _ [98].验证二叉搜索树 _ 将二叉树展开成链表
数据结构·算法·leetcode
8Qi88 分钟前
LeetCode 377:组合总和 Ⅳ(Combination Sum IV)—— 题解 ✅
算法·leetcode·动态规划·完全背包
凯瑟琳.奥古斯特9 分钟前
力扣1002题C++解法详解
开发语言·c++·算法·leetcode·职场和发展
codeejun20 分钟前
每日一Go-75、CI/CD 到 K8s:云原生ArgoCD / GitOps 全流程实战(Go + Gin)
ci/cd·云原生·golang
_日拱一卒32 分钟前
LeetCode:17电话号码的字母组合
java·数据结构·算法·leetcode·职场和发展
右耳朵猫AI37 分钟前
Go周刊2026W21 | Fiber 3.3、errcheck 1.20、Jet 2.15、Sarama 1.49
开发语言·后端·golang
sheeta199810 小时前
LeetCode 每日一题笔记 日期:2026.06.02 题目:3635. 最早完成陆地和水上游乐设施的时间 II
笔记·算法·leetcode
Lsk_Smion10 小时前
力扣实训 _ [102].层序遍历--前序--后续_递归与非递归的实现
数据结构·算法·leetcode
小欣加油12 小时前
leetcode3751 范围内总波动值I
java·数据结构·c++·算法·leetcode