Skip to content

Commit 8e31e20

Browse files
committed
modify code
1 parent c8ae9f5 commit 8e31e20

7 files changed

+45
-28
lines changed
48.8 KB
Binary file not shown.

src/class092/Code01_MinimizeDeviation.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
public class Code01_MinimizeDeviation {
1818

1919
public static int minimumDeviation(int[] nums) {
20+
// 有序表可以查询最大值、最小值
2021
TreeSet<Integer> set = new TreeSet<>();
2122
for (int num : nums) {
22-
set.add(num % 2 == 0 ? num : num * 2);
23+
if (num % 2 == 0) {
24+
set.add(num);
25+
} else {
26+
set.add(num * 2);
27+
}
2328
}
2429
int ans = set.last() - set.first();
2530
while (ans > 0 && set.last() % 2 == 0) {

src/class092/Code02_RabbitsInForest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,29 @@
44

55
// 森林中的兔子
66
// 森林中有未知数量的兔子
7-
// 提问其中若干只兔子 "还有多少只兔子与你颜色相同?"
8-
// 将答案收集到一个整数数组answers中
9-
// 其中answers[i]是第i只兔子的回答
10-
// 每只兔子都不会说谎,返回森林中兔子的最少数量
7+
// 你问兔子们一个问题: "还有多少只兔子与你颜色相同?"
8+
// 你将答案收集到了一个数组answers中
9+
// 你可能没有收集到所有兔子的回答,可能只是一部分兔子的回答
10+
// 其中answers[i]是第i只兔子的答案
11+
// 所有兔子都不会说错,返回森林中兔子的最少数量
1112
// 测试链接 : https://leetcode.cn/problems/rabbits-in-forest/
1213
public class Code02_RabbitsInForest {
1314

1415
public static int numRabbits(int[] arr) {
16+
// a / b 向上取整 -> (a + b - 1) / b
1517
Arrays.sort(arr);
16-
int x = arr[0];
17-
int cnt = 1;
18+
int n = arr.length;
1819
int ans = 0;
19-
for (int i = 1; i < arr.length; i++) {
20-
if (x != arr[i]) {
21-
ans += ((cnt + x) / (x + 1)) * (x + 1);
22-
x = arr[i];
23-
cnt = 1;
24-
} else {
25-
cnt++;
20+
for (int i = 0, j = 1, x; i < n; j++) {
21+
x = arr[i];
22+
while (j < n && x == arr[j]) {
23+
j++;
2624
}
25+
// i...j-1 都是同一种答案,当前组有j-i个回答
26+
ans += (j - i + x) / (x + 1) * (x + 1);
27+
i = j;
2728
}
28-
return ans + ((cnt + x) / (x + 1)) * (x + 1);
29+
return ans;
2930
}
3031

3132
}

src/class092/Code03_MinimumOperationsMakeSimilar.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
// 令 nums[j] = nums[j] - 2
1111
// 如果两个数组中每个元素出现的频率相等,我们称两个数组是 相似 的
1212
// 请你返回将 nums 变得与 target 相似的最少操作次数
13-
// 测试数据保证 nums 一定能变得与 target 相似
13+
// 测试数据保证nums一定能变得与target相似
1414
// 测试链接 : https://leetcode.cn/problems/minimum-number-of-operations-to-make-arrays-similar/
1515
public class Code03_MinimumOperationsMakeSimilar {
1616

1717
public static long makeSimilar(int[] nums, int[] target) {
1818
int n = nums.length;
19-
int oddSize = split(nums);
20-
split(target);
19+
int oddSize = split(nums, n);
20+
split(target, n);
2121
Arrays.sort(nums, 0, oddSize);
2222
Arrays.sort(nums, oddSize, n);
2323
Arrays.sort(target, 0, oddSize);
@@ -26,17 +26,19 @@ public static long makeSimilar(int[] nums, int[] target) {
2626
for (int i = 0; i < n; i++) {
2727
ans += Math.abs((long) nums[i] - target[i]);
2828
}
29-
return ans >> 2;
29+
return ans / 4;
3030
}
3131

32-
public static int split(int[] arr) {
33-
int line = 0;
34-
for (int i = 0; i < arr.length; i++) {
35-
if ((arr[i] & 1) != 0) {
36-
swap(arr, i, line++);
32+
// 把数组分割成左部分全是奇数,右部分全是偶数
33+
// 返回左部分的长度
34+
public static int split(int[] arr, int n) {
35+
int oddSize = 0;
36+
for (int i = 0; i < n; i++) {
37+
if ((arr[i] & 1) == 1) {
38+
swap(arr, i, oddSize++);
3739
}
3840
}
39-
return line;
41+
return oddSize;
4042
}
4143

4244
public static void swap(int[] arr, int i, int j) {

src/class092/Code04_Quiz.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public static void main(String[] args) throws IOException {
5151

5252
public static int compute() {
5353
Arrays.sort(nums, 0, n, (a, b) -> Math.abs(a[0] - a[1]) - Math.abs(b[0] - b[1]));
54-
int maxA = nums[0][0];
55-
int maxB = nums[0][1];
54+
int maxA = nums[0][0]; // 左边最大的推理能力
55+
int maxB = nums[0][1]; // 左边最大的阅读能力
5656
int ans = 0;
5757
for (int i = 1; i < n; i++) {
5858
if (nums[i][0] <= nums[i][1]) {

src/class092/Code05_DivideArrayIncreasingSequences.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package class092;
22

33
// 将数组分成几个递增序列
4-
// 给你一个 非递减 的正整数数组 nums 和整数 K
4+
// 给你一个有序的正数数组 nums 和整数 K
55
// 判断该数组是否可以被分成一个或几个 长度至少 为 K 的 不相交的递增子序列
6+
// 数组中的所有数字,都要被,若干不相交的递增子序列包含
67
// 测试链接 : https://leetcode.cn/problems/divide-array-into-increasing-sequences/
78
public class Code05_DivideArrayIncreasingSequences {
89

910
public static boolean canDivideIntoSubsequences(int[] nums, int k) {
1011
int cnt = 1;
12+
// maxCnt : 最大词频
1113
int maxCnt = 1;
14+
// 在有序数组中,求某个数的最大词频
1215
for (int i = 1; i < nums.length; i++) {
1316
if (nums[i - 1] != nums[i]) {
1417
maxCnt = Math.max(maxCnt, cnt);
@@ -18,6 +21,8 @@ public static boolean canDivideIntoSubsequences(int[] nums, int k) {
1821
}
1922
}
2023
maxCnt = Math.max(maxCnt, cnt);
24+
// 向下取整如果满足 >= k
25+
// 那么所有的递增子序列长度一定 >= k
2126
return nums.length / maxCnt >= k;
2227
}
2328

src/class092/Code06_MinimumNumberRefuelingStops.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public static int minRefuelStops(int target, int startFuel, int[][] stations) {
2020
if (startFuel >= target) {
2121
return 0;
2222
}
23+
// 大根堆
2324
PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);
25+
// 包括初始油量 + 沿途加的油,能让你达到什么位置,to
2426
int to = startFuel;
2527
int cnt = 0;
2628
for (int[] station : stations) {
@@ -40,6 +42,8 @@ public static int minRefuelStops(int target, int startFuel, int[][] stations) {
4042
}
4143
heap.add(fuel);
4244
}
45+
// 代码能走到这里,说明还没到达target
46+
// 如果还有油,看看能不能冲到target
4347
while (!heap.isEmpty()) {
4448
to += heap.poll();
4549
cnt++;

0 commit comments

Comments
 (0)