Skip to the content.

Kick Start 2020 Round C [1] Countdown


Contact me:

Blog -> https://cugtyt.github.io/blog/index
Email -> cugtyt@qq.com
GitHub -> Cugtyt@GitHub


描述:

一个数字数组,给定k,记录出现[k, k-1, …, 1]的连续子数组的次数。

思路:

记录数字减小的长度,如果出现了1且计数大于目标长度,表示一个出现。

def kcountdown(nums, k):
    decrease_count, count = 0, 0
    for i in range(1, len(nums)):
        if nums[i] == nums[i - 1] + 1:
            decrease_count += 1
        else:
            decrease_count = 0
        if nums[i] == 1 and decrease_count >= k - 1:
            count += 1
        
    return count