解题思路,维护一个有序数组(深拷贝),
每移动一步,添加原数组中窗口的最后一个值和删除原数组中窗口的起始值。
# -*- coding:utf-8 -*- class Solution: def _get_sorted_window(self, window, remove_num, add_num): """ 获取新的有序窗口 :param window: 原始窗口 :param remove_num: 移除的数字 :param add_num: 添加的数字 :return: """ if remove_num == add_num: return window new_remove_window = list() for i in range(len(window)): if window[i] == remove_num: new_remove_window = window[0:i] + window[i+1:] break new_add_window = list() flag = True for i in range(len(new_remove_window)): if new_remove_window[i] >= add_num: flag = False new_add_window = new_remove_window[0:i] + [add_num] + new_remove_window[i:] break if flag: new_add_window = new_remove_window + [add_num] return new_add_window def maxInWindows(self, num, size): # write code here if size > len(num) or size == 0: return list() if size == len(num): return [max(num)] window = num[:size].copy() window = sorted(window) rnt = list() rnt.append(window[-1]) # 开始滑动 start_i = 0 for i in range(size, len(num)): if i == 6: print(window) window = self._get_sorted_window(window, num[start_i], num[i]) start_i += 1 rnt.append(window[-1]) return rnt