[Sword refers to offer] 59. The maximum value of the sliding window

Title description

Insert picture description here
Insert picture description here

// 59. 滑动窗口的最大值

// 力扣
// 给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值。

// 牛客
// 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例
// 如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个
// 滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,
// 1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1},
// {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {
// 2,3,4,2,6,[2,5,1]}。
// 窗口大于数组长度的时候,返回空

answer

Maximum heap method

// 最大堆 

// 牛客
// 推荐方法。
// 构建最大堆maxHeap,构建答案保存数组res,
// 先把窗口范围size内的元素放入maxHeap,之后堆顶元素(最大值)放入res,
// 然后构建左指针i,右指针j,for循环右移两个指针,之前的左指针遍历元素
// 在maxHeap中去掉,加入右指针的新遍历元素,构成新的maxHeap(窗口遍历元素)
// 再次把堆顶最大值放入res,如此循环。
// 运行时间:14ms,超过75.10%用Java提交的代码
// 占用内存:9744KB,超过9.76%用Java提交的代码
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Comparator;
public class Solution {
    public ArrayList<Integer> maxInWindows(int [] num, int size) {
		ArrayList<Integer> res = new ArrayList<Integer>();
		if (size > num.length || size < 1)
			return res;
		
		PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o2.compareTo(o1);
			}
		});
		for (int i = 0; i < size; i++) {
			maxHeap.add(num[i]);
		}
		res.add(maxHeap.peek());
		for (int i = 0, j = i + size; j < num.length; i++, j++) {
			maxHeap.remove(num[i]);
			maxHeap.add(num[j]);
			res.add(maxHeap.peek());
		}
		return res;
    }
}



// 力扣
// 执行用时:93 ms, 在所有 Java 提交中击败了5.11%的用户
// 内存消耗:46.4 MB, 在所有 Java 提交中击败了74.46%的用户
class Solution {
    public int[] maxSlidingWindow(int[] num, int size) {
        ArrayList<Integer> res = new ArrayList<Integer>();
		if (size > num.length || size < 1)
			return toIntList(res);
		
		PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o2.compareTo(o1);
			}
		});
		for (int i = 0; i < size; i++) {
			maxHeap.add(num[i]);
		}
		res.add(maxHeap.peek());
		for (int i = 0, j = i + size; j < num.length; i++, j++) {
			maxHeap.remove(num[i]);
			maxHeap.add(num[j]);
			res.add(maxHeap.peek());
		}
		return toIntList(res);
    }

    private int[] toIntList(ArrayList<Integer> res) {
        int[] result = new int[res.size()];
        for (int i = 0; i < result.length; i++) {
            result[i] = res.get(i);
        }
        return result;
    }
}

Queue method


// 力扣
// 执行用时:15 ms, 在所有 Java 提交中击败了48.97%的用户
// 内存消耗:47.5 MB, 在所有 Java 提交中击败了12.75%的用户
import java.util.LinkedList;
class Solution {
    public int[] maxSlidingWindow(int[] num, int size) {
		if (size > num.length || size < 1)
			return new int[0];
		Deque<Integer> queue = new LinkedList<>();
		int[] res = new int[num.length - size + 1];
		for (int i = 0; i < size; i++) {
			while (!queue.isEmpty() && queue.peekLast() < num[i]) {
				queue.removeLast();
			}
			queue.addLast(num[i]);
		}
        int j = 0;
		if (!queue.isEmpty())
			res[j++] = queue.peekFirst();
		for (int i = size; i < num.length; i++) {
			if (!queue.isEmpty() && queue.peekFirst() == num[i - size]) {
				queue.removeFirst();
			}
			while (!queue.isEmpty() && queue.peekLast() < num[i]) {
				queue.removeLast();
			}
			queue.addLast(num[i]);
			res[j++] = queue.peekFirst();
		}
		return res;
    }
}


Guess you like

Origin blog.csdn.net/fisherish/article/details/114969042