-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwkb99.java
142 lines (116 loc) · 3.74 KB
/
wkb99.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package weekly;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class wkb99 {
//贪心
public int splitNum(int num) {
List<Integer> list = new ArrayList<>();
List<Integer> l1 = new ArrayList<>();
List<Integer> l2 = new ArrayList<>();
while (num > 0) {
list.add(num % 10);
num /= 10;
}
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
if (i % 2 == 0) {
l1.add(list.get(i));
} else {
l2.add(list.get(i));
}
}
return sum(l1) + sum(l2);
}
//找规律 递推
public int sum(List<Integer> list) {
int s = 0;
for (Integer i : list) {
s = s * 10 + i;
}
return s;
}
static Map<Integer, Long> map = new HashMap<>();
static {
map.put(1, (long) 1);
long sum = 1;
for (int i = 1; i <= (int) 1e5; i++) {
sum += (long) i * 4;
map.put(i + 1, sum);
}
}
public long coloredCells(int n) {
return map.get(n);
}
//排序,合并区间
public int countWays(int[][] ranges) {
Arrays.sort(ranges, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);
int pre = ranges[0][1];
int count = 1;
for (int i = 1; i < ranges.length; i++) {
if (ranges[i][0] <= pre) {
pre = Math.max(ranges[i][1], pre);
} else {
count++;
pre = ranges[i][1];
}
}
long res = 1;
for (int i = 0; i < count; i++) {
res *= 2;
res %= (int) 1e9 + 7;
}
return (int) res;
}
//两次dfs,也换根DP
public int rootCount(int[][] edges, int[][] guesses, int k) {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int[] edge : edges) {
if (!map.containsKey(edge[0])) map.put(edge[0], new ArrayList<>());
if (!map.containsKey(edge[1])) map.put(edge[1], new ArrayList<>());
map.get(edge[0]).add(edge[1]);
map.get(edge[1]).add(edge[0]);
}
Map<Integer, Set<Integer>> guessMap = new HashMap<>();
for (int[] guess : guesses) {
if (!guessMap.containsKey(guess[0])) guessMap.put(guess[0], new HashSet<>());
guessMap.get(guess[0]).add(guess[1]);
}
int kk = dfs(-1, 0, map, guessMap);
return dfs2(-1, 0, map, guessMap, k, kk);
}
int dfs(int parent, int index, Map<Integer, List<Integer>> map, Map<Integer, Set<Integer>> guessMap) {
int val = 0;
for (Integer child : map.getOrDefault(index, new ArrayList<>())) {
if (child == parent) continue;
if (guessMap.containsKey(index) && guessMap.get(index).contains(child)) {
val++;
}
val += dfs(index, child, map, guessMap);
}
return val;
}
int dfs2(int parent, int index, Map<Integer, List<Integer>> map, Map<Integer, Set<Integer>> guessMap, int k, int kk) {
int res = 0;
//可满足
if (guessMap.containsKey(index) && guessMap.get(index).contains(parent)) {
kk++;
}
if (guessMap.containsKey(parent) && guessMap.get(parent).contains(index)) {
kk--;
}
if (kk >= k) {
res++;
}
for (Integer child : map.getOrDefault(index, new ArrayList<>())) {
if (child == parent) continue;
res += dfs2(index, child, map, guessMap, k, kk);
}
return res;
}
}