日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 套娃_【leetcode编程题目354】俄罗斯套娃

發(fā)布時間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 套娃_【leetcode编程题目354】俄罗斯套娃 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

來自?https://leetcode.com/problems/russian-doll-envelopes/

You have a number of envelopes with widths and heights given as a pair of integers?(w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.

What is the maximum number of envelopes can you Russian doll? (put one inside other)

Example:

Given envelopes =?[[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is?3?([2,3] => [5,4] => [6,7]).

這個題目是最長子序列的變形;最長子序列可以參考https://en.wikipedia.org/wiki/Longest_increasing_subsequence

本題目的解法:

1. 先將長方形按照寬排序,如果寬度一樣, 按照高度降序排列,即 wi < wj or wi == wj && hi > hj; 這樣得到的序列,從寬度考慮,前面的肯定可以放入后面的;對于wi == wj && hi > hj是因為在寬度一致的情況下,將更高的放在前面,可以保證后面根據(jù)高度查找最長子序列的時候,將這種組合排除;

2. 然后根據(jù)高度查找可以嵌套的最長子序列即可;

func maxEnvelopes(envelopes [][]int) int {

sort.Sort(byShape(envelopes))

m := make([]int, len(envelopes)+1, len(envelopes)+1)

L := 0

for i, a := range envelopes {

lo, hi := 1, L

for lo <= hi {

mid := (lo + hi) / 2

b := envelopes[m[mid]]

if b[1] < a[1] {

lo = mid + 1

} else {

hi = mid - 1

}

}

m[lo] = i

if L < lo {

L = lo

}

}

return L

}

type byShape [][]int

func (a byShape) Len() int {

return len(a)

}

func (a byShape) Less(i, j int) bool {

if a[i][0] != a[j][0] {

return a[i][0] < a[j][0]

}

return a[j][1] < a[i][1]

}

func (a byShape) Swap(i, j int) {

a[i], a[j] = a[j], a[i]

}

總結(jié)

以上是生活随笔為你收集整理的java 套娃_【leetcode编程题目354】俄罗斯套娃的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。