modify code

master
algorithmzuo 3 years ago
parent 5c3bfa5c57
commit 08026a3cdc

@ -15,6 +15,43 @@ package class_2022_07_4_week;
// 因为达成不了要求
public class Code01_WaysWiggle {
// public static int ways(int[] arr) {
// // 是否天然
//
// int n = arr.length;
// // leftUp[i] i从右往左看能不能先升再降连到开头
// boolean[] leftUp = new boolean[n];
// boolean[] leftDown = new boolean[n];
// boolean[] rightUp = new boolean[n];
// boolean[] rightDown = new boolean[n];
//
// // 欠缺生成的过程!
//
// // i == 0 [0] X
// int ans = rightUp[1] || rightDown[1] ? 1 : 0;
// // i == n-1 [n-1] X
// ans += leftUp[n - 2] || leftDown[n - 2] ? 1 : 0;
// for (int i = 1; i < n - 1; i++) {
// int left = i - 1;
// int right = i + 1;
// if (arr[left] == arr[right]) {
// continue;
// }
// // 7 X 3
// if (arr[left] > arr[right]) {
// if (rightUp[right] && leftDown[left]) {
// ans++;
// }
// } else {
// // 3 x 7
// if (rightDown[right] && leftUp[left]) {
// ans++;
// }
// }
// }
// return ans;
// }
// 暴力方法
// 为了验证
public static int ways1(int[] arr) {
@ -91,27 +128,40 @@ public class Code01_WaysWiggle {
return 0;
}
int n = arr.length;
boolean[] up = new boolean[n];
boolean[] down = new boolean[n];
up[n - 1] = true;
down[n - 1] = true;
boolean[] rightUp = new boolean[n];
boolean[] rightDown = new boolean[n];
rightUp[n - 1] = true;
rightDown[n - 1] = true;
for (int i = n - 2; i >= 0; i--) {
up[i] = arr[i] < arr[i + 1] && down[i + 1];
down[i] = arr[i] > arr[i + 1] && up[i + 1];
rightUp[i] = arr[i] < arr[i + 1] && rightDown[i + 1];
rightDown[i] = arr[i] > arr[i + 1] && rightUp[i + 1];
}
if (up[0] || down[0]) {
// 数组是不是天然符合!
if (rightUp[0] || rightDown[0]) {
return 0;
}
int ans = (up[1] || down[1]) ? 1 : 0;
// 删掉0位置的数数组达标还是不达标
// 1 升
// 1 降
int ans = (rightUp[1] || rightDown[1]) ? 1 : 0;
// ...[0]
boolean leftUp = true;
boolean leftDown = true;
boolean tmp;
for (int i = 1, l = 0, r = 2; i < n - 1; i++, l++, r++) {
ans += (arr[l] > arr[r] && up[r] && leftDown) || (arr[l] < arr[r] && down[r] && leftUp) ? 1 : 0;
ans += (arr[l] > arr[r] && rightUp[r] && leftDown) || (arr[l] < arr[r] && rightDown[r] && leftUp) ? 1 : 0;
// i两个信息 i+1
// 变量复用
// boolean curLeftUp = arr[l] > arr[i] && leftDown;
// boolean curLeftDown = arr[l] < arr[i] && leftUp;
// leftUp = curLeftUp;
// leftDown = curLeftDown;
tmp = leftUp;
// 7 4
leftUp = arr[l] > arr[i] && leftDown;
leftDown = arr[l] < arr[i] && tmp;
}
// 单独算一下 删掉n-1位置数的时候
ans += leftUp || leftDown ? 1 : 0;
return ans == 0 ? -1 : ans;
}

@ -0,0 +1,155 @@
package class_2022_07_4_week;
// 你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度。
// 你要用 所有的火柴棍 拼成一个正方形。
// 你 不能折断 任何一根火柴棒,但你可以把它们连在一起,而且每根火柴棒必须 使用一次 。
// 如果你能拼出正方形,则返回 true ,否则返回 false 。
// 测试链接 : https://leetcode.cn/problems/matchsticks-to-square/
public class Code02_MatchsticksToSquare {
// 利用数组里的火柴,必须都使用
// 能不能拼出正方形
public static boolean yesOrNo(int[] arr) {
long sum = 0;
for (int num : arr) {
sum += num;
}
if (sum % 4 != 0) {
return false;
}
long len = sum / 4;
// 最多15根
// 00000000000.....000000000000000
return process(arr, 0, 0, len, 4);
}
// 所有火柴都在arr里
// 哪些火柴用了、哪些火柴没用的状态都在status里
// sum: 当前耕耘的这条边长度已经达成了sum
// len: 固定参数,每条边必须都达成这个长度
// edges: 还剩几条边,没填完
// 返回:最终能不能达成正方形的目标
// 看似三个可变参数status, sum, edges
// 其实只有一个status
// 15位状态2^15 -> 32...
public static boolean process(int[] arr, int status, long sum, long len, int edges) {
if (edges == 0) {
return status == (1 << arr.length) - 1 ? true : false;
}
// 剩下边 edges > 0
boolean ans = false;
for (int i = 0; i < arr.length; i++) { // 当前可以选择的边,全试一遍!
// 当前的i就是火柴编号
if ((status & (1 << i)) == 0) {
if (sum + arr[i] <= len) {
// 当前的边已经耕耘长度sum + 当前边的长度 不能超过len
if (sum + arr[i] < len) {
ans = process(arr, status | (1 << i), sum + arr[i], len, edges);
} else { // sum + arr[i] == len
ans = process(arr, status | (1 << i), 0, len, edges - 1);
}
}
}
if (ans) {
break;
}
}
return ans;
}
// 利用数组里的火柴,必须都使用
// 能不能拼出正方形
public static boolean yesOrNo2(int[] arr) {
long sum = 0;
for (int num : arr) {
sum += num;
}
if (sum % 4 != 0) {
return false;
}
long len = sum / 4;
// dp[status] =
int[] dp = new int[1 << arr.length];
// dp[status] == 0 没算过!
// dp[status] == 1 算过结果是true
// dp[status] == -1 算过结果是false
return process2(arr, 0, 0, len, 4, dp);
}
// 所有火柴都在arr里
// 哪些火柴用了、哪些火柴没用的状态都在status里
// sum: 当前耕耘的这条边长度已经达成了sum
// len: 固定参数,每条边必须都达成这个长度
// edges: 还剩几条边,没填完
// 返回:最终能不能达成正方形的目标
// 看似三个可变参数status, sum, edges
// 其实只有一个status
// 15位状态2^15 -> 32...
public static boolean process2(int[] arr, int status, long sum, long len, int edges, int[] dp) {
if (edges == 0) {
return status == (1 << arr.length) - 1 ? true : false;
}
// 缓存命中
if (dp[status] != 0) {
return dp[status] == 1;
}
// 剩下边 edges > 0
boolean ans = false;
for (int i = 0; i < arr.length; i++) { // 当前可以选择的边,全试一遍!
// 当前的i就是火柴编号
if ((status & (1 << i)) == 0) {
if (sum + arr[i] <= len) {
// 当前的边已经耕耘长度sum + 当前边的长度 不能超过len
if (sum + arr[i] < len) {
ans = process2(arr, status | (1 << i), sum + arr[i], len, edges, dp);
} else { // sum + arr[i] == len
ans = process2(arr, status | (1 << i), 0, len, edges - 1, dp);
}
}
}
if (ans) {
break;
}
}
// ans == true dp[status] = 1
// ans == false dp[status] = -1
dp[status] = ans ? 1 : -1;
return ans;
}
public static boolean makesquare(int[] matchsticks) {
int sum = 0;
for (int num : matchsticks) {
sum += num;
}
if ((sum & 3) != 0) {
return false;
}
int[] dp = new int[1 << matchsticks.length];
return process(matchsticks, 0, 0, sum >> 2, 4, dp);
}
public static boolean process(int[] arr, int status, int cur, int len, int edges, int[] dp) {
if (dp[status] != 0) {
return dp[status] == 1;
}
boolean ans = false;
if (edges == 0) {
ans = (status == (1 << arr.length) - 1) ? true : false;
} else {
for (int i = 0; i < arr.length && !ans; i++) {
if (((1 << i) & status) == 0 && cur + arr[i] <= len) {
if (cur + arr[i] == len) {
ans |= process(arr, status | (1 << i), 0, len, edges - 1, dp);
} else {
ans |= process(arr, status | (1 << i), cur + arr[i], len, edges, dp);
}
}
}
}
dp[status] = ans ? 1 : -1;
return ans;
}
}

@ -1,103 +0,0 @@
package class_2022_07_4_week;
import java.util.HashSet;
import java.util.PriorityQueue;
// 在一个 2 * 3 的板上board有 5 块砖瓦,用数字 1~5 来表示,
// 以及一块空缺用 0 来表示。一次 移动 定义为选择 0 与一个相邻的数字上下左右进行交换.
// 最终当板 board 的结果是 [[1,2,3],[4,5,0]] 谜板被解开。
// 给出一个谜板的初始状态 board 
// 返回最少可以通过多少次移动解开谜板,如果不能解开谜板,则返回 -1 。
// 测试链接 : https://leetcode.cn/problems/sliding-puzzle/
public class Code02_SidingPuzzle1 {
public static int b6 = 100000;
public static int b5 = 10000;
public static int b4 = 1000;
public static int b3 = 100;
public static int b2 = 10;
public static int[] nexts = new int[3];
public static int[][] end = { { 1, 2 }, { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 0 }, { 1, 1 } };
public static int slidingPuzzle(int[][] m) {
HashSet<Integer> set = new HashSet<>();
int from = m[0][0] * b6 + m[0][1] * b5 + m[0][2] * b4 + m[1][0] * b3 + m[1][1] * b2 + m[1][2];
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> (a[0] + a[1]) - (b[0] + b[1]));
heap.add(new int[] { 0, distance(from), from });
int ans = -1;
while (!heap.isEmpty()) {
int[] arr = heap.poll();
int distance = arr[0];
int cur = arr[2];
if (set.contains(cur)) {
continue;
}
if (cur == 123450) {
ans = distance;
break;
}
set.add(cur);
int nextSize = nexts(cur);
for (int i = 0; i < nextSize; i++) {
int next = nexts[i];
if (!set.contains(next)) {
heap.add(new int[] { distance + 1, distance(next), next });
}
}
}
return ans;
}
public static int nexts(int from) {
int a = from / b6;
int b = (from / b5) % 10;
int c = (from / b4) % 10;
int d = (from / b3) % 10;
int e = (from / b2) % 10;
int f = from % 10;
if (a == 0) {
nexts[0] = from + (b - a) * b6 + (a - b) * b5;
nexts[1] = from + (d - a) * b6 + (a - d) * b3;
return 2;
} else if (b == 0) {
nexts[0] = from + (a - b) * b5 + (b - a) * b6;
nexts[1] = from + (c - b) * b5 + (b - c) * b4;
nexts[2] = from + (e - b) * b5 + (b - e) * b2;
return 3;
} else if (c == 0) {
nexts[0] = from + (b - c) * b4 + (c - b) * b5;
nexts[1] = from + (f - c) * b4 + (c - f);
return 2;
} else if (d == 0) {
nexts[0] = from + (a - d) * b3 + (d - a) * b6;
nexts[1] = from + (e - d) * b3 + (d - e) * b2;
return 2;
} else if (e == 0) {
nexts[0] = from + (b - e) * b2 + (e - b) * b5;
nexts[1] = from + (d - e) * b2 + (e - d) * b3;
nexts[2] = from + (f - e) * b2 + (e - f);
return 3;
} else {
nexts[0] = from + (e - f) + (f - e) * b2;
nexts[1] = from + (c - f) + (f - c) * b4;
return 2;
}
}
public static int distance(int num) {
int ans = end[num / b6][0] + end[num / b6][1];
ans += end[(num / b5) % 10][0] + Math.abs(end[(num / b5) % 10][1] - 1);
ans += end[(num / b4) % 10][0] + Math.abs(end[(num / b4) % 10][1] - 2);
ans += Math.abs(end[(num / b3) % 10][0] - 1) + end[(num / b3) % 10][1];
ans += Math.abs(end[(num / b2) % 10][0] - 1) + Math.abs(end[(num / b2) % 10][1] - 1);
ans += Math.abs(end[num % 10][0] - 1) + Math.abs(end[num % 10][1] - 2);
return ans;
}
}

@ -1,120 +0,0 @@
package class_2022_07_4_week;
// 在一个 2 * 3 的板上board有 5 块砖瓦,用数字 1~5 来表示,
// 以及一块空缺用 0 来表示。一次 移动 定义为选择 0 与一个相邻的数字上下左右进行交换.
// 最终当板 board 的结果是 [[1,2,3],[4,5,0]] 谜板被解开。
// 给出一个谜板的初始状态 board 
// 返回最少可以通过多少次移动解开谜板,如果不能解开谜板,则返回 -1 。
// 测试链接 : https://leetcode.cn/problems/sliding-puzzle/
public class Code02_SidingPuzzle2 {
public static int[] status = { 12345, 12354, 12435, 12453, 12534, 12543, 13245, 13254, 13425, 13452, 13524, 13542,
14235, 14253, 14325, 14352, 14523, 14532, 15234, 15243, 15324, 15342, 15423, 15432, 21345, 21354, 21435,
21453, 21534, 21543, 23145, 23154, 23415, 23451, 23514, 23541, 24135, 24153, 24315, 24351, 24513, 24531,
25134, 25143, 25314, 25341, 25413, 25431, 31245, 31254, 31425, 31452, 31524, 31542, 32145, 32154, 32415,
32451, 32514, 32541, 34125, 34152, 34215, 34251, 34512, 34521, 35124, 35142, 35214, 35241, 35412, 35421,
41235, 41253, 41325, 41352, 41523, 41532, 42135, 42153, 42315, 42351, 42513, 42531, 43125, 43152, 43215,
43251, 43512, 43521, 45123, 45132, 45213, 45231, 45312, 45321, 51234, 51243, 51324, 51342, 51423, 51432,
52134, 52143, 52314, 52341, 52413, 52431, 53124, 53142, 53214, 53241, 53412, 53421, 54123, 54132, 54213,
54231, 54312, 54321, 102345, 102354, 102435, 102453, 102534, 102543, 103245, 103254, 103425, 103452, 103524,
103542, 104235, 104253, 104325, 104352, 104523, 104532, 105234, 105243, 105324, 105342, 105423, 105432,
120345, 120354, 120435, 120453, 120534, 120543, 123045, 123054, 123405, 123450, 123504, 123540, 124035,
124053, 124305, 124350, 124503, 124530, 125034, 125043, 125304, 125340, 125403, 125430, 130245, 130254,
130425, 130452, 130524, 130542, 132045, 132054, 132405, 132450, 132504, 132540, 134025, 134052, 134205,
134250, 134502, 134520, 135024, 135042, 135204, 135240, 135402, 135420, 140235, 140253, 140325, 140352,
140523, 140532, 142035, 142053, 142305, 142350, 142503, 142530, 143025, 143052, 143205, 143250, 143502,
143520, 145023, 145032, 145203, 145230, 145302, 145320, 150234, 150243, 150324, 150342, 150423, 150432,
152034, 152043, 152304, 152340, 152403, 152430, 153024, 153042, 153204, 153240, 153402, 153420, 154023,
154032, 154203, 154230, 154302, 154320, 201345, 201354, 201435, 201453, 201534, 201543, 203145, 203154,
203415, 203451, 203514, 203541, 204135, 204153, 204315, 204351, 204513, 204531, 205134, 205143, 205314,
205341, 205413, 205431, 210345, 210354, 210435, 210453, 210534, 210543, 213045, 213054, 213405, 213450,
213504, 213540, 214035, 214053, 214305, 214350, 214503, 214530, 215034, 215043, 215304, 215340, 215403,
215430, 230145, 230154, 230415, 230451, 230514, 230541, 231045, 231054, 231405, 231450, 231504, 231540,
234015, 234051, 234105, 234150, 234501, 234510, 235014, 235041, 235104, 235140, 235401, 235410, 240135,
240153, 240315, 240351, 240513, 240531, 241035, 241053, 241305, 241350, 241503, 241530, 243015, 243051,
243105, 243150, 243501, 243510, 245013, 245031, 245103, 245130, 245301, 245310, 250134, 250143, 250314,
250341, 250413, 250431, 251034, 251043, 251304, 251340, 251403, 251430, 253014, 253041, 253104, 253140,
253401, 253410, 254013, 254031, 254103, 254130, 254301, 254310, 301245, 301254, 301425, 301452, 301524,
301542, 302145, 302154, 302415, 302451, 302514, 302541, 304125, 304152, 304215, 304251, 304512, 304521,
305124, 305142, 305214, 305241, 305412, 305421, 310245, 310254, 310425, 310452, 310524, 310542, 312045,
312054, 312405, 312450, 312504, 312540, 314025, 314052, 314205, 314250, 314502, 314520, 315024, 315042,
315204, 315240, 315402, 315420, 320145, 320154, 320415, 320451, 320514, 320541, 321045, 321054, 321405,
321450, 321504, 321540, 324015, 324051, 324105, 324150, 324501, 324510, 325014, 325041, 325104, 325140,
325401, 325410, 340125, 340152, 340215, 340251, 340512, 340521, 341025, 341052, 341205, 341250, 341502,
341520, 342015, 342051, 342105, 342150, 342501, 342510, 345012, 345021, 345102, 345120, 345201, 345210,
350124, 350142, 350214, 350241, 350412, 350421, 351024, 351042, 351204, 351240, 351402, 351420, 352014,
352041, 352104, 352140, 352401, 352410, 354012, 354021, 354102, 354120, 354201, 354210, 401235, 401253,
401325, 401352, 401523, 401532, 402135, 402153, 402315, 402351, 402513, 402531, 403125, 403152, 403215,
403251, 403512, 403521, 405123, 405132, 405213, 405231, 405312, 405321, 410235, 410253, 410325, 410352,
410523, 410532, 412035, 412053, 412305, 412350, 412503, 412530, 413025, 413052, 413205, 413250, 413502,
413520, 415023, 415032, 415203, 415230, 415302, 415320, 420135, 420153, 420315, 420351, 420513, 420531,
421035, 421053, 421305, 421350, 421503, 421530, 423015, 423051, 423105, 423150, 423501, 423510, 425013,
425031, 425103, 425130, 425301, 425310, 430125, 430152, 430215, 430251, 430512, 430521, 431025, 431052,
431205, 431250, 431502, 431520, 432015, 432051, 432105, 432150, 432501, 432510, 435012, 435021, 435102,
435120, 435201, 435210, 450123, 450132, 450213, 450231, 450312, 450321, 451023, 451032, 451203, 451230,
451302, 451320, 452013, 452031, 452103, 452130, 452301, 452310, 453012, 453021, 453102, 453120, 453201,
453210, 501234, 501243, 501324, 501342, 501423, 501432, 502134, 502143, 502314, 502341, 502413, 502431,
503124, 503142, 503214, 503241, 503412, 503421, 504123, 504132, 504213, 504231, 504312, 504321, 510234,
510243, 510324, 510342, 510423, 510432, 512034, 512043, 512304, 512340, 512403, 512430, 513024, 513042,
513204, 513240, 513402, 513420, 514023, 514032, 514203, 514230, 514302, 514320, 520134, 520143, 520314,
520341, 520413, 520431, 521034, 521043, 521304, 521340, 521403, 521430, 523014, 523041, 523104, 523140,
523401, 523410, 524013, 524031, 524103, 524130, 524301, 524310, 530124, 530142, 530214, 530241, 530412,
530421, 531024, 531042, 531204, 531240, 531402, 531420, 532014, 532041, 532104, 532140, 532401, 532410,
534012, 534021, 534102, 534120, 534201, 534210, 540123, 540132, 540213, 540231, 540312, 540321, 541023,
541032, 541203, 541230, 541302, 541320, 542013, 542031, 542103, 542130, 542301, 542310, 543012, 543021,
543102, 543120, 543201, 543210 };
public static int[] ans = { 15, -1, -1, 3, 15, -1, -1, 15, 3, -1, -1, 17, 17, -1, -1, 15, 13, -1, -1, 17, 13, -1,
-1, 7, -1, 21, 15, -1, -1, 13, 3, -1, -1, 11, 11, -1, -1, 9, 15, -1, -1, 15, 9, -1, -1, 15, 17, -1, 15, -1,
-1, 11, 19, -1, -1, 13, 17, -1, -1, 19, 13, -1, -1, 15, 13, -1, -1, 7, 9, -1, -1, 11, -1, 9, 13, -1, -1, 9,
17, -1, -1, 15, 7, -1, -1, 17, 7, -1, -1, 11, 19, -1, -1, 11, 11, -1, 17, -1, -1, 13, 11, -1, -1, 5, 13, -1,
-1, 15, 11, -1, -1, 15, 17, -1, -1, 11, 11, -1, -1, 15, 14, -1, -1, 2, 14, -1, -1, 14, 2, -1, -1, 16, 16,
-1, -1, 14, 12, -1, -1, 16, 12, -1, -1, 6, 13, -1, -1, 1, 13, -1, 2, -1, 1, 0, -1, -1, -1, 10, -1, -1, 11,
12, 10, -1, 11, 12, -1, -1, -1, 15, 3, -1, -1, 17, -1, 14, -1, -1, 15, 16, 14, -1, 15, 16, -1, -1, -1, 6,
-1, -1, 5, 4, 17, -1, -1, 15, 13, -1, 16, -1, 15, 16, -1, -1, -1, 16, -1, -1, 15, 14, 18, -1, 17, 18, -1,
-1, -1, 15, 13, -1, -1, 5, -1, 4, -1, -1, 3, 4, 12, -1, 13, 14, -1, -1, -1, 12, -1, -1, 13, 14, -1, 20, 16,
-1, -1, 12, 4, -1, -1, 12, 12, -1, -1, 8, 16, -1, -1, 14, 8, -1, -1, 16, 16, -1, -1, 19, 17, -1, -1, 13, -1,
14, -1, -1, 13, 14, 18, -1, 17, 18, -1, -1, -1, 18, -1, -1, 17, 18, 5, -1, -1, 13, 13, -1, 16, -1, 15, 14,
-1, -1, -1, 16, -1, -1, 15, 14, 8, -1, 7, 6, -1, -1, -1, 7, 15, -1, -1, 13, -1, 10, -1, -1, 11, 12, 6, -1,
5, 6, -1, -1, -1, 12, -1, -1, 13, 14, 9, -1, -1, 15, 15, -1, 18, -1, 19, 16, -1, -1, -1, 14, -1, -1, 13, 14,
10, -1, 9, 10, -1, -1, 14, -1, -1, 12, 18, -1, -1, 12, 16, -1, -1, 18, 12, -1, -1, 14, 14, -1, -1, 8, 10,
-1, -1, 12, 13, -1, -1, 13, 17, -1, 16, -1, 15, 14, -1, -1, -1, 16, -1, -1, 15, 16, 12, -1, 11, 12, -1, -1,
-1, 13, 15, -1, -1, 19, -1, 20, -1, -1, 19, 20, 14, -1, 13, 14, -1, -1, -1, 14, -1, -1, 13, 14, 11, -1, -1,
15, 15, -1, 14, -1, 15, 16, -1, -1, -1, 16, -1, -1, 17, 16, 10, -1, 9, 10, -1, -1, -1, 9, 11, -1, -1, 13,
-1, 14, -1, -1, 13, 14, 12, -1, 11, 10, -1, -1, -1, 14, -1, -1, 13, 12, -1, 8, 12, -1, -1, 8, 18, -1, -1,
14, 6, -1, -1, 18, 6, -1, -1, 10, 20, -1, -1, 10, 10, -1, -1, 7, 11, -1, -1, 7, -1, 4, -1, -1, 5, 6, 4, -1,
5, 6, -1, -1, -1, 8, -1, -1, 9, 10, 19, -1, -1, 15, 7, -1, 14, -1, 13, 14, -1, -1, -1, 10, -1, -1, 9, 8, 18,
-1, 19, 20, -1, -1, -1, 19, 7, -1, -1, 11, -1, 10, -1, -1, 9, 10, 18, -1, 19, 20, -1, -1, -1, 10, -1, -1, 9,
8, 21, -1, -1, 11, 11, -1, 10, -1, 9, 10, -1, -1, -1, 14, -1, -1, 13, 12, 18, -1, 19, 20, -1, -1, 16, -1,
-1, 12, 12, -1, -1, 6, 14, -1, -1, 16, 10, -1, -1, 14, 16, -1, -1, 10, 12, -1, -1, 14, 15, -1, -1, 13, 13,
-1, 16, -1, 15, 14, -1, -1, -1, 16, -1, -1, 15, 14, 14, -1, 13, 14, -1, -1, -1, 7, 15, -1, -1, 15, -1, 14,
-1, -1, 13, 14, 10, -1, 9, 8, -1, -1, -1, 16, -1, -1, 15, 16, 11, -1, -1, 15, 17, -1, 18, -1, 17, 18, -1,
-1, -1, 18, -1, -1, 17, 18, 12, -1, 11, 12, -1, -1, -1, 9, 13, -1, -1, 13, -1, 10, -1, -1, 11, 12, 8, -1, 7,
8, -1, -1, -1, 12, -1, -1, 13, 14 };
public static int slidingPuzzle(int[][] board) {
int from = board[0][0] * 100000 + board[0][1] * 10000 + board[0][2] * 1000 + board[1][0] * 100
+ board[1][1] * 10 + board[1][2];
return ans[find(from)];
}
public static int find(int num) {
int ans = 0;
int l = 0;
int r = status.length - 1;
int m = 0;
while (l <= r) {
m = (l + r) / 2;
if (status[m] == num) {
ans = m;
break;
} else if (status[m] > num) {
r = m - 1;
} else {
l = m + 1;
}
}
return ans;
}
}

@ -2,15 +2,39 @@ package class_2022_07_4_week;
import java.util.Arrays;
// 给你一个整数数组 nums 。如果 nums 的一个子集中
// 所有元素的乘积可以表示为一个或多个 互不相同的质数 的乘积,那么我们称它为 好子集 。
// 比方说如果 nums = [1, 2, 3, 4] 
// [2, 3] [1, 2, 3] 和 [1, 3] 是 好 子集乘积分别为 6 = 2*3 6 = 2*3  3 = 3 
// [1, 4] 和 [4] 不是 好 子集因为乘积分别为 4 = 2*2 和 4 = 2*2 
// 请你返回 nums 中不同的  子集的数目对 109 + 7 取余 的结果。
// nums 中的 子集 是通过删除 nums 中一些可能一个都不删除也可能全部都删除
// 元素后剩余元素组成的数组。
// 如果两个子集删除的下标不同,那么它们被视为不同的子集。
// 测试链接 : https://leetcode.cn/problems/the-number-of-good-subsets/
public class Code03_TheNumberOfGoodSubsets {
// 2, 3, 5, 6, 7, 10, 11, 13, 14,
// 15, 17, 19, 21, 22, 23, 26, 29, 30
public static int[] primes = {
0, 0, 1, 2, 0, 4, 3, 8, 0, 0,
5, 16, 0, 32, 9, 6, 0, 64, 0, 128,
0, 10, 17, 256, 0, 0, 33, 0, 0, 512, 7 };
public static int[] primes = {
// 11 7 5 3 2
// 2 0 0 0 0 1
// 2 5 0 0 1 0 1
0, // 0 00000000
0, // 1 00000000
1, // 2 00000001
2, // 3 00000010
0, // 4 00000000
4, // 5 00000100
3, // 6 00000011
8, // 7 00001000
0, // 8 00000000
0, // 9 00000000
5, // 10 00000101
16, 0, 32, 9, 6, 0, 64, 0, 128, 0, 10, 17, 256, 0, 0, 33, 0, 0,
512,// 29 10000000
7 // 30 2 * 3 * 5 111
};
public static int[] counts = new int[31];
@ -29,12 +53,19 @@ public class Code03_TheNumberOfGoodSubsets {
status[0] = (status[0] << 1) % mod;
}
for (int i = 2; i <= 30; i++) {
int cur = primes[i];
if (cur != 0 && counts[i] != 0) {
// 2 几次 3 几次 4几次 5几次 30 几次
int curPrimesStatus = primes[i];
if (curPrimesStatus != 0 && counts[i] != 0) {
// curPrimesStatus K次
for (int from = 0; from < (1 << 10); from++) {
if ((from & cur) == 0) {
int to = from | cur;
status[to] = (int) (((long) status[to] + ((long) status[from] * counts[i])) % mod);
// from 11111111
// 枚举所有的状态 from
// from & curPrimesStatus == 0
if ((from & curPrimesStatus) == 0) {
// to
int to = from | curPrimesStatus;
status[to] = (int) (((long) status[to] + ((long) status[from] * counts[i])) % mod);
// // status[to] += status[from] * counts[i];
}
}
}

@ -1,40 +0,0 @@
package class_2022_07_4_week;
// 测试链接 : https://leetcode.cn/problems/matchsticks-to-square/
public class Code04_MatchsticksToSquare {
public static boolean makesquare(int[] matchsticks) {
int sum = 0;
for (int num : matchsticks) {
sum += num;
}
if ((sum & 3) != 0) {
return false;
}
int[] dp = new int[1 << matchsticks.length];
return process(matchsticks, 0, 0, sum >> 2, 4, dp);
}
public static boolean process(int[] arr, int status, int cur, int len, int edges, int[] dp) {
if (dp[status] != 0) {
return dp[status] == 1;
}
boolean ans = false;
if (edges == 0) {
ans = (status == (1 << arr.length) - 1) ? true : false;
} else {
for (int i = 0; i < arr.length && !ans; i++) {
if (((1 << i) & status) == 0 && cur + arr[i] <= len) {
if (cur + arr[i] == len) {
ans |= process(arr, status | (1 << i), 0, len, edges - 1, dp);
} else {
ans |= process(arr, status | (1 << i), cur + arr[i], len, edges, dp);
}
}
}
}
dp[status] = ans ? 1 : -1;
return ans;
}
}

@ -1452,6 +1452,37 @@ Leetcode测试链接 : https://leetcode.cn/problems/rectangle-area-ii/
第033节 2022年7月第4周流行算法题目解析
一个数组如果满足 :
升降升降升降... 或者 降升降升...都是满足的
给定一个数组,
1看有几种方法能够剔除一个元素达成上述的要求
2数组天然符合要求返回0
3剔除1个元素达成不了要求返回-1
比如:
给定[3, 4, 5, 3, 7]返回3
移除0元素4 5 3 7 符合
移除1元素3 5 3 7 符合
移除2元素3 4 3 7 符合
再比如:给定[1, 2, 3, 4] 返回-1
因为达成不了要求
你将得到一个整数数组 matchsticks ,其中 matchsticks[i] 是第 i 个火柴棒的长度。
你要用 所有的火柴棍 拼成一个正方形。
你 不能折断 任何一根火柴棒,但你可以把它们连在一起,而且每根火柴棒必须 使用一次 。
如果你能拼出正方形,则返回 true ,否则返回 false 。
测试链接 : https://leetcode.cn/problems/matchsticks-to-square/
给你一个整数数组 nums 。如果 nums 的一个子集中
所有元素的乘积可以表示为一个或多个 互不相同的质数 的乘积,那么我们称它为 好子集 。
比方说如果 nums = [1, 2, 3, 4] 
[2, 3] [1, 2, 3] 和 [1, 3] 是 好 子集乘积分别为 6 = 2*3 6 = 2*3  3 = 3 
[1, 4] 和 [4] 不是 好 子集因为乘积分别为 4 = 2*2 和 4 = 2*2 
请你返回 nums 中不同的  子集的数目对 109 + 7 取余 的结果。
nums 中的 子集 是通过删除 nums 中一些可能一个都不删除也可能全部都删除
元素后剩余元素组成的数组。
如果两个子集删除的下标不同,那么它们被视为不同的子集。
测试链接 : https://leetcode.cn/problems/the-number-of-good-subsets/

Loading…
Cancel
Save