Skip to content

Commit 8295aca

Browse files
committed
weekly contest 345
1 parent cfcdc82 commit 8295aca

File tree

4 files changed

+546
-3
lines changed

4 files changed

+546
-3
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ Record leetcode contest and ideas every week, and encourage yourself to think mo
44

55
leetcode url: <https://leetcode.cn/u/cctest/>
66

7-
8-
9-
* ☀️ [weekly contest 341](src/main/java/weekly/wk341.java) 模拟 | 美剧 | 贪心 | 树形dp
7+
* ☀️ [weekly contest 345](src/main/java/weekly/wk345.java) 模拟 | 异或 | dp | 并查集
8+
* ☀️ [weekly contest 344](src/main/java/weekly/wk344.java) 前缀和 | 哈希 | 模拟 | 树形dp
9+
* ☀️ [weekly contest 343](src/main/java/weekly/wk343.java) 模拟 | 模拟 | Dijkstra
10+
* ☀️ [weekly contest 341](src/main/java/weekly/wk341.java) 模拟 | 枚举 | 贪心 | 树形dp
1011
* ☀️ [weekly contest 340](src/main/java/weekly/wk340.java) 模拟 | 哈希 | 贪心 | bfs+优化
1112
* ☀️ [weekly contest 339](src/main/java/weekly/wk339.java) 模拟 | 哈希 | 贪心 | 平衡树
1213
* ☀️ [weekly contest 338](src/main/java/weekly/wk338.java) 贪心 | 谈心 | 前缀和+二分 | 拓扑排序

