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
}
相关推荐
Jeremy爱编码33 分钟前
leetcode热题腐烂的橘子
算法·leetcode·职场和发展
alphaTao1 小时前
LeetCode 每日一题 2025/12/22-2025/12/28
算法·leetcode
小白菜又菜2 小时前
Leetcode 1523. Count Odd Numbers in an Interval Range
算法·leetcode
捧 花2 小时前
前端如何调用后端接口(HTML + JS & Vue )
服务器·golang·vue·api·前后端交互
小白菜又菜3 小时前
Leetcode 944. Delete Columns to Make Sorted
算法·leetcode
bybitq3 小时前
Go 语言之旅方法(Methods)与接口(Interfaces)完全指南
开发语言·golang·xcode
saber_andlibert4 小时前
【C++转GO】初阶知识
开发语言·c++·golang
IT艺术家-rookie4 小时前
golang--解决 Go 并发场景下的数据竞争问题的方案
golang
Swift社区4 小时前
LeetCode 458 - 可怜的小猪
算法·leetcode·职场和发展
Dream it possible!5 小时前
LeetCode 面试经典 150_分治_将有序数组转换为二叉搜索树(105_108_C++_简单)(递归)
c++·leetcode·面试