Skip to content

Commit 343d95a

Browse files
committed
weekly contest 393
1 parent ccb9aed commit 343d95a

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Record leetcode contest and ideas every week, and encourage yourself to think mo
55
leetcode url: <https://leetcode.cn/u/cctest/>
66

77

8+
* 🐼 [weekly contest 393](src/main/java/weekly/wk393.java) 枚举 | 遍历 | 二分 | dp
89
* 🐼 [weekly contest 392](src/main/java/weekly/wk392.java) 遍历 | 贪心 | 排序 | 并查集
910
* 🐼 [weekly contest 391](src/main/java/weekly/wk391.java) 遍历 | 模拟 | 滑动窗口 | 数学
1011
* 🐼 [weekly contest 390](src/main/java/weekly/wk390.java) 滑动窗口 | 枚举 | 懒删除 | 字典树

src/main/java/weekly/wk393.java

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package weekly;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.PriorityQueue;
7+
import java.util.Set;
8+
9+
public class wk393 {
10+
11+
// 枚举
12+
public String findLatestTime(String s) {
13+
char[] chars = s.toCharArray();
14+
if (chars[0] == '?' && chars[1] == '?') {
15+
chars[0] = '1';
16+
chars[1] = '1';
17+
} else if (chars[0] == '?') {
18+
if (chars[1] > '1') {
19+
chars[0] = '0';
20+
} else {
21+
chars[0] = '1';
22+
}
23+
} else if (chars[1] == '?') {
24+
if (chars[0] == '1') {
25+
chars[1] = '1';
26+
} else {
27+
chars[1] = '9';
28+
}
29+
}
30+
31+
if (chars[3] == '?') {
32+
chars[3] = '5';
33+
}
34+
if (chars[4] == '?') {
35+
chars[4] = '9';
36+
}
37+
return new String(chars);
38+
}
39+
40+
// 遍历
41+
public int maximumPrimeDifference(int[] nums) {
42+
int left = -1;
43+
int right = -1;
44+
for (int i = 0; i < nums.length; i++) {
45+
if (isPrime(nums[i])) {
46+
if (left == -1) {
47+
left = i;
48+
}
49+
right = i;
50+
}
51+
}
52+
return right - left;
53+
}
54+
55+
public boolean isPrime(int number) {
56+
if (number == 1) return false;
57+
int count = 0;
58+
for (int i = 2; i < number; i++) {
59+
if ((number % i) == 0) {
60+
count++;
61+
}
62+
}
63+
return count == 0;
64+
}
65+
66+
67+
// 超时
68+
/* public long findKthSmallest(int[] coins, int k) {
69+
Set<Long> set = new HashSet<>();
70+
PriorityQueue<long[]> priorityQueue = new PriorityQueue<>((a, b) -> Long.compare(a[0], b[0]));
71+
72+
for (int i = 0; i < coins.length; i++) {
73+
priorityQueue.add(new long[]{coins[i], i});
74+
}
75+
76+
77+
long ans = -1;
78+
for (int i = 0; i < k; i++) {
79+
80+
while (true) {
81+
long[] poll = priorityQueue.poll();
82+
if (set.contains(poll[0])) {
83+
int index = (int) poll[1];
84+
priorityQueue.add(new long[]{poll[0] + coins[index], index});
85+
continue;
86+
}
87+
int index = (int) poll[1];
88+
priorityQueue.add(new long[]{poll[0] + coins[index], index});
89+
ans = poll[0];
90+
set.add(poll[0]);
91+
break;
92+
}
93+
}
94+
return ans;
95+
}
96+
97+
*/
98+
99+
100+
// 二分 容斥+枚举子集
101+
public long findKthSmallest(int[] coins, int k) {
102+
int mn = Integer.MAX_VALUE;
103+
for (int x : coins) {
104+
mn = Math.min(mn, x);
105+
}
106+
long left = k - 1, right = (long) mn * k;
107+
while (left + 1 < right) {
108+
long mid = (left + right) / 2;
109+
if (check(mid, coins, k)) {
110+
right = mid;
111+
} else {
112+
left = mid;
113+
}
114+
}
115+
return right;
116+
}
117+
118+
private boolean check(long m, int[] coins, int k) {
119+
long cnt = 0;
120+
next:
121+
for (int i = 1; i < (1 << coins.length); i++) { // 枚举所有非空子集
122+
long lcmRes = 1; // 计算子集 LCM
123+
for (int j = 0; j < coins.length; j++) {
124+
if ((i >> j & 1) == 1) {
125+
lcmRes = lcm(lcmRes, coins[j]);
126+
if (lcmRes > m) { // 太大了
127+
continue next;
128+
}
129+
}
130+
}
131+
cnt += Integer.bitCount(i) % 2 == 1 ? m / lcmRes : -m / lcmRes;
132+
}
133+
return cnt >= k;
134+
}
135+
136+
private long lcm(long a, long b) {
137+
return a * b / gcd(a, b);
138+
}
139+
140+
private long gcd(long a, long b) {
141+
return b == 0 ? a : gcd(b, a % b);
142+
}
143+
144+
145+
146+
// 动态规划
147+
public int minimumValueSum(int[] nums, int[] andValues) {
148+
Map<Long, Integer> memo = new HashMap<>();
149+
int ans = dfs(0, 0, -1, nums, andValues, memo);
150+
return ans < Integer.MAX_VALUE / 2 ? ans : -1;
151+
}
152+
153+
private int dfs(int i, int j, int and, int[] nums, int[] andValues, Map<Long, Integer> memo) {
154+
int n = nums.length;
155+
int m = andValues.length;
156+
if (m - j > n - i) { // 剩余元素不足
157+
return Integer.MAX_VALUE / 2;
158+
}
159+
if (j == m) { // 分了 m 段
160+
return i == n ? 0 : Integer.MAX_VALUE / 2;
161+
}
162+
and &= nums[i];
163+
if (and < andValues[j]) { // 剪枝:无法等于 andValues[j]
164+
return Integer.MAX_VALUE / 2;
165+
}
166+
long mask = (long) i << 36 | (long) j << 32 | and; // 三个状态压缩成一个 long
167+
if (memo.containsKey(mask)) {
168+
return memo.get(mask);
169+
}
170+
int res = dfs(i + 1, j, and, nums, andValues, memo); // 不划分
171+
if (and == andValues[j]) { // 划分,nums[i] 是这一段的最后一个数
172+
res = Math.min(res, dfs(i + 1, j + 1, -1, nums, andValues, memo) + nums[i]);
173+
}
174+
memo.put(mask, res);
175+
return res;
176+
}
177+
178+
179+
}

0 commit comments

Comments
 (0)