You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.7 KiB

2 years ago
package class16;
import java.util.Arrays;
public class Code03_MinPatches {
// arr请保证有序且正数 1~aim
public static int minPatches(int[] arr, int aim) {
int patches = 0; // 缺多少个数字
long range = 0; // 已经完成了1 ~ range的目标
Arrays.sort(arr);
for (int i = 0; i != arr.length; i++) {
// arr[i]
// 要求1 ~ arr[i]-1 范围被搞定!
while (arr[i] - 1 > range) { // arr[i] 1 ~ arr[i]-1
range += range + 1; // range + 1 是缺的数字
patches++;
if (range >= aim) {
return patches;
}
}
// 要求被满足了!
range += arr[i];
if (range >= aim) {
return patches;
}
}
while (aim >= range + 1) {
range += range + 1;
patches++;
}
return patches;
}
// 嘚瑟
public static int minPatches2(int[] arr, int K) {
int patches = 0; // 缺多少个数字
int range = 0; // 已经完成了1 ~ range的目标
for (int i = 0; i != arr.length; i++) {
// 1~range
// 1 ~ arr[i]-1
while (arr[i] > range + 1) { // arr[i] 1 ~ arr[i]-1
if (range > Integer.MAX_VALUE - range - 1) {
return patches + 1;
}
range += range + 1; // range + 1 是缺的数字
patches++;
if (range >= K) {
return patches;
}
}
if (range > Integer.MAX_VALUE - arr[i]) {
return patches;
}
range += arr[i];
if (range >= K) {
return patches;
}
}
while (K >= range + 1) {
if (K == range && K == Integer.MAX_VALUE) {
return patches;
}
if (range > Integer.MAX_VALUE - range - 1) {
return patches + 1;
}
range += range + 1;
patches++;
}
return patches;
}
public static void main(String[] args) {
int[] test = { 1, 2, 31, 33 };
int n = 2147483647;
System.out.println(minPatches(test, n));
}
}