Skip to content

Commit a966671

Browse files
committed
weekly contest 347
1 parent 39faee8 commit a966671

File tree

2 files changed

+241
-0
lines changed

2 files changed

+241
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ leetcode url: <https://leetcode.cn/u/cctest/>
5959
* 🐭 [weekly contest 280](src/main/java/weekly/wk289.java)
6060

6161

62+
* 🐸 [biweekly contest 105](src/main/java/weekly/wkb105.java) 模拟 | 记忆化搜索 | 分类讨论 | 并查集
6263
* 🐸 [biweekly contest 102](src/main/java/weekly/wkb102.java) 模拟 | 前缀和 | bfs+dfs | Dijkstra
6364
* 🐸 [biweekly contest 101](src/main/java/weekly/wkb101.java) 哈希 | 滑动窗口 | 并查集 | BFS
6465
* 🐸 [biweekly contest 100](src/main/java/weekly/wkb100.java) 四维 | 贪心 | 堆 | 二分

src/main/java/weekly/wkb105.java

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
package weekly;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
public class wkb105 {
13+
14+
//模拟
15+
public int buyChoco(int[] prices, int money) {
16+
Arrays.sort(prices);
17+
if (prices[0] + prices[1] > money) {
18+
return money;
19+
}
20+
return money - prices[0] - prices[1];
21+
}
22+
23+
//记忆化搜索
24+
public int minExtraChar(String s, String[] dictionary) {
25+
Arrays.sort(dictionary, (a, b) -> b.length() - a.length());
26+
dp = new int[s.length()];
27+
Arrays.fill(dp, -1);
28+
help(s, dictionary, 0);
29+
return dp[0];
30+
}
31+
32+
33+
int[] dp;
34+
35+
int help(String s, String[] dictionary, int index) {
36+
37+
if (index >= s.length()) {
38+
return 0;
39+
}
40+
if (dp[index] != -1) return dp[index];
41+
int ans = help(s, dictionary, index + 1) + 1;
42+
for (int j = 0; j < dictionary.length; j++) {
43+
if (index + dictionary[j].length() > s.length()) {
44+
continue;
45+
}
46+
if (s.substring(index, index + dictionary[j].length()).equals(dictionary[j])) {
47+
ans = Math.min(ans, help(s, dictionary, index + dictionary[j].length()));
48+
}
49+
}
50+
dp[index] = ans;
51+
return ans;
52+
}
53+
54+
55+
//分类讨论
56+
public long maxStrength(int[] nums) {
57+
long ans = 1;
58+
59+
List<Integer> list = new ArrayList<>();
60+
List<Integer> pos = new ArrayList<>();
61+
int zero = 0;
62+
for (int num : nums) {
63+
if (num > 0) {
64+
pos.add(num);
65+
ans *= num;
66+
} else if (num < 0) {
67+
list.add(num);
68+
} else {
69+
zero++;
70+
}
71+
}
72+
73+
if (pos.size() == 0 && list.size() == 1) {
74+
if (zero > 0) {
75+
return 0;
76+
} else {
77+
return list.get(0);
78+
}
79+
}
80+
Collections.sort(list);
81+
int n = 0;
82+
if (list.size() % 2 == 0) {
83+
n = list.size();
84+
} else {
85+
n = list.size() - 1;
86+
}
87+
for (int i = 0; i < n; i++) {
88+
ans *= list.get(i);
89+
}
90+
return ans;
91+
92+
}
93+
94+
//并查集
95+
class UnionFind {
96+
public final int[] parents;
97+
public int count;
98+
99+
public UnionFind(int n) {
100+
this.parents = new int[n];
101+
reset();
102+
}
103+
104+
public void reset() {
105+
for (int i = 0; i < parents.length; i++) {
106+
parents[i] = i;
107+
}
108+
count = parents.length - 1;
109+
}
110+
111+
public int find(int i) {
112+
int parent = parents[i];
113+
if (parent == i) {
114+
return i;
115+
} else {
116+
int root = find(parent);
117+
parents[i] = root;
118+
return root;
119+
}
120+
}
121+
122+
public boolean union(int i, int j) {
123+
int r1 = find(i);
124+
int r2 = find(j);
125+
if (r1 != r2) {
126+
count--;
127+
parents[r1] = r2;
128+
return true;
129+
} else {
130+
return false;
131+
}
132+
}
133+
134+
/* void isolate(int x) {
135+
if (x != parents[x]) {
136+
parents[x] = x;
137+
count++;
138+
}
139+
}*/
140+
141+
}
142+
143+
public static List<Integer> getPrimes(int n) {
144+
145+
boolean[] isComposite = new boolean[n + 1];
146+
List<Integer> primes = new ArrayList<>();
147+
148+
for (int i = 2; i <= n; i++) {
149+
if (!isComposite[i]) {
150+
primes.add(i);
151+
for (int j = i * i; j >= 0 && j <= n; j += i) { // 标记该数的倍数为合数
152+
isComposite[j] = true;
153+
}
154+
}
155+
}
156+
157+
return primes;
158+
}
159+
160+
static List<Integer>[] zhiyinshu = new List[100001];
161+
162+
static {
163+
164+
zhiyinshu[1]=new ArrayList<>();
165+
for (int i = 2; i < zhiyinshu.length; i++) {
166+
if (zhiyinshu[i] == null) {
167+
for (int j = i; j < zhiyinshu.length; j += i) {
168+
if (zhiyinshu[j] == null) {
169+
zhiyinshu[j] = new ArrayList<>();
170+
}
171+
zhiyinshu[j].add(i);
172+
}
173+
}
174+
}
175+
}
176+
177+
public boolean canTraverseAllPairs(int[] nums) {
178+
int n = nums.length;
179+
int mx = 0;
180+
for (int num : nums) {
181+
mx = Math.max(num, mx);
182+
}
183+
UnionFind uf = new UnionFind(nums.length + mx+1);
184+
185+
for (int i = 0; i < nums.length; i++) {
186+
for (Integer prime : zhiyinshu[nums[i]]) {
187+
uf.union(i, prime + n);
188+
}
189+
}
190+
int group = -1;
191+
for (int i = 0; i < n; i++) {
192+
int g = uf.find(i);
193+
if (group == -1) group = g;
194+
if (group != g) return false;
195+
}
196+
return true;
197+
198+
}
199+
/* public boolean canTraverseAllPairs(int[] nums) {
200+
201+
if(nums.length==1&&nums[0]==1) return true;
202+
203+
Set<Integer> set=new HashSet<>();
204+
for (int num : nums) {
205+
set.add(num);
206+
}
207+
nums=new int[set.size()];
208+
int index=0;
209+
for (Integer integer : set) {
210+
nums[index++]=integer;
211+
}
212+
213+
UnionFind uf = new UnionFind(primes.size());
214+
int[] count = new int[primes.size()];
215+
Map<Integer,Integer> map=new HashMap<>();
216+
for (int i = 0; i < nums.length; i++) {
217+
if (nums[i] == 1) return false;
218+
for (int j = 0;j < primes.size()&&nums[i]>=primes.get(j); j++) {
219+
if (nums[i] % primes.get(j) == 0) {
220+
count[j]++;
221+
if(!map.containsKey(i)){
222+
map.put(i,j);
223+
}else {
224+
uf.union(map.get(i),j);
225+
}
226+
}
227+
}
228+
}
229+
int group=-1;
230+
for (int i = 0; i < primes.size(); i++) {
231+
if(count[i]==0) continue;
232+
int g = uf.find(i);
233+
if(group==-1) group=g;
234+
if(group!=g) return false;
235+
}
236+
return true;
237+
}*/
238+
239+
240+
}

0 commit comments

Comments
 (0)