src/main/java/weekly/wk343.java

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package weekly;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.PriorityQueue;
9+
import java.util.Set;
10+
11+
public class wk343 {
12+
13+
//模拟
14+
public int isWinner(int[] player1, int[] player2) {
15+
int sum1 = 0, sum2 = 0;
16+
for (int i = 0; i < player1.length; i++) {
17+
boolean f1 = false, f2 = false;
18+
int j = i - 1;
19+
while (j >= 0 && j >= i - 2) {
20+
if (player1[j] == 10) {
21+
f1 = true;
22+
}
23+
if (player2[j] == 10) {
24+
f2 = true;
25+
}
26+
j--;
27+
}
28+
if (f1) {
29+
sum1 += player1[i] * 2;
30+
} else {
31+
sum1 += player1[i];
32+
}
33+
if (f2) {
34+
sum2 += player2[i] * 2;
35+
} else {
36+
sum2 += player2[i];
37+
}
38+
}
39+
int ans = 0;
40+
if (sum1 > sum2) {
41+
ans = 1;
42+
} else if (sum2 > sum1) {
43+
ans = 2;
44+
}
45+
return ans;
46+
}
47+
48+
49+
//模拟
50+
static public int firstCompleteIndex(int[] arr, int[][] mat) {
51+
Map<Integer, Integer> map = new HashMap<>();
52+
int n = mat[0].length;
53+
int m = mat.length;
54+
for (int i = 0; i < mat.length; i++) {
55+
for (int j = 0; j < mat[0].length; j++) {
56+
map.put(mat[i][j], i * n + j);
57+
}
58+
}
59+
60+
Map<Integer, Integer> col = new HashMap<>();
61+
Map<Integer, Integer> row = new HashMap<>();
62+
63+
for (int i = 0; i < arr.length; i++) {
64+
Integer integer = map.get(arr[i]);
65+
int x = integer / n;
66+
int y = integer % n;
67+
row.put(x, row.getOrDefault(x, 0) + 1);
68+
col.put(y, col.getOrDefault(y, 0) + 1);
69+
if (row.get(x) == n || col.get(y) == m) {
70+
return i;
71+
}
72+
}
73+
return arr.length;
74+
}
75+
76+
77+
78+
79+
// static int[][] moves = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
80+
//
81+
// static public int minimumCost(int[] start, int[] target, int[][] specialRoads) {
82+
//
83+
// PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]);
84+
// priorityQueue.add(new int[]{0, start[0], start[1]});
85+
//
86+
// Map<String, List<Integer>> map = new HashMap<>();
87+
// for (int i = 0; i < specialRoads.length; i++) {
88+
// int[] specialRoad = specialRoads[i];
89+
// if (Math.abs(specialRoad[0] - specialRoad[2]) + Math.abs(specialRoad[1] - specialRoad[3]) >= specialRoad[4]) {
90+
// String k = specialRoad[0] + "-" + specialRoad[1];
91+
// if (!map.containsKey(k)) {
92+
// map.put(k, new ArrayList<>());
93+
// }
94+
// map.get(k).add(i);
95+
// }
96+
// }
97+
// // System.out.println(map.size());
98+
// Set<String> set = new HashSet<>();
99+
// while (!priorityQueue.isEmpty()) {
100+
// int[] cur = priorityQueue.poll();
101+
// int cost = cur[0];
102+
// int x = cur[1];
103+
// int y = cur[2];
104+
// if (set.contains(x + "-" + y)) continue;
105+
// if (x == target[0] && y == target[1]) {
106+
// return cost;
107+
// }
108+
// set.add(x + "-" + y);
109+
//
110+
// for (int[] move : moves) {
111+
// int nx = move[0] + x;
112+
// int ny = move[1] + y;
113+
// if(nx<=target[0]&&ny<=target[1]&&!set.contains(nx+"-"+ny)){
114+
// priorityQueue.add(new int[]{cost + 1, nx, ny});
115+
// }
116+
// }
117+
// for (Integer index : map.getOrDefault(x + "-" + y, new ArrayList<>())) {
118+
// if(set.contains(specialRoads[index][2]+"-"+specialRoads[index][3])) continue;
119+
// priorityQueue.add(new int[]{cost + specialRoads[index][4], specialRoads[index][2], specialRoads[index][3]});
120+
// }
121+
// }
122+
// return -1;
123+
// }
124+
125+
126+
//朴素Dijkstra
127+
/* public int minimumCost(int[] start, int[] target, int[][] specialRoads) {
128+
long t = (long) target[0] << 32 | target[1];
129+
Map<Long, Integer> dis = new HashMap();
130+
dis.put(t, Math.abs(start[0] - target[0]) + Math.abs(start[1] - target[1]));
131+
dis.put((long) start[0] << 32 | start[1], 0);
132+
Set<Long> vis = new HashSet<>();
133+
for (; ; ) {
134+
long v = -1;
135+
int dv = -1;
136+
for (Map.Entry<Long, Integer> e : dis.entrySet()) {
137+
if (!vis.contains(e.getKey()) && (dv < 0 || e.getValue() < dv)) {
138+
v = e.getKey();
139+
dv = e.getValue();
140+
}
141+
}
142+
if (v == t) return dv; // 到终点的最短路已确定
143+
vis.add(v);
144+
int vx = (int) (v >> 32), vy = (int) (v & Integer.MAX_VALUE);
145+
// 更新到终点的最短路
146+
dis.merge(t, dv + (target[0] - vx + target[1] - vy), Math::min);
147+
//计算改点到其他special的距离
148+
for (int[] r : specialRoads) {
149+
int d = dv + Math.abs(r[0] - vx) + Math.abs(r[1] - vy) + r[4];
150+
long w = (long) r[2] << 32 | r[3];
151+
if (d < dis.getOrDefault(w, Integer.MAX_VALUE))
152+
dis.put(w, d);
153+
}
154+
}
155+
}*/
156+
157+
int dis(int x1, int y1, int x2, int y2) {
158+
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
159+
}
160+
161+
//优先队列 Dijkstra
162+
public int minimumCost(int[] start, int[] target, int[][] specialRoads) {
163+
PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]);
164+
priorityQueue.add(new int[]{0, start[0], start[1]});
165+
priorityQueue.add(new int[]{Integer.MAX_VALUE, target[0], target[1]});
166+
Set<Long> visited = new HashSet<>();
167+
while (!priorityQueue.isEmpty()) {
168+
int[] poll = priorityQueue.poll();
169+
long flag = ((long) poll[1] << 32) + poll[2];
170+
System.out.println(poll[0]+" "+poll[1]+" "+poll[2]);
171+
if (visited.contains(flag)) continue;
172+
if (poll[1] == target[0] && poll[2] == target[1]) {
173+
return poll[0];
174+
}
175+
visited.add(flag);
176+
priorityQueue.add(new int[]{dis(target[0], target[1], poll[1], poll[2])+poll[0], target[0], target[1]});
177+
for (int[] specialRoad : specialRoads) {
178+
long f = ((long) specialRoad[2] << 32) + specialRoad[3];
179+
if (visited.contains(f)) continue;
180+
int d = Math.min(specialRoad[4] + dis(poll[1], poll[2], specialRoad[0], specialRoad[1]),dis(specialRoad[2],specialRoad[3],poll[1],poll[2]))+poll[0];
181+
priorityQueue.add(new int[]{d, specialRoad[2], specialRoad[3]});
182+
}
183+
}
184+
return -1;
185+
}
186+
public static void main(String[] args) {
187+
wk343 w=new wk343();
188+
w.minimumCost(new int[]{1,1},new int[]{10,9},new int[][]{
189+
{5,2,3,6,3},{5,6,9,5,3},{5,9,1,2,5},{8,6,9,8,1}
190+
});
191+
}
192+
193+
194+
}

