Skip to content

Commit 89fcb7d

Browse files
Merge pull request SharingSource#544 from SharingSource/ac_oier
✨feat: add 1239、1723、473
2 parents d7a7b9f + 3b58acc commit 89fcb7d

3 files changed

+15
-11
lines changed

LeetCode/1231-1240/1239. 串联字符串的最大长度(中等).md

+11-4
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,19 @@ class Solution {
285285
ans = Math.max(ans, cur);
286286
return cur;
287287
}
288-
void swap(int[] arr, int i, int j) {
289-
int c = arr[i];
290-
arr[i] = arr[j];
291-
arr[j] = c;
288+
void shuffle(int[] nums) {
289+
for (int i = n; i > 0; i--) {
290+
int idx = random.nextInt(i);
291+
swap(nums, idx, i - 1);
292+
}
293+
}
294+
void swap(int[] nums, int a, int b) {
295+
int c = nums[a];
296+
nums[a] = nums[b];
297+
nums[b] = c;
292298
}
293299
void sa() {
300+
shuffle(ws);
294301
for (double t = hi; t > lo; t *= fa) {
295302
int a = random.nextInt(n), b = random.nextInt(n);
296303
int prev = calc();

LeetCode/1721-1730/1723. 完成所有工作的最短时间(困难).md

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
Tag : 「DFS」、「模拟退火」、「启发式搜素」、「随机化」
66

7-
8-
9-
107
给你一个整数数组 `jobs` ,其中 $jobs[i]$ 是完成第 $i$ 项工作要花费的时间。
118

129
请你将这些工作分配给 $k$ 位工人。所有工作都应该分配给工人,且每项工作只能分配给一位工人。
@@ -195,7 +192,7 @@ class Solution {
195192
int ans = 0x3f3f3f3f;
196193
Random random = new Random(20210508);
197194
// 最高温/最低温/变化速率(以什么速度进行退火,系数越低退火越快,迭代次数越少,落入「局部最优」(WA)的概率越高;系数越高 TLE 风险越大)
198-
double hi = 1e5, lo = 1e-5, fa = 0.90;
195+
double hi = 1e8, lo = 1e-4, fa = 0.90;
199196
// 迭代次数,与变化速率同理
200197
int N = 400;
201198

@@ -230,6 +227,7 @@ class Solution {
230227
arr[j] = c;
231228
}
232229
void sa() {
230+
shuffle(jobs);
233231
for (double t = hi; t > lo; t *= fa) {
234232
int a = random.nextInt(n), b = random.nextInt(n);
235233
int prev = calc(); // 退火前
@@ -244,7 +242,6 @@ class Solution {
244242
jobs = _jobs;
245243
n = jobs.length;
246244
k = _k;
247-
shuffle(jobs);
248245
while (N-- > 0) sa();
249246
return ans;
250247
}

LeetCode/471-480/473. 火柴拼正方形(中等).md

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class Solution {
116116
int[] ms;
117117
int n, k;
118118
Random random = new Random(20220601);
119-
double hi = 1e4, lo = 1e-4, fa = 0.98;
119+
double hi = 1e9, lo = 1e-4, fa = 0.95;
120120
int N = 400;
121121
boolean ans = false;
122122
int calc() {
@@ -130,6 +130,7 @@ class Solution {
130130
return diff;
131131
}
132132
void sa() {
133+
shuffle(ms);
133134
for (double t = hi; t > lo && !ans; t *= fa) {
134135
int a = random.nextInt(n), b = random.nextInt(n);
135136
if (a == b) continue;
@@ -147,7 +148,6 @@ class Solution {
147148
for (int i : ms) sum += i;
148149
k = sum / 4;
149150
if (k * 4 != sum) return false;
150-
shuffle(ms);
151151
while (!ans && N-- > 0) sa();
152152
return ans;
153153
}

0 commit comments

Comments
 (0)