有个马戏团正在设计叠罗汉的表演节目,一个人要站在另一人的肩膀上。出于实际和美观的考虑,在上面的人要比下面的人矮一点且轻一点。已知马戏团每个人的身高和体重,请编写代码计算叠罗汉最多能叠几个人。
示例:
代码语言:javascript复制输入:height = [65,70,56,75,60,68] weight = [100,150,90,190,95,110]
输出:6
解释:从上往下数,叠罗汉最多能叠 6 层:(56,90), (60,95), (65,100), (68,110), (70,150), (75,190)
提示:
height.length == weight.length <= 10000
解题思路
1,先按照身高升序排序
2,相同身高,按照体重降序排序
3,身高转化成了最长递增序列问题
代码实现
代码语言:javascript复制func bestSeqAtIndex(height []int, weight []int) int {
if len(weight)<1{
return 0
}
for i:=0;i<len(height);i {
for j:=i;j<len(height);j {
if height[i]>height[j]{
height[i],height[j]=height[j],height[i]
weight[i],weight[j]=weight[j],weight[i]
}
}
}
for i:=0;i<len(weight)-1;i {
j:=1
for i j<len(weight) && height[i]==height[i j]{
j
}
weight=sort(weight,i,j)
i =j
}
var dp []int
dp=append(dp,weight[0])
for i:=1;i<len(weight);i {
if weight[i]>dp[len(dp)-1]{
dp=append(dp,weight[i])
}else{
l:=0
r:=len(dp)-1
p:=0
for l<=r{
mid:=(l r)>>1
if weight[i]>dp[mid]{
p=mid 1
l=mid 1
}else{
r=mid-1
}
}
dp[p]=weight[i]
}
}
fmt.Println(dp)
return len(dp)
}
func sort(a []int,s,e int)[]int{
for i:=s;i<=e;i {
for j:=i;j<e;j {
if a[i]<a[j]{
a[i],a[j]=a[j],a[i]
}
}
}
return a
}