这是java中的代码
import java.io.*;
import java.util.*;
public class MinInwindow {
public static void main(String[] args) {
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();
printMinInWindow(arr, n, k);
}
static void printMinInWindow(int[] arr, int n, int k) {
int start = 0, last = 0;
ArrayList<Integer> list = new ArrayList<Integer>(k);
while (last < n) {
int val = arr[last];
if (last < k) {
list = addNewINtoList(list, val);
last++;
} else {
//print minimum from window
System.out.print(list.get(0) + " ");
int startValue = arr[start];
start++;
list = removefromListIfAvailable(list, startValue);
list = addNewINtoList(list, val);
last++;
}
}
System.out.println(list.get(0) + " ");
}
static ArrayList<Integer> addNewINtoList(ArrayList<Integer> list, int val) {
if (list.isEmpty()) {
list.add(val);
} else {
int size = list.size();
while (size > 0 && list.get(size - 1) > val) {
list.remove(size - 1);
size--;
}
list.add(val);
}
return list;
}
static ArrayList<Integer> removefromListIfAvailable(ArrayList<Integer> list, int val) {
if (list.isEmpty()) {
return list;
}
int startValue = list.get(0);
if (startValue == val) {
list.remove(0);
}
return list;
}
}