We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
class Result {
public static List solve(List arr, List queries) {
List maxList = new ArrayList<>();
for (int query : queries) {
int minMax = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
// Calculate the initial maximum within the first window
for (int i = 0; i < query; i++) {
max = Math.max(max, arr.get(i));
}
minMax = Math.min(minMax, max);
// Slide the window and update the maximum
for (int i = query; i < arr.size(); i++) {
// Remove the element that goes out of the window
if (arr.get(i - query) == max) {
max = Integer.MIN_VALUE;
// Recalculate the maximum within the current window
for (int j = i - query + 1; j <= i; j++) {
max = Math.max(max, arr.get(j));
}
}
// Update the maximum with the new element
max = Math.max(max, arr.get(i));
// Update the minimum of maximums
minMax = Math.min(minMax, max);
}
maxList.add(minMax);
}
return maxList;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queries with Fixed Length
You are viewing a single comment's thread. Return to all comments →
Hey guys this is the Java15 program
class Result { public static List solve(List arr, List queries) { List maxList = new ArrayList<>();
}