Skip to content

Commit 92199dd

Browse files
committed
modify code
1 parent d3aac13 commit 92199dd

File tree

8 files changed

+132
-6
lines changed

8 files changed

+132
-6
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package class090;
2+
3+
// 分成k份的最大乘积
4+
// 一个数字n一定要分成k份,得到的乘积尽量大是多少
5+
// 数字n和k,可能非常大,到达10^12规模
6+
// 结果可能更大,所以返回结果对1000000007取模
7+
// 没有在线测试,对数器验证
8+
public class Code02_MaxProduct {
9+
10+
// 暴力递归
11+
// 为了验证
12+
public static int maxValue1(int n, int k) {
13+
return f1(n, k);
14+
}
15+
16+
// 剩余的数字rest拆成k份
17+
// 返回最大乘积
18+
// 暴力尝试一定能得到最优解
19+
public static int f1(int rest, int k) {
20+
if (k == 1) {
21+
return rest;
22+
}
23+
int ans = Integer.MIN_VALUE;
24+
for (int cur = 1; cur <= rest && (rest - cur) >= (k - 1); cur++) {
25+
int curAns = cur * f1(rest - cur, k - 1);
26+
ans = Math.max(ans, curAns);
27+
}
28+
return ans;
29+
}
30+
31+
// 贪心的解
32+
// 这是最优解
33+
// 如果结果很大,那么求余数
34+
public static int maxValue2(long n, long k) {
35+
if (k == 0 || n < k) {
36+
return -1;
37+
}
38+
int mod = 1000000007;
39+
long a = n / k;
40+
long b = n % k;
41+
long part1 = power(a + 1, b, mod);
42+
long part2 = power(a, k - b, mod);
43+
return (int) (part1 * part2) % mod;
44+
}
45+
46+
// 返回a的n次方%mod的结果
47+
public static long power(long a, long n, int mod) {
48+
long ans = 1;
49+
long tmp = a;
50+
while (n != 0) {
51+
if ((n & 1) != 0) {
52+
ans = (ans * tmp) % mod;
53+
}
54+
n >>= 1;
55+
tmp = (tmp * tmp) % mod;
56+
}
57+
return ans;
58+
}
59+
60+
// 对数器
61+
// 为了验证
62+
public static void main(String[] args) {
63+
int N = 30;
64+
int testTimes = 2000;
65+
System.out.println("测试开始");
66+
for (int i = 1; i <= testTimes; i++) {
67+
int n = (int) (Math.random() * N) + 1;
68+
int k = (int) (Math.random() * n) + 1;
69+
int ans1 = maxValue1(n, k);
70+
int ans2 = maxValue2(n, k);
71+
if (ans1 != ans2) {
72+
// 如果出错了
73+
// 可以增加打印行为找到一组出错的例子
74+
// 然后去debug
75+
System.out.println("出错了!");
76+
}
77+
if (i % 100 == 0) {
78+
System.out.println("测试到第" + i + "组");
79+
}
80+
}
81+
System.out.println("测试结束");
82+
}
83+
84+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package class092;
2+
3+
import java.util.Arrays;
4+
5+
// 消灭怪物的最大数量
6+
// 你正在玩一款电子游戏,在游戏中你需要保护城市免受怪物侵袭
7+
// 给定一个下标从0开始且大小为n的整数数组dist
8+
// 其中dist[i]是第i个怪物与城市的初始距离
9+
// 怪物以恒定的速度走向城市,每个怪物的速度都以一个长度为n的整数数组speed表示
10+
// 其中speed[i]是第i个怪物的速度
11+
// 你有一种武器,一旦充满电,就可以消灭一个怪物
12+
// 但是,武器需要1的时间才能充电完成
13+
// 武器在游戏开始时是充满电的状态,怪物从0时刻开始移动
14+
// 一旦任一怪物到达城市,你就输掉了这场游戏
15+
// 如果某个怪物恰好在某一分钟开始时到达城市,这也会被视为输掉游戏
16+
// 在你可以使用武器之前,游戏就会结束
17+
// 返回在你输掉游戏前可以消灭的怪物的最大数量
18+
// 如果你可以在所有怪物到达城市前将它们全部消灭返回n
19+
// 测试链接 : https://leetcode.cn/problems/eliminate-maximum-number-of-monsters/
20+
public class Code01_EliminateMaximumMonsters {
21+
22+
public static int eliminateMaximum(int[] dist, int[] speed) {
23+
int n = dist.length;
24+
int[] come = new int[n];
25+
for (int i = 0; i < n; i++) {
26+
come[i] = (dist[i] + speed[i] - 1) / speed[i];
27+
}
28+
Arrays.sort(come);
29+
for (int i = 0; i < n; i++) {
30+
if (come[i] <= i) {
31+
return i;
32+
}
33+
}
34+
return n;
35+
}
36+
37+
}

src/class092/Code01_MinimizeDeviation.java renamed to src/class092/Code02_MinimizeDeviation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// 数组的偏移量是数组中任意两个元素之间的最大差值
1515
// 返回数组在执行某些操作之后可以拥有的最小偏移量
1616
// 测试链接 : https://leetcode.cn/problems/minimize-deviation-in-array/
17-
public class Code01_MinimizeDeviation {
17+
public class Code02_MinimizeDeviation {
1818

1919
public static int minimumDeviation(int[] nums) {
2020
TreeSet<Integer> set = new TreeSet<>();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// 将答案收集到一个整数数组answers中,其中answers[i]是第i只兔子的回答
99
// 给你数组 answers ,返回森林中兔子的最少数量
1010
// 测试链接 : https://leetcode.cn/problems/rabbits-in-forest/
11-
public class Code02_RabbitsInForest {
11+
public class Code03_RabbitsInForest {
1212

1313
public static int numRabbits(int[] arr) {
1414
if (arr == null || arr.length == 0) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// 你有k个非递减排列的整数列表
88
// 找到一个最小区间,使得k个列表中的每个列表至少有一个数包含在其中
99
// 测试链接 : https://leetcode.cn/problems/smallest-range-covering-elements-from-k-lists/
10-
public class Code03_SmallestRange {
10+
public class Code04_SmallestRange {
1111

1212
public static int[] smallestRange(List<List<Integer>> nums) {
1313
int n = nums.size();

src/class091/Code07_LargestPalindromicNumber.java renamed to src/class092/Code07_LargestPalindromicNumber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package class091;
1+
package class092;
22

33
// 最大回文数字
44
// 给你一个仅由数字(0 - 9)组成的字符串num

src/class093/Code04.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package class093;
2+
3+
public class Code04 {
4+
5+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package class092;
1+
package class093;
22

33
// 梦之城
44
// 给定n棵树,和两个长度为n的数组a和b
@@ -18,7 +18,7 @@
1818
import java.io.StreamTokenizer;
1919
import java.util.Arrays;
2020

21-
public class Code04_DreamCity {
21+
public class Code05_DreamCity {
2222

2323
public static int[][] tree = new int[250][2];
2424

0 commit comments

Comments
 (0)