mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
添加 problem 354
This commit is contained in:
@ -0,0 +1,42 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
type sortEnvelopes [][]int
|
||||
|
||||
func (s sortEnvelopes) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
func (s sortEnvelopes) Less(i, j int) bool {
|
||||
if s[i][0] == s[j][0] {
|
||||
return s[i][1] > s[j][1]
|
||||
}
|
||||
return s[i][0] < s[j][0]
|
||||
}
|
||||
func (s sortEnvelopes) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
|
||||
func maxEnvelopes(envelopes [][]int) int {
|
||||
sort.Sort(sortEnvelopes(envelopes))
|
||||
dp := []int{}
|
||||
for _, e := range envelopes {
|
||||
low, high := 0, len(dp)
|
||||
for low < high {
|
||||
mid := low + (high-low)>>1
|
||||
if dp[mid] >= e[1] {
|
||||
high = mid
|
||||
} else {
|
||||
low = mid + 1
|
||||
}
|
||||
}
|
||||
if low == len(dp) {
|
||||
dp = append(dp, e[1])
|
||||
} else {
|
||||
dp[low] = e[1]
|
||||
}
|
||||
}
|
||||
return len(dp)
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question354 struct {
|
||||
para354
|
||||
ans354
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para354 struct {
|
||||
envelopes [][]int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans354 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem354(t *testing.T) {
|
||||
|
||||
qs := []question354{
|
||||
|
||||
question354{
|
||||
para354{[][]int{[]int{5, 4}, []int{6, 4}, []int{6, 7}, []int{2, 3}}},
|
||||
ans354{3},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 354------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans354, q.para354
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, maxEnvelopes(p.envelopes))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
31
Algorithms/0354. Russian Doll Envelopes/README.md
Executable file
31
Algorithms/0354. Russian Doll Envelopes/README.md
Executable file
@ -0,0 +1,31 @@
|
||||
# [354. Russian Doll Envelopes](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)
|
||||
|
||||
**Note:**Rotation is not allowed.
|
||||
|
||||
**Example:**
|
||||
|
||||
**Input:** [[5,4],[6,4],[6,7],[2,3]]
|
||||
**Output:** 3
|
||||
**Explanation: T**he maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一些标记了宽度和高度的信封,宽度和高度以整数对形式 (w, h) 出现。当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。
|
||||
|
||||
请计算最多能有多少个信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。
|
||||
|
||||
说明:
|
||||
- 不允许旋转信封。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出一组信封的宽度和高度,如果组成俄罗斯套娃,问最多能套几层。只有当一个信封的宽度和高度都比另外一个信封大的时候,才能套在小信封上面。
|
||||
- 这一题的实质是第 300 题 Longest Increasing Subsequence 的加强版。能组成俄罗斯套娃的条件就是能找到一个最长上升子序列。但是这题的条件是二维的,要求能找到在二维上都能满足条件的最长上升子序列。先降维,把宽度排序。然后在高度上寻找最长上升子序列。这里用到的方法和第 300 题的方法一致。解题思路详解见第 300 题。
|
Reference in New Issue
Block a user