1. 程式人生 > 實用技巧 >[LeetCode] 674. Longest Continuous Increasing Subsequence

[LeetCode] 674. Longest Continuous Increasing Subsequence

Given an unsorted array of integers, find the length of longestcontinuousincreasing subsequence (subarray).

Example 1:

Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. 
Even though [1,3,5,7] is also an increasing subsequence, 
it's not a continuous one where 5 and 7 are separated by 4.

Example 2:

Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1. 

Note:Length of the array will not exceed 10,000.

最長連續遞增序列。我一開始以為是一個偏向滑動視窗的題但是後來我想多了。直接看程式碼吧。

時間O(n)

空間O(1)

Java實現

 1 class Solution {
 2     public int findLengthOfLCIS(int[] nums) {
3 // corner case 4 if (nums == null || nums.length == 0) { 5 return 0; 6 } 7 8 // normal case 9 int res = 1; 10 int count = 1; 11 for (int i = 1; i < nums.length; i++) { 12 if (nums[i] > nums[i - 1]) { 13 count++;
14 res = Math.max(res, count); 15 } else { 16 count = 1; 17 } 18 } 19 return res; 20 } 21 }

LeetCode 題目總結