-
Notifications
You must be signed in to change notification settings - Fork 0
/
wk392.java
171 lines (148 loc) · 4.25 KB
/
wk392.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package weekly;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class wk392 {
// 遍历
public int longestMonotonicSubarray(int[] nums) {
int ans = 0;
int temp = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i - 1]) {
temp++;
} else {
ans = Math.max(temp, ans);
temp = 1;
}
}
ans = Math.max(temp, ans);
temp = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] < nums[i - 1]) {
temp++;
} else {
ans = Math.max(temp, ans);
temp = 1;
}
}
ans = Math.max(temp, ans);
temp = 0;
return ans;
}
// 贪心
public String getSmallestString(String s, int k) {
char[] chars = s.toCharArray();
for (int i = 0; i < s.length(); i++) {
int d = distance('a', s.charAt(i));
if (k - d >= 0) {
chars[i] = 'a';
k -= d;
} else {
chars[i] -= k;
break;
}
}
return new String(chars);
}
int distance(char a, char b) {
if (a > b) {
char t = a;
a = b;
b = t;
}
return Math.min(b - a, a + 26 - b);
}
// 排序
public long minOperationsToMakeMedianK(int[] nums, int k) {
Arrays.sort(nums);
int mid = nums.length / 2;
long ans = 0;
for (int i = mid; i < nums.length; i++) {
if (nums[i] < k) {
ans += k - nums[i];
}
}
for (int i = mid; i >= 0; i--) {
if (nums[i] > k) {
ans += nums[i] - k;
}
}
return ans;
}
class UnionFind {
public final int[] parents;
public int count;
public UnionFind(int n) {
this.parents = new int[n];
reset();
}
public void reset() {
for (int i = 0; i < parents.length; i++) {
parents[i] = i;
}
count = parents.length - 1;
}
public int find(int i) {
int parent = parents[i];
if (parent == i) {
return i;
} else {
int root = find(parent);
parents[i] = root;
return root;
}
}
public boolean union(int i, int j) {
int r1 = find(i);
int r2 = find(j);
if (r1 != r2) {
count--;
parents[r1] = r2;
return true;
} else {
return false;
}
}
}
//并查集 &性质
public int[] minimumCost(int n, int[][] edges, int[][] query) {
UnionFind uf = new UnionFind(n + 1);
Map<Integer, List<int[]>> map = new HashMap<>();
for (int i = 0; i < edges.length; i++) {
int a = edges[i][0];
int b = edges[i][1];
int w = edges[i][2];
uf.union(a, b);
if (!map.containsKey(a)) map.put(a, new ArrayList<>());
if (!map.containsKey(b)) map.put(b, new ArrayList<>());
map.get(a).add(new int[]{b, w});
map.get(b).add(new int[]{a, w});
}
Map<Integer, Integer> count = new HashMap<>();
for (int i = 0; i < n; i++) {
int index = uf.find(i);
int t = count.getOrDefault(index, -1);
for (int[] ints : map.getOrDefault(i, new ArrayList<>())) {
if (t == -1) {
t = ints[1];
} else {
t &= ints[1];
}
}
count.put(index, t);
}
int[] ans = new int[query.length];
for (int i = 0; i < query.length; i++) {
if (uf.find(query[i][0]) != uf.find(query[i][1])) {
ans[i] = -1;
} else if (query[i][0] == query[i][1]) {
ans[i] = 0;
} else {
ans[i] = count.get(uf.find(query[i][0]));
}
}
return ans;
}
}