Пример:
Код: Выделить всё
arr = [1, 3, 2, 1], m = 3, k = 2
m represents a group size
k represents the kth minimum element in the group of size m
so find the 2nd minimum element in a group of size 3.
There are 2 contiguous groups possible:
[1, 3, 2] and [3, 2, 1]
k = 2 , 2nd minimum element in above groups is [2,2]
Код: Выделить всё
k, m , n are of size 1 to 10^5
array item is of size 1 to 10^9
Подход 1:
Это мой наивный подход:
Код: Выделить всё
public static int[] solve(int[] arr, int m, int k) {
List result = new ArrayList();
int n = arr.length;
for(int i=0; i Integer.compare(b, a));
for (int i = 0; i < m; i++) {
q.add(arr[i]);
if (q.size() > k) {
q.poll();
}
}
result[0] = q.peek();
for (int i = 1; i
Подробнее здесь: [url]https://stackoverflow.com/questions/78180982/find-min-items-in-each-group-of-array[/url]