提交 9b1ac5fd 编写于 作者: GreyZeng's avatar GreyZeng

update heap

上级 266965f0
......@@ -7,7 +7,7 @@ import java.util.PriorityQueue;
// 几乎有序是指,如果把数组排好顺序的话,每个元素移动的距离一定不超过k,并且k相对于数组长度来说是比较小的。
// 请选择一个合适的排序策略,对这个数组进行排序。(从小到大)
// tips: 加k个数进堆,然后再加入一个,弹出一个,最后堆里面剩下的继续弹出即可 时间复杂度:O(N*logK)
public class Code_DistanceLessK {
public class DistanceLessK {
public static void sortedArrDistanceLessK(int[] arr, int k) {
k = Math.min(arr.length - 1, k);
PriorityQueue<Integer> heap = new PriorityQueue<>();
......@@ -20,7 +20,9 @@ public class Code_DistanceLessK {
for (; i < arr.length; i++) {
heap.offer(arr[i]);
// 移动一定不会超过K次
arr[index++] = heap.poll();
if (!heap.isEmpty()) {
arr[index++] = heap.poll();
}
}
while (!heap.isEmpty()) {
arr[index++] = heap.poll();
......
package git.snippet.common;
import java.util.Arrays;
public class Generator {
// for test
// 几乎有序的数组排序测试
public static int[] randomArrayNoMoveMoreK(int maxSize, int maxValue, int K) {
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
}
// 先排个序
Arrays.sort(arr);
// 然后开始随意交换,但是保证每个数距离不超过K
// swap[i] == true, 表示i位置已经参与过交换
// swap[i] == false, 表示i位置没有参与过交换
boolean[] isSwap = new boolean[arr.length];
for (int i = 0; i < arr.length; i++) {
int j = Math.min(i + (int) (Math.random() * (K + 1)), arr.length - 1);
if (!isSwap[i] && !isSwap[j]) {
isSwap[i] = true;
isSwap[j] = true;
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
return arr;
}
// for test
public static int[] generateRandomArray(int maxSize, int maxValue) {
// Math.random() -> [0,1)
......
......@@ -7,10 +7,8 @@ import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*;
@DisplayName("几乎有序的数组排序测试")
public class Code_DistanceLessKTest {
public class DistanceLessKTest {
@Test
@DisplayName("几乎有序的数组排序测试")
......@@ -20,39 +18,12 @@ public class Code_DistanceLessKTest {
int maxValue = 100;
for (int i = 0; i < testTime; i++) {
int k = (int) (Math.random() * maxSize) + 1;
int[] arr = randomArrayNoMoveMoreK(maxSize, maxValue, k);
int[] arr = Generator.randomArrayNoMoveMoreK(maxSize, maxValue, k);
int[] arr1 = Generator.copyArray(arr);
int[] arr2 = Generator.copyArray(arr);
Code_DistanceLessK.sortedArrDistanceLessK(arr1, k);
DistanceLessK.sortedArrDistanceLessK(arr1, k);
Arrays.sort(arr2);
Assertions.assertArrayEquals(arr1, arr2);
}
}
// for test
public static int[] randomArrayNoMoveMoreK(int maxSize, int maxValue, int K) {
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
}
// 先排个序
Arrays.sort(arr);
// 然后开始随意交换,但是保证每个数距离不超过K
// swap[i] == true, 表示i位置已经参与过交换
// swap[i] == false, 表示i位置没有参与过交换
boolean[] isSwap = new boolean[arr.length];
for (int i = 0; i < arr.length; i++) {
int j = Math.min(i + (int) (Math.random() * (K + 1)), arr.length - 1);
if (!isSwap[i] && !isSwap[j]) {
isSwap[i] = true;
isSwap[j] = true;
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
return arr;
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册