Created
November 17, 2016 21:06
-
-
Save mggowtham25/e1475bf024f7a23821039973ab496bb2 to your computer and use it in GitHub Desktop.
Sliding Window - Maximum of that range (Int)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Gowtham on 10/20/2016. | |
*/ | |
import java.util.ArrayList; | |
import java.util.List; | |
public class SlidingWindowMaximum { | |
private List<Integer> values = new ArrayList<Integer>(); | |
public void insertElements(int element) { | |
values.add(element); | |
} | |
public void findMin(int windowSize) { | |
for (int i = 0; i < values.size(); i = i + windowSize) { | |
int globalMinimum = Integer.MAX_VALUE; | |
for (int k = i; k < i + windowSize; k++) { | |
int v = values.get(k); | |
if (v < globalMinimum) { | |
globalMinimum = v; | |
} | |
} | |
System.out.println(globalMinimum); | |
} | |
} | |
public void findMax(int WindowSize) { | |
for (int j = 0; j < values.size(); j = j + WindowSize) { | |
int globalMaximum = Integer.MIN_VALUE; | |
for (int l = j; l < j + WindowSize; l++) { | |
int x = values.get(l); | |
if (x > globalMaximum) { | |
globalMaximum = x; | |
} | |
} | |
System.out.println(globalMaximum); | |
} | |
} | |
public static void main(String[] args) { | |
SlidingWindowMaximum obj = new SlidingWindowMaximum(); | |
obj.insertElements(5); | |
obj.insertElements(6); | |
obj.insertElements(10); | |
obj.insertElements(11); | |
obj.insertElements(1); | |
obj.insertElements(15); | |
obj.insertElements(2); | |
obj.insertElements(0); | |
obj.insertElements(44); | |
obj.findMin(3); | |
obj.findMax(3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment