LeetCode239. 滑动窗口最大值(滑动窗口)

给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口 k 内的数字。滑动窗口每次只向右移动一位。

返回滑动窗口最大值。

示例:

输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
输出: [3,3,5,5,6,7] 
解释: 

  滑动窗口的位置                最大值
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7

注意:

你可以假设 k 总是有效的,1 ≤ k ≤ 输入数组的大小,且输入数组不为空。

进阶:

你能在线性时间复杂度内解决此题吗?

分析
对比穷举,滑动窗口可以进行如下剪枝:
第一次在窗口[index+1,index+k)记录最值,用于下一次窗口右移后与新加入的进行取最值,直接得到窗口区域内最值。
但是窗口移动也会舍弃最左边的,所以要根据情况重新遍历,选择用于下一次的最值。
提交击败90%上。

class Solution {
	public int[] maxSlidingWindow(int[] nums, int k) {
		if (nums == null || nums.length == 0)
			return new int[0];
		int[] result = new int[nums.length - k + 1];
		int index = 0;
		int max = Integer.MIN_VALUE;
		for (int i = 1; i < k; ++i) {
			max = Math.max(nums[i], max);
		}
		result[index++] = Math.max(nums[0], max);
		for (; index < result.length; ++index) {
			result[index] = Math.max(max, nums[index + k - 1]);
			// max被窗口舍弃,重新选择max
			if (result[index] == nums[index]) {
				int temp = Integer.MIN_VALUE;
				for (int i = 1; i < k; ++i) {
					temp = Math.max(temp, nums[index + i]);
				}
				max = temp;
			} else {
				max = result[index];
			}
		}
		return result;
	}
}

一开始绕了弯路的解答,5.04%

class Solution {
	public int[] maxSlidingWindow(int[] nums, int k) {
		if (nums == null || nums.length == 0)
			return new int[0];
		int[] result = new int[nums.length - k + 1];
		Queue heap = new Queue();
		int index = 0;
		for (int i = 0; i < k; ++i) {
			heap.add(nums[i]);
		}
		result[index++] = heap.head.value;
		for (int i = 1; i < result.length; ++i) {
			heap.remove(nums[i - 1]);
			heap.add(nums[i + k - 1]);
			result[index++] = heap.head.value;
		}
		return result;
	}

	static class Queue {
		static class Node {
			int value;
			Node next;

			public Node() {
			}

			public Node(int value) {
				this.value = value;
			}

		}

		Node head;

		public Queue() {
		}

		public void remove(int value) {
			Node temp = head, pre = null;
			while (temp.next != null) {
				if (temp.value == value) {
					break;
				} else {
					pre = temp;
					temp = temp.next;
				}
			}
			if (pre == null) {// head
				head = head.next;
			} else {
				pre.next = temp.next;
			}

		}

		public void add(int value) {
			Node newn = new Node(value);
			if (head == null) {
				head = newn;
			} else {
				Node temp = head, pre = null;
				while (temp != null) {
					if (value > temp.value)
						break;
					else {
						pre = temp;
						temp = temp.next;
					}
				}
				if (pre == null) {// head位置
					newn.next = head;
					head = newn;
				} else {
					pre.next = newn;
					newn.next = temp;
				}
			}

		}

		void print() {
			Node temp = head;
			while (temp != null) {
				System.out.println(temp.value);
				temp = temp.next;
			}
		}

	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值