LeetCode Biweekly Contest 137 | Find the Power of K-Size Subarrays II

Поділитися
Вставка
  • Опубліковано 2 лис 2024

КОМЕНТАРІ • 1

  • @saivamsi8400
    @saivamsi8400 2 місяці тому

    Here is MY Code :
    class Solution:
    def resultsArray(self, nums: List[int], k: int) -> List[int]:
    n = len(nums)
    if n == 1: #If there is only one ele it is always sorted
    if k == 1:
    return [nums[0]]
    else:
    return [-1]
    if k == 1: #Window containing one ele is always sorted
    return nums
    i = 0
    j = 0
    res = [-1 for i in range(n-k+1)] #Initialising result with -1
    while j < n:
    if j > 0 and nums[j] != nums[j-1] + 1: #Checking the condition to calculate our ans
    i = j # If they are not consecutive => any window containing them cannot be sorted so slide the window beyond the pair
    j += 1
    continue
    if j - i + 1 < k: #window size not hit
    j += 1
    elif j - i + 1 == k: #window size hit so take decision
    res[i] = nums[j]
    i += 1
    j += 1
    return res