Skip to content

Commit d14893c

Browse files
committed
modify code on class
1 parent 6c437d2 commit d14893c

File tree

7 files changed

+197
-12
lines changed

7 files changed

+197
-12
lines changed

src/followup/Code01_Cola.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public static int buy(int[] qian, int[] zhang, int rest) {
5656

5757
// 正式的方法
5858
public static int putTimes(int m, int a, int b, int c, int x) {
59-
// 0 1 2
59+
// 0 1 2
6060
int[] qian = { 100, 50, 10 };
61-
int[] zhang = { c, b, a };
61+
int[] zhang = { c, b, a };
6262
// 总共需要多少次投币
6363
int puts = 0;
6464
// 之前面值的钱还剩下多少总钱数
@@ -74,6 +74,10 @@ public static int putTimes(int m, int a, int b, int c, int x) {
7474
// 那么当前面值参与搞定第一瓶可乐,需要掏出多少张呢?就是curQianFirstBuyZhang
7575
int curQianFirstBuyZhang = (x - preQianRest + qian[i] - 1) / qian[i];
7676
if (zhang[i] >= curQianFirstBuyZhang) { // 如果之前的钱和当前面值的钱,能凑出第一瓶可乐
77+
// 买!
78+
// preQianRest + qian[i] * curQianFirstBuyZhang 第一次买可乐的总钱数
79+
// x
80+
// preQianRest + qian[i] * curQianFirstBuyZhang - x 1
7781
// 凑出来了一瓶可乐也可能存在找钱的情况,
7882
giveRest(qian, zhang, i + 1, (preQianRest + qian[i] * curQianFirstBuyZhang) - x, 1);
7983
puts += curQianFirstBuyZhang + preQianZhang;
@@ -109,6 +113,9 @@ public static int putTimes(int m, int a, int b, int c, int x) {
109113
return m == 0 ? puts : -1;
110114
}
111115

116+
// 假设单次买可乐,每次零钱是oneTimeRest
117+
// 一共买了times次可乐,谁买的? i-1号面值买的
118+
// i... 可能获得零钱,请把张数调整对
112119
public static void giveRest(int[] qian, int[] zhang, int i, int oneTimeRest, int times) {
113120
for (; i < 3; i++) {
114121
zhang[i] += (oneTimeRest / qian[i]) * times;

src/followup/Code02_Drive.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,26 @@ public class Code02_Drive {
1616
// 一定要让A区域分到N/2个司机,让B区域也分到N/2个司机
1717
// 返回最大的总收益
1818
public static int maxMoney(int[][] matrix) {
19-
return process(matrix, 0, matrix.length / 2);
19+
// 0.....
20+
// N A N/2 B N/2
21+
return process2(matrix, 0, matrix.length / 2);
2022
}
2123

24+
// int[][] matrix N * 2 大小的
25+
// i A : matrix[i][0] B : matrix[i][1]
26+
// 0..i-1司机,已经做完选择了,不用再操心了,
2227
// 从i开始到最后所有的司机,在A区域还有aRest个名额的情况下,返回最优分配的收益
2328
public static int process(int[][] matrix, int i, int aRest) {
2429
if (aRest < 0) {
2530
return -1;
2631
}
32+
// aRest >= 0 N A 耗尽 A B
2733
if (i == matrix.length) {
2834
return aRest == 0 ? 0 : -1;
2935
}
36+
// aRest >= 0 && 还有司机选
3037
int goToA = -1;
38+
// 当前司机 i 决定去A区域之后,后续过程最好的收益,nextA
3139
int nextA = process(matrix, i + 1, aRest - 1);
3240
if (nextA != -1) {
3341
goToA = matrix[i][0] + nextA;
@@ -39,6 +47,25 @@ public static int process(int[][] matrix, int i, int aRest) {
3947
}
4048
return Math.max(goToA, goToB);
4149
}
50+
51+
52+
53+
public static int process2(int[][] matrix, int i, int aRest) {
54+
if (i == matrix.length) {
55+
return aRest == 0 ? 0 : -1;
56+
}
57+
int goToA = -1;
58+
if(aRest > 0) {
59+
goToA = matrix[i][0] + process2(matrix, i+1, aRest - 1);
60+
}
61+
int goToB = -1;
62+
int goAs = (matrix.length / 2) - aRest;
63+
int goBs = i - goAs;
64+
if(goBs < (matrix.length / 2)) {
65+
goToB = matrix[i][1] + process2(matrix, i+1, aRest);
66+
}
67+
return Math.max(goToA, goToB);
68+
}
4269

4370
public static void main(String[] args) {
4471
int[][] matrix = { { 10, 20 }, { 20, 40 } };

src/followup/Code03_Array.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static int findError(String[] contents) {
2727
return 0;
2828
}
2929

30+
// str -> " [[[7]]] " 返回什么
3031
public static Integer value(String str, int[] arr) {
3132
int value = Integer.valueOf(str.replace("[", "").replace("]", ""));
3233
int levels = str.lastIndexOf("[") + 1;
@@ -40,9 +41,20 @@ public static Integer value(String str, int[] arr) {
4041
}
4142

4243
public static void main(String[] args) {
43-
String[] contents = { "arr[7]", "arr[0]=6", "arr[1]=3", "arr[2]=1", "arr[3]=2", "arr[4]=4", "arr[5]=0",
44-
"arr[6]=5", "arr[arr[1]]=3", "arr[arr[arr[arr[5]]]]=arr[arr[arr[3]]]", "arr[arr[4]]=arr[arr[arr[0]]]",
45-
"arr[arr[1]] = 7", "arr[0] = arr[arr[arr[1]]]" };
44+
String[] contents = {
45+
"arr[7]",
46+
"arr[0]=6",
47+
"arr[1]=3",
48+
"arr[2]=1",
49+
"arr[3]=2",
50+
"arr[4]=4",
51+
"arr[5]=0",
52+
"arr[6]=5",
53+
"arr[arr[1]]=3",
54+
"arr[arr[arr[arr[5]]]]=arr[arr[arr[3]]]",
55+
"arr[arr[4]]=arr[arr[arr[0]]]",
56+
"arr[arr[1]] = 7",
57+
"arr[0] = arr[arr[arr[1]]]" };
4658
System.out.println(findError(contents));
4759

4860
}

src/followup/Code04_Painting.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
public class Code04_Painting {
77
// N * M的棋盘
8+
// 所有格子必须染色
89
// 每种颜色的格子数必须相同的
910
// 相邻格子染的颜色必须不同
10-
// 所有格子必须染色
1111
// 返回至少多少种颜色可以完成任务
1212

13+
// 暴力解
1314
public static int minColors(int N, int M) {
14-
for (int i = 2; i < N * M; i++) {
15+
for (int i = 2; i <= N * M; i++) {
1516
int[][] matrix = new int[N][M];
1617
// 下面这一句可知,需要的最少颜色数i,一定是N*M的某个因子
1718
if ((N * M) % i == 0 && can(matrix, N, M, i)) {
@@ -59,8 +60,8 @@ public static boolean process(int[][] matrix, int N, int M, int pNum, int row, i
5960

6061
public static void main(String[] args) {
6162
// 根据代码16行的提示,打印出答案,看看是答案是哪个因子
62-
for (int N = 2; N < 10; N++) {
63-
for (int M = 2; M < 10; M++) {
63+
for (int N = 2; N < 11; N++) {
64+
for (int M = 2; M < 11; M++) {
6465
System.out.println("N = " + N);
6566
System.out.println("M = " + M);
6667
System.out.println("ans = " + minColors(N, M));

src/followup/Code05_CardsProblem.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* 第一张第三个属性为"C"、第二张第三个属性为"C"、第三张第三个属性为"C",全一样
1616
* 每种属性都满足在三张扑克中全一样,或全不一样,所以这三张扑克达标
1717
* 返回在cards[]中任意挑选三张扑克,达标的方法数
18-
*
1918
* */
2019
public class Code05_CardsProblem {
2120

@@ -64,6 +63,8 @@ public static int ways2(String[] cards) {
6463
ways += n == 3 ? 1 : (n * (n - 1) * (n - 2) / 6);
6564
}
6665
}
66+
// 依次选出 第一个状态 第二个状态 第三个状态
67+
// 15 6 21
6768
LinkedList<Integer> path = new LinkedList<>();
6869
for (int i = 0; i < 27; i++) {
6970
if (counts[i] != 0) {

src/followup/KN.java

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package followup;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
6+
/*
7+
*
8+
* 已知数组中其他数都出现了N次,只有一种数出现了K次
9+
* 怎么找到出现了K次的数?做到时间复杂度O(N),额外空间复杂度O(1)
10+
* 规定:N > 1,K > 0,K < N
11+
*
12+
* */
13+
14+
public class KN {
15+
16+
public static int get1(int[] arr, int k, int n) {
17+
HashMap<Integer, Integer> map = new HashMap<>();
18+
for (int num : arr) {
19+
if (!map.containsKey(num)) {
20+
map.put(num, 1);
21+
} else {
22+
map.put(num, map.get(num) + 1);
23+
}
24+
}
25+
for (Integer key : map.keySet()) {
26+
if (map.get(key) == k) {
27+
return key;
28+
}
29+
}
30+
return -1;
31+
}
32+
33+
public static int get2(int[] arr, int k, int n) {
34+
int[] count = new int[32];
35+
for (int num : arr) {
36+
for (int i = 0; i < 32; i++) {
37+
if ((num & (1 << i)) != 0) {
38+
count[i]++;
39+
}
40+
}
41+
}
42+
for (int i = 0; i < count.length; i++) {
43+
count[i] = (count[i] % n) / k;
44+
}
45+
int ans = 0;
46+
for (int i = 0; i < count.length; i++) {
47+
ans |= (count[i] << i);
48+
}
49+
return ans;
50+
}
51+
52+
public static int[] randomArray(int k, int n, int type) {
53+
HashSet<Integer> set = new HashSet<>();
54+
while (set.size() != type - 1) {
55+
set.add((int) (Math.random() * 10000) - (int) (Math.random() * 10000));
56+
}
57+
int knum = 0;
58+
do {
59+
knum = (int) (Math.random() * 10000) - (int) (Math.random() * 10000);
60+
} while (set.contains(knum));
61+
int[] arr = new int[(type - 1) * n + k];
62+
int index = 0;
63+
for (int num : set) {
64+
for (int i = 0; i < n; i++) {
65+
arr[index++] = num;
66+
}
67+
}
68+
for (int i = 0; i < k; i++) {
69+
arr[index++] = knum;
70+
}
71+
for (int i = arr.length - 1; i >= 0; i--) {
72+
int j = (int) (Math.random() * (i + 1));
73+
int tmp = arr[i];
74+
arr[i] = arr[j];
75+
arr[j] = tmp;
76+
}
77+
return arr;
78+
}
79+
80+
public static boolean check(int[] arr, int k, int n, int type) {
81+
HashMap<Integer, Integer> map = new HashMap<>();
82+
for (int num : arr) {
83+
if (!map.containsKey(num)) {
84+
map.put(num, 1);
85+
} else {
86+
map.put(num, map.get(num) + 1);
87+
}
88+
}
89+
int kcount = 0;
90+
int ncount = 0;
91+
for (Integer key : map.keySet()) {
92+
if (map.get(key) == n) {
93+
ncount++;
94+
} else if (map.get(key) == k) {
95+
kcount++;
96+
} else {
97+
return false;
98+
}
99+
}
100+
return map.size() == type && kcount == 1 && ncount == type - 1;
101+
}
102+
103+
public static void main(String[] args) {
104+
int knMax = 20;
105+
int typeMax = 50;
106+
int testTimes = 100000;
107+
System.out.println("test begin");
108+
for (int i = 0; i < testTimes; i++) {
109+
int k = 0;
110+
int n = 0;
111+
do {
112+
k = (int) (Math.random() * knMax) + 1;
113+
n = (int) (Math.random() * knMax) + 2;
114+
} while (k >= n);
115+
int type = (int) (Math.random() * typeMax) + 5;
116+
int[] arr = randomArray(k, n, type);
117+
if (!check(arr, k, n, type)) {
118+
System.out.println("random arr error!");
119+
}
120+
int ans1 = get1(arr, k, n);
121+
int ans2 = get2(arr, k, n);
122+
if (ans1 != ans2) {
123+
System.out.println("Oops!");
124+
}
125+
}
126+
System.out.println("test end");
127+
}
128+
129+
}

src/topinterviewquestions/Problem_0395_LongestSubstringWithAtLeastKRepeatingCharacters.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ public static int longestSubstring2(String s, int k) {
3131
int N = str.length;
3232
int max = 0;
3333
for (int require = 1; require <= 26; require++) {
34+
// a~z a~z 出现次数
35+
// count[0 1 2] a b c
3436
int[] count = new int[26];
37+
// 目前窗口内收集了几种字符了
3538
int collect = 0;
39+
// 目前窗口内出现次数>=k次的字符,满足了几种
3640
int satisfy = 0;
41+
// 窗口右边界
3742
int R = -1;
38-
for (int L = 0; L < N; L++) {
43+
for (int L = 0; L < N; L++) { // L要尝试每一个窗口的最左位置
44+
// [L..R] R+1
3945
while (R + 1 < N && !(collect == require && count[str[R + 1] - 'a'] == 0)) {
4046
R++;
4147
if (count[str[R] - 'a'] == 0) {
@@ -46,9 +52,11 @@ public static int longestSubstring2(String s, int k) {
4652
}
4753
count[str[R] - 'a']++;
4854
}
55+
// [L...R]
4956
if (satisfy == require) {
5057
max = Math.max(max, R - L + 1);
5158
}
59+
// L++
5260
if (count[str[L] - 'a'] == 1) {
5361
collect--;
5462
}

0 commit comments

Comments
 (0)