
Problem
Analysis
Solution
Python implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
class : def findLengthOfLCIS(self, nums): """ :type nums: List[int] :rtype: int """ max_len = 0 cur_len = 0 prev = float("-inf") for num in nums: if num > prev: cur_len += 1 else: max_len = max(cur_len, max_len) cur_len = 1 prev = num return max(max_len, cur_len)
|
Java implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
class { public int findLengthOfLCIS(int[] nums) { int maxLen = 0; int curLen = 0; int prev = 0; for(int num : nums){ if(num > prev){ curLen ++; }else{ maxLen = maxLen > curLen ? maxLen : curLen; curLen = 1; } prev = num; } return maxLen > curLen ? maxLen : curLen; } }
|
Time complexity
O(n).
Space complexity
O(1).
Link
674. Longest Continuous Increasing Subsequence
(中文版) 算法笔记: 力扣#674 最长连续递增序列
近期评论