NSArray * arr = @[@10,@9,@2,@5,@3,@7,@101,@18];
NSMutableArray * mutArr = [[NSMutableArray alloc] init];
int count = 0;
for (int i = 0; i < arr.count; i++) {
[mutArr removeAllObjects];
[mutArr addObject:arr[i]];
for (int j = i+1; j < arr.count; j++) {
if ([arr[j] intValue] > [[mutArr lastObject] intValue]) {
[mutArr addObject:arr[j]];
}
}
if (count < mutArr.count) {
count = mutArr.count;
}
}
NSLog(@"%d",count);
給定一個無序的整數數組,找到其中最長上升子序列的長度。
示例:
輸入: [10,9,2,5,3,7,101,18]
輸出: 4
解釋: 最長的上升子序列是 [2,3,7,101],它的長度是 4。
說明:
可能會有多種最長上升子序列的組合,你只需要輸出對應的長度即可。
你算法的時間複雜度應該爲 O(n2) 。