class Solution: def solve(self, nums, k): if k > len(nums): return [] res = [] temp = nums[0] point = 0 for i in range(k): if nums[i] > temp: temp = nums[i] point = i res.append(temp) for i in range(k, len(nums)): if nums[i] < temp and (i - point) < k: temp = nums[point] elif nums[i] < temp and (i - point) >= k: point = i - k + 1 for j in range(i - k + 1, i + 1): if nums[j] > nums[point]: point = j temp = nums[point] else: temp = nums[i] point = i res.append(temp) return res ob = Solution() nums = [12, 7, 3, 9, 10, 9] k = 3 print(ob.solve(nums, k))