-
Notifications
You must be signed in to change notification settings - Fork 0
/
wk304.java
176 lines (155 loc) · 4.73 KB
/
wk304.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
172
173
174
175
176
package weekly;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
public class wk304 {
//简单题,排序+模拟
//其实可以直接求不同数字个数
public int minimumOperations(int[] nums) {
Arrays.sort(nums);
int i = 0;
int max = nums[nums.length - 1];
int sum = 0;
int ans = 0;
while (sum < max) {
nums[i] -= sum;
if (nums[i] <= 0) {
i++;
} else {
sum += nums[i++];
ans++;
}
}
return ans;
}
//中等题,排序后开始拿1 2 3 ...个 要注意追后的时候要判断能不能构成一个新的组
public int maximumGroups(int[] grades) {
Arrays.sort(grades);
int pre = 0;
int count = 0;
int i = 0;
int ans = 0;
for (i = 0; i < grades.length; ) {
int pcount = 0;
int psum = 0;
while (i < grades.length && pcount <= count) {
psum += grades[i++];
pcount++;
}
if (psum <= pre || pcount <= count) {
return ans;
}
ans++;
pre = psum;
count = pcount;
}
return ans;
}
//中等题,图遍历就行了,找出每个点到所有点的距离
public int closestMeetingNode(int[] edges, int node1, int node2) {
int[] dis1 = help(edges, node1);
int[] dis2 = help(edges, node2);
int ans = -1;
int min = Integer.MAX_VALUE;
for (int i = 0; i < dis1.length; i++) {
if (dis1[i] == 0 || dis2[i] == 0) continue;
int max = Math.max(dis1[i], dis2[i]);
if (max < min) {
ans = i;
min = max;
}
}
return ans;
}
int[] help(int[] edges, int node) {
int[] dis = new int[edges.length];
int step = 0;
//没路可走 或者 已经访问过
while (node != -1 && dis[node] == 0) {
dis[node] = step;
node = edges[node];
step++;
}
return dis;
}
/* int[] help(int[] edges, int node) {
int[] dis = new int[edges.length];
Arrays.fill(dis, -1);
boolean[] visited = new boolean[edges.length];
Queue<Integer> queue = new LinkedList<>();
queue.add(node);
int step = 0;
visited[node] = true;
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
Integer cur = queue.poll();
dis[cur] = step;
int next = edges[cur];
if (next == -1 || visited[next]) continue;
queue.add(next);
visited[next] = true;
}
step++;
}
return dis;
}*/
//困难题,周赛写的解法
/*public int longestCycle(int[] edges) {
int[] in = new int[edges.length];
for (int i = 0; i < edges.length; i++) {
if (edges[i] == -1) continue;
in[edges[i]]++;
}
int max = -1;
boolean[] visited = new boolean[edges.length];
for (int i = 0; i < in.length; i++) {
max = Math.max(max, help2(edges, visited, i));
}
return max;
}
int help2(int[] edges, boolean[] visited, int node) {
List<Integer> path = new ArrayList<>();
while (!visited[node]) {
path.add(node);
visited[node] = true;
node = edges[node];
if (node == -1) break;
}
if (node == -1) return -1;
for (int i = 0; i < path.size(); i++) {
if (path.get(i) == node) {
return path.size() - i;
}
}
return -1;
}*/
//时间计时法,还可以拓扑排序删除不是坏的变,然后bfs求环长度
public int longestCycle(int[] edges) {
int[] dis = new int[edges.length];
int ans = -1;
int step = 1;
for (int i = 0; i < edges.length; i++) {
if (dis[i] == 0) {
int start = step;
int node = i;
while (node != -1 && dis[node] == 0) {
dis[node] = step++;
node = edges[node];
}
if (node != -1 && dis[node] >= start) {
ans = Math.max(step - dis[node], ans);
}
}
}
return ans;
}
public static void main(String[] args) {
wk304 w = new wk304();
w.longestCycle(new int[]{3, 3, 4, 2, 3});
}
}