-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwk379.java
136 lines (109 loc) · 3.43 KB
/
wk379.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
package weekly;
import java.util.HashSet;
import java.util.Set;
public class wk379 {
//遍历
public int areaOfMaxDiagonal(int[][] dimensions) {
int ans = 0;
int max = 0;
for (int[] dimension : dimensions) {
int val = dimension[0] * dimension[0] + dimension[1] * dimension[1];
if (val > max) {
max = val;
ans = dimension[0] * dimension[1];
} else if (val == max) {
ans = Math.max(dimension[0] * dimension[1], ans);
}
}
return ans;
}
boolean checkIfPointsAreOnDiagonalLine(int c, int d, int e, int f) {
int xDiff = Math.abs(c - e);
int yDiff = Math.abs(d - f);
return xDiff == yDiff;
}
// b是否在c中间
boolean checkInner(int a, int b, int c) {
if (a > c) {
if (b > c && b < a) return true;
} else {
if (b < c && b > a) return true;
}
return false;
}
int checkLine(int a, int b, int c, int d, int e, int f) {
int ans = Integer.MAX_VALUE;
if (a == e) {
if (c == e && checkInner(b, d, f)) {
ans = Math.min(ans, 2);
} else {
ans = Math.min(ans, 1);
}
} else if (b == f) {
if (d == f && checkInner(a, c, e)) {
ans = Math.min(ans, 2);
} else {
ans = Math.min(ans, 1);
}
} else {
ans = Math.min(ans, 2);
}
return ans;
}
//分类讨论 做麻了
public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) {
int ans = checkLine(a, b, c, d, e, f);
boolean b1 = checkIfPointsAreOnDiagonalLine(a, b, c, d);
boolean b2 = checkIfPointsAreOnDiagonalLine(c, d, e, f);
//象和后在一条对角线上
if (b2) {
if (b1) {
//在同一侧的对角线上
if ((a - e) * (c - e) > 0 && (b - f) * (d - f) > 0) {
int diffA = Math.abs(a - e);
int diffB = Math.abs(c - e);
if (diffA < diffB) {
} else {
ans = 1;
}
} else {
ans = 1;
}
} else {
ans = 1;
}
}
return ans;
}
//贪心
public int maximumSetSize(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
for (int i : nums1) {
set1.add(i);
}
for (int i : nums2) {
set2.add(i);
}
//找出set1和set2都存在的
Set<Integer> all = new HashSet<>();
for (Integer i : set1) {
if (set2.contains(i)) {
all.add(i);
}
}
//还需要删除这些
int need1 = Math.max(0, (set1.size()-nums1.length/2));
int need2 = Math.max(0, (set2.size() - nums2.length/2));
//能用公共的覆盖
if (all.size() >= need1 + need2) {
return set1.size() + set2.size() - all.size();
}else {
return set1.size()+set2.size()-need1-need2;
}
}
public static void main(String[] args) {
wk379 w = new wk379();
w.maximumSetSize(new int[]{1,2,3,4,5,6},new int[]{2,3,2,3,2,3});
}
}