SoFunction
Updated on 2025-03-04

Go language problem solution LeetCode674 longest continuous incremental sequence

Question description

674. Longest continuous incremental sequence

Given an array of unsorted integers, find the longest andContinuously incremented subsequences, and return the length of the sequence.

Continuously incremented subsequencesCan be subscripted by twolandr(l < r)OK if for eachl <= i < r, allnums[i] < nums[i + 1], then subsequence[nums[l], nums[l + 1], ..., nums[r - 1], nums[r]]It is a continuous incremental sub-sequence.

Example 1:

enter:nums = [1,3,5,4,7]
Output:3
explain:The longest continuous incremental sequence is [1,3,5], The length is3。
although [1,3,5,7] It is also a subsequence of ascending order, But it's not continuous,because 5 and 7 In the original array 4 Separate。 

Example 2:

enter:nums = [2,2,2,2,2]
Output:1
explain:The longest continuous incremental sequence is [2], The length is1。

hint:

1 <= <= 10^4

-10^9 <= nums[i] <= 10^9

Idea Analysis

One traversal:

Maintain a value that records the current incremental length: temp = 1, because the minimum incremental growth is also 1

Update temp:

  • When the current value is greater than the previous value, it is an incremental sequence, temp = temp + 1
  • When the current value is less than or equal to the previous value, classify temp into the result list and reset temp = 1

When ended, the last temp is grouped into the result list

Returns the largest value in the result list.

Of course, you can also not use list storage. You just need to reset temp, compare the current temp and the largest temp_max, and then update temp_max. Use lists to help you understand more.

AC Code

class Solution:
    def findLengthOfLCIS(self, nums: List[int]) -> int:
        rev = []
        ln = len(nums)
        if ln == 0 or ln == 1:
            return ln
        temp = 1
        for i in range(1, ln):
            if nums[i]>nums[i-1]:
                temp += 1
            else:
                (temp)
                temp = 1
        (temp)
        return max(rev)

Don't use list:

class Solution:
    def findLengthOfLCIS(self, nums: List[int]) -> int:
        rev = 0
        ln = len(nums)
        if ln == 0 or ln == 1:
            return ln
        temp = 1
        for i in range(1, ln):
            if nums[i]>nums[i-1]:
                temp += 1
            else:
                rev = temp if temp > rev else rev
                temp = 1
        rev = temp if temp > rev else rev
        return rev

refer to

Drawing solution algorithm: 674. Longest continuous incremental sequence - Longest continuous incremental sequence - LeetCode ()

The above is the detailed content of the longest continuous incremental sequence of the go language problem LeetCode674. For more information about the longest continuous incremental sequence of the go problem solution, please pay attention to my other related articles!