src/main/java/weekly/wk344.java

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package weekly;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
public class wk344 {
11+
// 前缀和
12+
public int[] distinctDifferenceArray(int[] nums) {
13+
int[] ans = new int[nums.length];
14+
15+
for (int i = 0; i < nums.length; i++) {
16+
Set<Integer> left = new HashSet<>();
17+
for (int j = 0; j <= i; j++) {
18+
left.add(nums[j]);
19+
}
20+
Set<Integer> right = new HashSet<>();
21+
for (int j = i + 1; j < nums.length; j++) {
22+
right.add(nums[j]);
23+
}
24+
ans[i] = left.size() - right.size();
25+
}
26+
return ans;
27+
}
28+
29+
30+
//记录 数字->频率 频率->个数
31+
class FrequencyTracker {
32+
33+
int[] count = new int[(int) 1e5 + 7];
34+
Map<Integer, Integer> map = new HashMap<>();
35+
36+
public FrequencyTracker() {
37+
38+
}
39+
40+
public void add(int number) {
41+
Integer c = map.getOrDefault(number, 0);
42+
count[c]--;
43+
count[c + 1]++;
44+
map.put(number, c + 1);
45+
}
46+
47+
public void deleteOne(int number) {
48+
Integer c = map.getOrDefault(number, 0);
49+
if (c == 0) return;
50+
count[c]--;
51+
count[c - 1]++;
52+
map.put(number, c - 1);
53+
}
54+
55+
public boolean hasFrequency(int frequency) {
56+
return count[frequency] > 0;
57+
}
58+
}
59+
60+
61+
//模拟
62+
static public int[] colorTheArray(int n, int[][] queries) {
63+
int[] ans = new int[queries.length];
64+
int[] color = new int[n];
65+
int same = 0;
66+
for (int i = 0; i < queries.length; i++) {
67+
int[] query = queries[i];
68+
int index = query[0];
69+
70+
//若颜色未改变
71+
if (color[index] == query[1]) {
72+
ans[i] = same;
73+
continue;
74+
}
75+
//考虑前面一个
76+
if (index > 0) {
77+
//现在相同了
78+
if (color[index - 1] == query[1]) {
79+
same++;
80+
//之前有颜色且是相同的色块,则表示这次需要减少
81+
} else if (color[index] != 0 && color[index - 1] == color[index]) {
82+
same--;
83+
}
84+
}
85+
//考虑后面一个
86+
if (index < color.length - 1) {
87+
if (color[index + 1] == query[1]) {
88+
same++;
89+
} else if (color[index] != 0 && color[index + 1] == color[index]) {
90+
same--;
91+
}
92+
}
93+
color[index] = query[1];
94+
ans[i] = same;
95+
}
96+
return ans;
97+
}
98+
99+
public static void main(String[] args) {
100+
minIncrements(7, new int[]{1, 5, 2, 2, 3, 3, 1});
101+
}
102+
103+
/* static public int minIncrements(int n, int[] cost) {
104+
int ans = 0;
105+
int step = 1;
106+
int[] sum = new int[n];
107+
sum[0] = cost[0];
108+
int i = 1;
109+
while (i < n) {
110+
int count = 1 << step;
111+
for (int j = i; j < i + count; j++) {
112+
sum[j] = cost[j] + sum[(j - 1) / 2];
113+
}
114+
i += count;
115+
step++;
116+
}
117+
List<Integer> sumS = new ArrayList<>();
118+
int max = 0;
119+
for (int j = n / 2; j < n; j++) {
120+
max = Math.max(sum[j], max);
121+
sumS.add(sum[j]);
122+
}
123+
124+
List<Integer> nSum = new ArrayList<>();
125+
for (int j = 0; j < sumS.size(); j += 2) {
126+
int min = Math.min(max - sumS.get(j), max - sumS.get(j + 1));
127+
ans += max - sumS.get(j + 1) - min;
128+
ans += max - sumS.get(j) - min;
129+
nSum.add(min);
130+
}
131+
sumS = nSum;
132+
133+
while (sumS.size() > 1) {
134+
nSum = new ArrayList<>();
135+
for (int j = 0; j < sumS.size(); j += 2) {
136+
int min = Math.min(sumS.get(j), sumS.get(j + 1));
137+
ans += sumS.get(j) - min;
138+
ans += sumS.get(j + 1) - min;
139+
nSum.add(min);
140+
}
141+
sumS = nSum;
142+
}
143+
return ans;
144+
}*/
145+
146+
//树形DP
147+
static public int minIncrements(int n, int[] cost) {
148+
return dfs(cost, 0)[0];
149+
}
150+
151+
static int[] dfs(int[] cost, int index) {
152+
if (index >= cost.length) {
153+
return new int[2];
154+
}
155+
int[] left = dfs(cost, index * 2 + 1), right = dfs(cost, index * 2 + 2);
156+
157+
return new int[]{
158+
//arr[0]表示
159+
left[0] + right[0] + Math.abs(left[1] - right[1]),
160+
cost[index] + Math.max(left[1], right[1])
161+
};
162+
}
163+
}

0 commit comments

Comments
 (0)