-
Notifications
You must be signed in to change notification settings - Fork 0
/
wk365.java
120 lines (101 loc) · 3.28 KB
/
wk365.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
package weekly;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class wk365 {
/* public long maximumTripletValue(int[] nums) {
long max = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
for (int k = j + 1; k < nums.length; k++) {
max = Math.max(max, ((long) nums[i] - nums[j]) * nums[k]);
}
}
}
return max;
}*/
//枚举
public long maximumTripletValue(int[] nums) {
long max = 0;
long sub = 0;
long ans = 0;
for (int i = 0; i < nums.length; i++) {
ans = Math.max(ans, sub * nums[i]);
sub = Math.max(sub, max - nums[i]);
max = Math.max(max, nums[i]);
}
return ans;
}
//滑动窗口+分类
public int minSizeSubarray(int[] nums, int target) {
long total = 0;
for (int x : nums) total += x;
int n = nums.length;
int ans = Integer.MAX_VALUE;
int left = 0;
long sum = 0;
for (int right = 0; right < n * 2; right++) {
sum += nums[right % n];
while (sum > target % total) {
sum -= nums[left++ % n];
}
if (sum == target % total) {
ans = Math.min(ans, right - left + 1);
}
}
return ans == Integer.MAX_VALUE ? -1 : ans + (int) (target / total) * n;
}
//内向基环树
static public int[] countVisitedNodes(List<Integer> edges) {
int[] ans = new int[edges.size()];
int[] in = new int[edges.size()];
for (Integer edge : edges) {
in[edge]++;
}
for (int i = 0; i < edges.size(); i++) {
int cur = i;
if (ans[cur] != 0) continue;
int time = 0;
Map<Integer, Integer> set = new HashMap<>();
List<Integer> list = new ArrayList<>();
set.put(cur, time);
list.add(cur);
while (true) {
time++;
cur = edges.get(cur);
//之前计算过了
if (ans[cur] != 0) {
for (int j = 0; j < list.size(); j++) {
ans[list.get(j)] = ans[cur] + list.size() - j;
}
break;
}
//存在圈
if (set.containsKey(cur)) {
int circleIn = set.get(cur);
int circle = time - circleIn;
for (int j = 0; j < list.size(); j++) {
//在圈之前
if (j < circleIn) {
ans[list.get(j)] = circle + circleIn - j;
//在圈里
} else {
ans[list.get(j)] = circle;
}
}
break;
}
set.put(cur, time);
list.add(cur);
}
}
return ans;
}
public static void main(String[] args) {
countVisitedNodes(Arrays.asList(1,2,0,0));
}
}