classSolution { public: vector<int> maxSlidingWindow(vector<int>& nums, int k){ int n = nums.size(); priority_queue<pair<int, int>> q; for (int i = 0; i < k; ++i) { q.emplace(nums[i], i); } vector<int> ans = {q.top().first}; for (int i = k; i < n; ++i) { q.emplace(nums[i], i); while (q.top().second <= i - k) { q.pop(); } ans.push_back(q.top().first); } return ans; } };
var a []int type hp struct{ sort.IntSlice } func(h hp) Less(i, j int) bool { return a[h.IntSlice[i]] > a[h.IntSlice[j]] } func(h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) } func(h *hp) Pop() interface{} { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }
funcmaxSlidingWindow(nums []int, k int) []int { a = nums q := &hp{make([]int, k)} for i := 0; i < k; i++ { q.IntSlice[i] = i } heap.Init(q)
n := len(nums) ans := make([]int, 1, n-k+1) ans[0] = nums[q.IntSlice[0]] for i := k; i < n; i++ { heap.Push(q, i) for q.IntSlice[0] <= i-k { heap.Pop(q) } ans = append(ans, nums[q.IntSlice[0]]) } return ans }
classSolution { public: vector<int> maxSlidingWindow(vector<int>& nums, int k){ int n = nums.size(); deque<int> q; for (int i = 0; i < k; ++i) { while (!q.empty() && nums[i] >= nums[q.back()]) { q.pop_back(); } q.push_back(i); }
vector<int> ans = {nums[q.front()]}; for (int i = k; i < n; ++i) { while (!q.empty() && nums[i] >= nums[q.back()]) { q.pop_back(); } q.push_back(i); while (q.front() <= i - k) { q.pop_front(); } ans.push_back(nums[q.front()]); } return ans; } };
classSolution { publicint[] maxSlidingWindow(int[] nums, int k) { intn= nums.length; Deque<Integer> deque = newLinkedList<Integer>(); for (inti=0; i < k; ++i) { while (!deque.isEmpty() && nums[i] >= nums[deque.peekLast()]) { deque.pollLast(); } deque.offerLast(i); }
int[] ans = newint[n - k + 1]; ans[0] = nums[deque.peekFirst()]; for (inti= k; i < n; ++i) { while (!deque.isEmpty() && nums[i] >= nums[deque.peekLast()]) { deque.pollLast(); } deque.offerLast(i); while (deque.peekFirst() <= i - k) { deque.pollFirst(); } ans[i - k + 1] = nums[deque.peekFirst()]; } return ans; } }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution: defmaxSlidingWindow(self, nums: List[int], k: int) -> List[int]: n = len(nums) q = collections.deque() for i inrange(k): while q and nums[i] >= nums[q[-1]]: q.pop() q.append(i)
ans = [nums[q[0]]] for i inrange(k, n): while q and nums[i] >= nums[q[-1]]: q.pop() q.append(i) while q[0] <= i - k: q.popleft() ans.append(nums[q[0]]) return ans
n := len(nums) ans := make([]int, 1, n-k+1) ans[0] = nums[q[0]] for i := k; i < n; i++ { push(i) for q[0] <= i-k { q = q[1:] } ans = append(ans, nums[q[0]]) } return ans }
classSolution { public: vector<int> maxSlidingWindow(vector<int>& nums, int k){ int n = nums.size(); vector<int> prefixMax(n), suffixMax(n); for (int i = 0; i < n; ++i) { if (i % k == 0) { prefixMax[i] = nums[i]; } else { prefixMax[i] = max(prefixMax[i - 1], nums[i]); } } for (int i = n - 1; i >= 0; --i) { if (i == n - 1 || (i + 1) % k == 0) { suffixMax[i] = nums[i]; } else { suffixMax[i] = max(suffixMax[i + 1], nums[i]); } }
vector<int> ans; for (int i = 0; i <= n - k; ++i) { ans.push_back(max(suffixMax[i], prefixMax[i + k - 1])); } return ans; } };
classSolution { publicint[] maxSlidingWindow(int[] nums, int k) { intn= nums.length; int[] prefixMax = newint[n]; int[] suffixMax = newint[n]; for (inti=0; i < n; ++i) { if (i % k == 0) { prefixMax[i] = nums[i]; } else { prefixMax[i] = Math.max(prefixMax[i - 1], nums[i]); } } for (inti= n - 1; i >= 0; --i) { if (i == n - 1 || (i + 1) % k == 0) { suffixMax[i] = nums[i]; } else { suffixMax[i] = Math.max(suffixMax[i + 1], nums[i]); } }
int[] ans = newint[n - k + 1]; for (inti=0; i <= n - k; ++i) { ans[i] = Math.max(suffixMax[i], prefixMax[i + k - 1]); } return ans; } }
[sol3-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution: defmaxSlidingWindow(self, nums: List[int], k: int) -> List[int]: n = len(nums) prefixMax, suffixMax = [0] * n, [0] * n for i inrange(n): if i % k == 0: prefixMax[i] = nums[i] else: prefixMax[i] = max(prefixMax[i - 1], nums[i]) for i inrange(n - 1, -1, -1): if i == n - 1or (i + 1) % k == 0: suffixMax[i] = nums[i] else: suffixMax[i] = max(suffixMax[i + 1], nums[i])
ans = [max(suffixMax[i], prefixMax[i + k - 1]) for i inrange(n - k + 1)] return ans
var maxSlidingWindow = function(nums, k) { const n = nums.length; const prefixMax = newArray(n).fill(0); const suffixMax = newArray(n).fill(0); for (let i = 0; i < n; i++) { if (i % k === 0) { prefixMax[i] = nums[i]; } else { prefixMax[i] = Math.max(prefixMax[i - 1], nums[i]); } } for (let i = n - 1; i >= 0; --i) { if (i === n || (i + 1) % k === 0) { suffixMax[i] = nums[i]; } else { suffixMax[i] = Math.max(suffixMax[i + 1], nums[i]); } } const ans = []; for (let i = 0; i < n - k + 1; i++) { ans.push(Math.max(suffixMax[i], prefixMax[i + k - 1])); } return ans; };