-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33.)SlidingWindowMaximum.java
More file actions
84 lines (77 loc) · 2.19 KB
/
33.)SlidingWindowMaximum.java
File metadata and controls
84 lines (77 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Sliding window maximum brute force TC - O(N*k)
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static int[] maxSlidingWindow(int arr[],int k){
int n = arr.length;
int ans[] = new int[n-k+1];
for(int i =0;i<n-k+1;i++){
int maxVal = Integer.MIN_VALUE;
for(int j= 1;j<i+k;j++){
maxVal = Math.max(maxVal,arr[j]);
}
ans[i] = maxVal;
}
return ans;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
int k = sc.nextInt();
int ans[] = maxSlidingWindow(arr,k);
System.out.println(Arrays.toString(ans));
}
}
//Better approch sliding windwo stack
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
int[] nge = new int[n]; // Next Greater Element index array
Stack<Integer> st = new Stack<>();
// Step 1: NGE array banao
for (int i = n - 1; i >= 0; i--) {
while (!st.isEmpty() && nums[st.peek()] <= nums[i]) {
st.pop();
}
nge[i] = st.isEmpty() ? n : st.peek();
st.push(i);
}
// Step 2: Har window ka max निकालो
int[] res = new int[n - k + 1];
int j = 0; // current max ka index
for (int i = 0; i <= n - k; i++) {
if (j < i) j = i; // agar max index window ke bahar chala gaya
// jab tak window ke andar NGE hai, udhar jump kar jao
while (nge[j] < i + k) {
j = nge[j];
}
res[i] = nums[j];
}
return res;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
int k = sc.nextInt();
int ans[] = maxSlidingWindow(arr,k);
System.out.println(Arrays.toString(ans));
}
}