我正在研究《破解编码面试》第6版,并且遇到了马戏团塔问题(17.8号)。我有一个可以在O(N logN)时间运行的解决方案,但是本书的解决方案(有所不同)说O(N logN)解决方案非常复杂,但是我的代码却没有。我希望获得一些帮助,以确定我的解决方案是否正确,以及它是否实际上在O(N logN)时间内运行。我想了解为什么我错了(或正确),所以任何细节都将有所帮助。
马戏团正在设计一个塔楼套路,由站在彼此肩膀上的人们组成。出于实用和审美的原因,每个人都必须比其下方的人矮一些和矮一些。给定马戏团中每个人的身高和体重,编写一种方法来计算此类塔楼中的最大人数。
def circus_tower(people):
# People is a list of tuples of length 2, where item[0] is their height, and item[1] is their weight.
if len(people) == 0:
return 0
people = sorted(people, key=lambda x: x[0]) # Sort people by heights. O(P*logP).
start = 0
end = 0
longest_sequence = 0
for i in range(1, len(people)): # O(P).
if people[i][1] > people[i-1][1]: # Weight check.
end = i
else:
longest_sequence = end - start + 1
start = i
end = i
return max(longest_sequence, end - start + 1)
以下是一些示例输入以及我的代码返回的内容:
circus_tower([(65, 100), (70, 150), (56, 90), (75, 190), (60, 95), (68, 110)])
6
circus_tower([(65, 100), (70, 150), (56, 90), (75, 190), (65, 95), (68, 110)])
4
circus_tower([(56, 90), (65, 95), (72, 100), (68, 90), (70, 150), (75, 190)])
2
circus_tower([])
0
答案 0 :(得分:0)
我还“找到了”一个简单的解决方案,无法理解我错了:
module CircusTower
class Person
include Comparable
attr_reader :height, :weight
def initialize(height, weight)
@height = height
@weight = weight
end
def <=>(other)
res = height <=> other.height
if res == 0
weight <=> other.weight
else
res
end
end
end
# Time=O(n * log n) Mem=O(n)
def solve_b(people)
sorted = people.sort.reverse
res = []
sorted.each do |person|
if res.size == 0
res << person
else
if res.last.height > person.height && res.last.weight > person.weight
res << person
end
end
end
res.size
end
RSpec.describe 'CircusTower' do
include CircusTower
subject { solve_b(people) }
context 'book example' do
let(:people) do
[
Person.new(65, 100),
Person.new(70, 150),
Person.new(56, 90),
Person.new(75, 190),
Person.new(60, 95),
Person.new(68, 110),
]
end
it { is_expected.to eq 6 }
end
context 'tricky example' do
let(:people) do
[
Person.new(1,1),
Person.new(1,7),
Person.new(1,9),
Person.new(2,2),
Person.new(2,6),
Person.new(3,3),
Person.new(3,5),
Person.new(4,4),
]
end
it { is_expected.to eq 4 }
end
end
end
答案 1 :(得分:0)
有正确的解决方案
module CircusTowerSo
class Person
include Comparable
attr_reader :height, :weight
def initialize(height, weight)
@height = height
@weight = weight
end
def <=>(other)
res = height <=> other.height
if res == 0
weight <=> other.weight
else
res
end
end
def smaller?(other)
height < other.height && weight < other.weight
end
def to_s
"(#{height}, #{weight})"
end
def inspect
to_s
end
end
# Time=O(n * n) Mem=O(n * n)
def solve_b(people)
sorted = people.sort
find_lis_by_weight(sorted).size
end
def find_lis_by_weight(people)
longest_by_index_cache = people.each_with_index.map { |person, i| [i, [person]] }.to_h
longest = []
people.each_with_index do |person, index|
res = longest_for_index(longest_by_index_cache, index, person)
if res.size > longest.size
longest = res
end
longest_by_index_cache[index] = res
end
longest
end
def longest_for_index(longest_by_index_cache, index, person)
longest_prev_seq = []
index.times do |i|
prev_seq = longest_by_index_cache[i]
if prev_seq.last.smaller?(person) && prev_seq.size > longest_prev_seq.size
longest_prev_seq = prev_seq
end
end
longest_prev_seq + [person]
end
RSpec.describe 'CircusTower' do
include CircusTower
subject { solve_b(people) }
context 'book example' do
let(:people) do
[
Person.new(65, 100),
Person.new(70, 150),
Person.new(56, 90),
Person.new(75, 190),
Person.new(60, 95),
Person.new(68, 110),
]
end
it { is_expected.to eq 6 }
end
context 'tricky example' do
let(:people) do
[
Person.new(1, 1),
Person.new(1, 7),
Person.new(1, 9),
Person.new(2, 2),
Person.new(2, 6),
Person.new(3, 3),
Person.new(3, 5),
Person.new(4, 4),
]
end
it { is_expected.to eq 4 }
end
context 'more tricky example' do
let(:people) do
[
Person.new(1, 1),
Person.new(2, 2),
Person.new(3, 3),
Person.new(4, 1),
]
end
it { is_expected.to eq 3 }
end
end
end
在https://github.com/holyketzer/ctci_v6上查看CTCI的更多解决方案
答案 2 :(得分:0)
我把问题分成了三个部分。
插入数据HeightWeight对象,然后按高度或宽度排序。我已经按高度排序了
然后将值插入地图以获得具有最小重量的唯一高度。
在那之后,我找到了重量的最长递增子序列。
import java.util.*;
public class CircusTower {
private class HeightWeight implements Comparable<HeightWeight>{
int height;
int weight;
HeightWeight(int height, int weight) {
this.height = height;
this.weight = weight;
}
@Override
public int compareTo(HeightWeight other) {
if(this.height == other.height){
return this.weight - other.weight;
}else{
return this.height - other.height;
}
}
}
public static void main(String[] args) {
int[][] arr = {{1,1},{1,7},{1,9},{2,2},{2,6},{3,3},{3,5},{4,4}};
CircusTower ct = new CircusTower();
System.out.println(ct.getMaxHeightTower(arr));
}
public int getMaxHeightTower(int[][] arr){
List<HeightWeight> list = new ArrayList<>();
int i =0;
for(i =0; i<arr.length; i++){
list.add(new HeightWeight(arr[i][0], arr[i][1]));
}
Collections.sort(list);
Map<Integer, Integer> map = new HashMap<>();
for(i =0; i<list.size(); i++){
HeightWeight current = list.get(i);
if(!map.containsKey(current.height)){
map.put(current.height, current.weight);
}
}
int[] nums = map.values().stream().mapToInt(Integer::intValue).toArray();
return getLIS(nums);
}
public int getLIS(int[] nums){
int _l = nums.length;
int[] out = new int[_l];
int mx = Integer.MIN_VALUE;
/*
we initialize the array with ones because
a single character has a subsequence of length one
*/
Arrays.fill(out, 1);
for(int i = 0; i < _l; i++)
for(int j = i + 1; j < _l; j++){
/*
every iteration, all we're doing is checking what is the longest
increasing subsequence so far, till this point
*/
if(nums[j] > nums[i])
out[j] = Math.max(out[j], out[i] + 1);
/*
we keep checking for a max value because the longest
subsequence could exist before the end of the string
*/
mx = Math.max(out[j], mx);
}
return mx == Integer.MIN_VALUE ? 1 : mx;
}
}
答案 3 :(得分:-1)
您的解决方案是错误的。如果您运行
circus_tower([[1,1],[1,7],[1,9],[2,2],[2,6],[3,3],[3,5],[4,4]])
它返回2
,而最长的子序列([1,1]<[2,2]<[3,3]<[4,4]
)的长度为4。
代码的问题是只能找到连续的子序列。