代码拉取完成,页面将自动刷新
/**
* This file contain an implementation of the maximum sliding window problem. This code has been
* tested against the judge data on:
*
* <p>https://leetcode.com/problems/sliding-window-maximum/description/
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.other;
import java.util.ArrayDeque;
import java.util.Deque;
public class SlidingWindowMaximum {
int[] values;
public int N, lo, hi;
Deque<Integer> deque = new ArrayDeque<>();
public SlidingWindowMaximum(int[] values) {
if (values == null) throw new IllegalArgumentException();
this.values = values;
N = values.length;
}
// Advances the front of the window by one unit
public void advance() {
// Remove all the worse values in the back of the deque
while (!deque.isEmpty() && values[deque.peekLast()] < values[hi])
deque.removeLast(); // Change the '<' sign here ^^^ to '>' for minimum sliding window
// Add the next index to the back of the deque
deque.addLast(hi);
// Increase the window size
hi++;
}
// Retracks the back of the window by one unit
public void shrink() {
// Decrease window size by pushing it forward
lo++;
// Remove elements in the front of the queue whom are no longer
// valid in the reduced window.
while (!deque.isEmpty() && deque.peekFirst() < lo) deque.removeFirst();
}
// Query the current maximum value in the window
public int getMax() {
if (lo >= hi) throw new IllegalStateException("Make sure lo < hi");
return values[deque.peekFirst()];
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。