leetcode354 俄罗斯套娃信封问题 golang


本文摘自网络,作者,侵删。

354. 俄罗斯套娃信封问题

解法1

思路

动态规划,用A[i] 表示 [0,i]最多的信封数。
那么有初始条件 A[i]=1,
则对于数组A[i]前面的数比它小的数A[j],有A[i] = max(A[i],A[j]+1)
所有有递推公式 A[i] = max(A[0],A[1],A[2],...A[j])+1

代码

func maxEnvelopes(envelopes [][]int) int {
    if len(envelopes)==0{
        return 0
    }
    sort.Slice(envelopes,func(i,j int)bool{
        if envelopes[i][0]==envelopes[j][0]{
            return envelopes[i][1]<envelopes[j][1]
        }
        return envelopes[i][0] < envelopes[j][0]
    })
    A := make([]int,len(envelopes))
    A[0]=1
    ans := 1
    for i:=1;i<len(A);i++{
        A[i]=1
        for j:=0;j<i;j++{
            if envelopes[i][0]> envelopes[j][0] && envelopes[i][1] > envelopes[j][1]{
                A[i]=max(A[i],A[j]+1)
            }
        }
        ans = max(ans,A[i])
    }
    fmt.Println(envelopes)
    fmt.Println(A)
    return ans
}

 func max(a,b int)int{
    if a>b{
        return a
    }
    return b
}

时间复杂度 O(n)

解法2

思路

这个思路关键在于我们尽量维护尽可能小的数组

代码

func maxEnvelopes(envelopes [][]int) int {
    if len(envelopes)==0{
        return 0
    }
    sort.Slice(envelopes,func(i,j int)bool{
        if envelopes[i][0]==envelopes[j][0]{
            return envelopes[i][1]>envelopes[j][1]
        }
        return envelopes[i][0] < envelopes[j][0]
    })
    A := make([][]int,0,len(envelopes))
    for _,item := range envelopes{
        // 在数组A中寻找第一个大于item的下标,如果不存在,返回A的长度。
        index := sort.Search(len(A),func(i int)bool{
            return A[i][0] >= item[0] || A[i][1]>=item[1]
        })
        if index < len(A){
            A[index]=item
        }else{
            A = append(A,item)
        }
    }
    return len(A)
}

本文来自:简书

感谢作者:lucasgao

查看原文:leetcode354 俄罗斯套娃信封问题 golang

相关阅读 >>

Golang判断interface为nil

聊聊dubbo-Go-proxy的authorityfilter

详解Golang ssh连接服务器(模拟交互terminal)

Go - 实现项目内链路追踪(二)

Go timer 是如何被调度的?

Golang中线程和协程的区别是什么

这一次,彻底搞懂 Go cond

20 Golang中使用第三方包

手撸Golang 创建型设计模式 抽象工厂

Go 使用pprof 排查内存泄露

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...