以下是“破解编码面试”一书中的问题:
一个马戏团正在设计一个由站在一起的人组成的塔台程序
肩膀。出于实用和审美的考虑,每个人都必须比他或她下面的人矮和轻。给定马戏团中每个人的身高和体重,写出一种计算尽可能多的人数的方法
在这样的塔里。
例子:
输入:
(ht, wt): (65, 100) (70, 150) (56, 90) (75, 190) (60, 95) (68, 110)
输出:
最长的塔长6,自上而下包括:
(56, 90) (60,95) (65,100) (68,110) (70,150) (75,190)
以下是本书中的解决方案:
“当我们去掉这个问题的所有毛边时,我们可以理解,问题实际上是以下几个方面。
我们有一对物品的清单找出最长的顺序,使第一项和第二项都按非递减顺序排列。”
我举了个例子:
{1,1}
{3,3} {7,2}
{6,4} {9,9} {8,5}
在这里,序列的值不是按非递减顺序排列的,但它仍然是一个有效的塔我找不到一种方法把同样的物品组织到另一个有着非递减顺序的塔中我相信没有这种办法。所以在我看来解决方案是不正确的。
我遗漏了什么吗?
提前谢谢。
最佳答案
不,您的值不是按非递减顺序排列的。正如@mooseboys评论所说,在你的情况下,第三个值的权重大于第二个值。({3,3}->{7,2},2问题是最长递增子序列(lis)(dp)的微小变化。
您可以根据高度对元素进行排序,然后应用“在权重上查找最长的递增子序列”。
请在下面找到Java实现:
class Person implements Comparable<Person> {
int height;
int weight;
public Person(int height, int weight) {
this.height = height;
this.weight = weight;
}
@Override
public String toString() {
return "Person{" +
"height=" + height +
", weight=" + weight +
'}';
}
@Override
public int compareTo(Person p) {
if(this.height>p.height) {
return 1;
} else if(this.height < p.height) {
return -1;
} else {
return 0;
}
}
}
public class CircusTower {
public void calculatePeople(Person[] input) {
int weightArray[] = new int[input.length];
String[] output = new String[input.length];
for (int i=0;i<input.length;i++) {
weightArray[i] = 1;
output[i] = input[i].toString() + "";
}
int maxLength = 0;
for (int i=1;i<input.length;i++) {
for (int j=0;j<i;j++) {
if( weightArray[j]+1>weightArray[i] && input[i].weight>input[j].weight) {
weightArray[i] = weightArray[j] + 1;
output[i] = output[j] + " " + input[i].toString();
if(maxLength<weightArray[i]) {
maxLength = weightArray[i];
}
}
}
}
System.out.println();
for (int i = 0; i < input.length; i++) {
if (weightArray[i] == maxLength) {
System.out.println("Longest Increasing subsequence - " + output[i] + " of length = " + maxLength);
}
}
}
public static void main(String[] args) {
CircusTower ct = new CircusTower();
Person p1 = new Person(65,100);
Person p2 = new Person(70,150);
Person p3 = new Person(56, 90);
Person p4 = new Person(75, 190);
Person p5 = new Person(60, 95);
Person p6 = new Person(68, 110);
Person[] array = new Person[]{p1,p2,p3,p4,p5,p6};
Arrays.sort(array);
ct.calculatePeople(array);
}
}
我用的是lis问题的n平方实现,在nlogn中也可以用更好的实现。
希望能澄清。