题目链接
class Solution {
public int findLengthOfLCIS(int[] nums) {
// return findLengthOfLCIS1(nums);
return findLengthOfLCIS2(nums);
}
// 1.贪心算法。速度2ms
public int findLengthOfLCIS1(int[] nums) {
int start = 0;
int max = 1;
for(int i = 1; i < nums.length; i++) {
if(nums[i] <= nums[i-1]) {
start = i;
}
max = Math.max(max, i-start+1); // 两边植树取中间
}
return max;
}
// 2.栈。速度 8ms或7ms
public int findLengthOfLCIS2(int[] nums) {
Stack<Integer> stack = new Stack<>();
int max = 0;
for(int i = 0; i < nums.length; i++) {
while(!stack.isEmpty() && stack.peek() >= nums[i]) {
stack = new Stack<>();
}
stack.push(nums[i]);
max = Math.max(max, stack.size());
}
return max;
}
}