05 August 2008
最多能将一个array分成若干块,每块单独排序后,连接起来后是有序的,求最多数量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int maxChunksToSorted(int[] arr) {
LinkedList<Integer> stack = new LinkedList<Integer>();
for(int num : arr) {
if(!stack.isEmpty() && num < stack.getLast()) {
int head = stack.removeLast();
while(!stack.isEmpty() && num < stack.getLast()) stack.removeLast();
stack.addLast(head);
}
else stack.addLast(num);
}
return stack.size();
}
}