Leetcode Note: Go - Longest Continuous Increasing Subsequence

Longest Continuous Increasing Subsequence - LeetCode
https://leetcode.com/problems/longest-continuous-increasing-subsequence/

  • Go 言語で取り組んだメモ

所感

  • ソートされていない int 配列 nums が渡される
  • 最も長い continuous increasing subsequence を int で return する
    • subsequence は strictly increasing
  • continuous increasing subsequence とは 2つ のインデックス l, r が次のようになるパターン
    • [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]]
    • かつ それぞれが l <= i < r, nums[i] < nums[i + 1] となるもの

回答

Longest Continuous Increasing Subsequence - LeetCode
https://leetcode.com/problems/longest-continuous-increasing-subsequence/solution/

func findLengthOfLCIS(nums []int) int {
    ans := 0
    anchor := 0
    
    for i := 0; i< len(nums); i++ {
        if i > 0 && nums[i-1] >= nums[i] {
            anchor = i
        }
        ans = int(math.Max(float64(ans), float64(i - anchor + 1)))
    }
    
    return ans
}