あるインタビューでこの質問をされたことがあります。私は主に、O(n^2) よりも優れた時間で 2 次元配列をトレースするソリューションを提供することに行き詰まりました。質問は次のとおりです。
/*
Here's a helper class that can efficiently return the smallest
object it contains. Assume it magically knows how to sort your
objects correctly.
*/
@interface MinHeap : NSObject
@property (readonly) NSUInteger count;
// Adds an object
- (void)addObject:(id)object;
// Returns (but does not remove) the smallest object, or nil if empty
- (id)minObject;
// Removes and returns the smallest object, or nil if empty
- (id)popMinObject;
// Removes all objects
- (void)removeAllObjects;
// **Implement this method**
- (NSArray*)process:(NSArray*)incomingArray
@end
/*
Sample input:
[
[ @2, @4, @6 ],
[ @1, @5, @10 ],
[ @3, @7, @8, @98, @99 ],
[],
[ @4, @4 ]
]
Expected output:
[ @1, @2, @3, @4, @4, @4, @5, @6, @7, @8, @10, @98, @99 ]
*/
これが私が与えた答えです:
- (NSArray*)process:(NSArray*)incomingArray
{
// n squared
for (int i=0; i<incomingArray.count; i++)
{
NSArray* row = incomingArray[i];
for (int j=0; j<row.count; j++)
[self addObject:row[j]];
}
NSMutableArray* returnArray = [NSMutableArray new];
// n
for (int i=0; i<self.count; i++)
[returnArray addObject:[self minObject]];
return [NSArray arrayWithArray:returnArray];
}
どうやら (私が彼の期待する解決策を示していたとしたら) 2 次元配列内の個々の配列が既にソートされているという仮定を利用していたでしょう。上記の解決策は、私のインタビュアーを納得させませんでした。
では、上記のクラスを O(n^2) よりも効率的な方法で使用して、期待される出力を生成するにはどうすればよいでしょうか?
PS: A) サンプル入力内の個々の配列は常にソートされることに注意してください。B) 入力には重複があり、出力には重複が含まれている必要があります。