-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwkb113.java
139 lines (113 loc) · 3.76 KB
/
wkb113.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
package weekly;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class wkb113 {
//找到截断点
public int minimumRightShifts(List<Integer> nums) {
if (nums.size() == 1) return 0;
List<Integer> list = new ArrayList<>();
if (nums.get(0) < nums.get(nums.size() - 1)) {
list.add(0);
}
for (int i = 1; i < nums.size(); i++) {
if (nums.get(i) < nums.get(i - 1)) {
list.add(i);
}
}
if (list.size() > 1) return -1;
return (nums.size() - list.get(0)) % nums.size();
}
//贪心
static public int minLengthAfterRemovals(List<Integer> nums) {
List<Integer> list = new ArrayList<>();
int count = 1;
for (int i = 1; i < nums.size(); i++) {
if (nums.get(i).equals(nums.get(i - 1))) {
count++;
} else {
list.add(count);
count = 1;
}
}
list.add(count);
if (list.size() == 1) return nums.size();
Collections.sort(list, Comparator.reverseOrder());
int max = list.get(0);
int left = 0;
for (Integer integer : list) {
left += integer;
}
left -= max;
return max >= left ? max - left : nums.size() % 2;
}
// 哈希
static public int countPairs(List<List<Integer>> coordinates, int k) {
int ans = 0;
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < coordinates.size(); i++) {
int x = coordinates.get(i).get(0);
int y = coordinates.get(i).get(1);
for (int j = 0; j <= k; j++) {
int cx = x ^ j;
int cy = y ^ (k - j);
ans += map.getOrDefault(cx + "-" + cy, 0);
}
map.put(x + "-" + y, map.getOrDefault(x + "-" + y, 0) + 1);
}
return ans;
}
//换根dp
public int[] minEdgeReversals(int n, int[][] edges) {
Map<Integer, List<int[]>> 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(new int[]{edge[1], 0});
map.get(edge[1]).add(new int[]{edge[0], 1});
}
//计算正反路径次数
int[] count = dfs(0, -1, map);
int[] ans = new int[n];
//
help(0, -1, map, count, new int[2], ans);
return ans;
}
int[] dfs(int cur, int pre, Map<Integer, List<int[]>> map) {
int[] ans = new int[2];
for (int[] next : map.get(cur)) {
if (next[0] == pre) continue;
ans[next[1]]++;
int[] child = dfs(next[0], cur, map);
ans[0] += child[0];
ans[1] += child[1];
}
return ans;
}
//
void help(int cur, int pre, Map<Integer, List<int[]>> map, int[] count, int[] parent, int[] ans) {
//根节点的反路径-根节点到此的反路径+根节点到此的正路径
ans[cur] = count[1] - parent[1]+parent[0];
for (int[] next : map.get(cur)) {
if (next[0] == pre) continue;
parent[next[1]]++;
help(next[0], cur, map, count, parent,ans);
//回溯
parent[next[1]]--;
}
}
public static void main(String[] args) {
minLengthAfterRemovals(Arrays.asList(1000000000, 1000000000));
}
}