publicclassSlidingWindowMaximum { publicstaticint[] maxSlidingWindow(int[] nums, int k) { if (nums == null || k < 1 || nums.length < k) { returnnull; }
LinkedList<Integer> maxQueue = newLinkedList<>(); // 共有 arr.length - k + 1 个窗口, 即共有arr.length - win + 1个最大值 int[] result = newint[nums.length - k + 1]; intindex=0;
// 用 i 表示 R for (inti=0; i < nums.length; i++) { while (!maxQueue.isEmpty() && nums[maxQueue.peekLast()] <= nums[i]) { maxQueue.pollLast(); } maxQueue.addLast(i);
// i - k 为过期的索引, 即窗口的 L 已越过该位置, 需弹出头结点维持窗口大小为 k if (maxQueue.peekFirst() == i - k) { System.out.println(i + " " + k); maxQueue.pollFirst(); } if (i >= k - 1) { result[index++] = nums[maxQueue.peekFirst()]; } }
arr_len = len(height_arr) left_max = height_arr[0] right_max = height_arr[arr_len - 1] l = 1 r = arr_len - 2 result = 0
while l <= r: if left_max <= right_max: result += max(0, left_max - height_arr[l]) left_max = max(left_max, height_arr[l]) l += 1 else: result += max(0, right_max - height_arr[r]) right_max = max(right_max, height_arr[r]) r -= 1
nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2018 NVIDIA Corporation Built on Sat_Aug_25_21:08:01_CDT_2018 Cuda compilation tools, release 10.0, V10.0.130
defget_and_remove_last_element(stack): result = stack.pop() iflen(stack) == 0: return result else: last = get_and_remove_last_element(stack) stack.append(result) return last