Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 65b9788

Browse files
committed
sliding window added
1 parent 9ed53df commit 65b9788

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

‎AlgorithmCode/NQueen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public static void main(String[] args) throws IOException {
6666
// 1 3
6767
// 2 0
6868
// 3 2
69+
6970
if (nQueenHelper(N)) {
7071
System.out.println("\n" + "N Queen Exist on " + N + " x " + N + " Matrix" + "\n");
7172
System.out.println("points:");

‎AlgorithmCode/SlidingWindowMaximum.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* This file contain an implementation of the maximum sliding window problem. This code has been
3+
* tested against the judge data on:
4+
*
5+
* <p>https://leetcode.com/problems/sliding-window-maximum/description/
6+
*
7+
* @author William Fiset, william.alexandre.fiset@gmail.com
8+
*/
9+
10+
import java.util.ArrayDeque;
11+
import java.util.Deque;
12+
13+
public class SlidingWindowMaximum {
14+
15+
int[] values;
16+
public int N, lo, hi;
17+
18+
Deque<Integer> deque = new ArrayDeque<>();
19+
20+
public SlidingWindowMaximum(int[] values) {
21+
if (values == null) throw new IllegalArgumentException();
22+
this.values = values;
23+
N = values.length;
24+
}
25+
26+
// Advances the front of the window by one unit
27+
public void advance() {
28+
29+
// Remove all the worse values in the back of the deque
30+
while (!deque.isEmpty() && values[deque.peekLast()] < values[hi])
31+
deque.removeLast(); // Change the '<' sign here ^^^ to '>' for minimum sliding window
32+
33+
// Add the next index to the back of the deque
34+
deque.addLast(hi);
35+
36+
// Increase the window size
37+
hi++;
38+
}
39+
40+
// Retracks the back of the window by one unit
41+
public void shrink() {
42+
43+
// Decrease window size by pushing it forward
44+
lo++;
45+
46+
// Remove elements in the front of the queue whom are no longer
47+
// valid in the reduced window.
48+
while (!deque.isEmpty() && deque.peekFirst() < lo) deque.removeFirst();
49+
}
50+
51+
// Query the current maximum value in the window
52+
public int getMax() {
53+
if (lo >= hi) throw new IllegalStateException("Make sure lo < hi");
54+
return values[deque.peekFirst()];
55+
}
56+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /