Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cafb352

Browse files
feat: add solutions to lc problem: No.10037 (doocs#2199)
No.10037.Maximum Size of a Set After Removals
1 parent 610f831 commit cafb352

File tree

7 files changed

+335
-6
lines changed

7 files changed

+335
-6
lines changed

‎solution/10000-10099/10037.Maximum Size of a Set After Removals/README.md‎

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,137 @@
6262
<!-- 这里可写当前语言的特殊实现逻辑 -->
6363

6464
```python
65-
65+
class Solution:
66+
def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int:
67+
s1 = set(nums1)
68+
s2 = set(nums2)
69+
n = len(nums1)
70+
a = min(len(s1 - s2), n // 2)
71+
b = min(len(s2 - s1), n // 2)
72+
return min(a + b + len(s1 & s2), n)
6673
```
6774

6875
### **Java**
6976

7077
<!-- 这里可写当前语言的特殊实现逻辑 -->
7178

7279
```java
73-
80+
class Solution {
81+
public int maximumSetSize(int[] nums1, int[] nums2) {
82+
Set<Integer> s1 = new HashSet<>();
83+
Set<Integer> s2 = new HashSet<>();
84+
for (int x : nums1) {
85+
s1.add(x);
86+
}
87+
for (int x : nums2) {
88+
s2.add(x);
89+
}
90+
int n = nums1.length;
91+
int a = 0, b = 0, c = 0;
92+
for (int x : s1) {
93+
if (!s2.contains(x)) {
94+
++a;
95+
}
96+
}
97+
for (int x : s2) {
98+
if (!s1.contains(x)) {
99+
++b;
100+
} else {
101+
++c;
102+
}
103+
}
104+
a = Math.min(a, n / 2);
105+
b = Math.min(b, n / 2);
106+
return Math.min(a + b + c, n);
107+
}
108+
}
74109
```
75110

76111
### **C++**
77112

78113
```cpp
79-
114+
class Solution {
115+
public:
116+
int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {
117+
unordered_set<int> s1(nums1.begin(), nums1.end());
118+
unordered_set<int> s2(nums2.begin(), nums2.end());
119+
int n = nums1.size();
120+
int a = 0, b = 0, c = 0;
121+
for (int x : s1) {
122+
if (!s2.count(x)) {
123+
++a;
124+
}
125+
}
126+
for (int x : s2) {
127+
if (!s1.count(x)) {
128+
++b;
129+
} else {
130+
++c;
131+
}
132+
}
133+
a = min(a, n / 2);
134+
b = min(b, n / 2);
135+
return min(a + b + c, n);
136+
}
137+
};
80138
```
81139
82140
### **Go**
83141
84142
```go
143+
func maximumSetSize(nums1 []int, nums2 []int) int {
144+
s1 := map[int]bool{}
145+
s2 := map[int]bool{}
146+
for _, x := range nums1 {
147+
s1[x] = true
148+
}
149+
for _, x := range nums2 {
150+
s2[x] = true
151+
}
152+
a, b, c := 0, 0, 0
153+
for x := range s1 {
154+
if !s2[x] {
155+
a++
156+
}
157+
}
158+
for x := range s2 {
159+
if !s1[x] {
160+
b++
161+
} else {
162+
c++
163+
}
164+
}
165+
n := len(nums1)
166+
a = min(a, n/2)
167+
b = min(b, n/2)
168+
return min(a+b+c, n)
169+
}
170+
```
85171

172+
### **TypeScript**
173+
174+
```ts
175+
function maximumSetSize(nums1: number[], nums2: number[]): number {
176+
const s1: Set<number> = new Set(nums1);
177+
const s2: Set<number> = new Set(nums2);
178+
const n = nums1.length;
179+
let [a, b, c] = [0, 0, 0];
180+
for (const x of s1) {
181+
if (!s2.has(x)) {
182+
++a;
183+
}
184+
}
185+
for (const x of s2) {
186+
if (!s1.has(x)) {
187+
++b;
188+
} else {
189+
++c;
190+
}
191+
}
192+
a = Math.min(a, n >> 1);
193+
b = Math.min(b, n >> 1);
194+
return Math.min(a + b + c, n);
195+
}
86196
```
87197

88198
### **...**

‎solution/10000-10099/10037.Maximum Size of a Set After Removals/README_EN.md‎

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,135 @@ It can be shown that 6 is the maximum possible size of the set s after the remov
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int:
60+
s1 = set(nums1)
61+
s2 = set(nums2)
62+
n = len(nums1)
63+
a = min(len(s1 - s2), n // 2)
64+
b = min(len(s2 - s1), n // 2)
65+
return min(a + b + len(s1 & s2), n)
5966
```
6067

6168
### **Java**
6269

6370
```java
64-
71+
class Solution {
72+
public int maximumSetSize(int[] nums1, int[] nums2) {
73+
Set<Integer> s1 = new HashSet<>();
74+
Set<Integer> s2 = new HashSet<>();
75+
for (int x : nums1) {
76+
s1.add(x);
77+
}
78+
for (int x : nums2) {
79+
s2.add(x);
80+
}
81+
int n = nums1.length;
82+
int a = 0, b = 0, c = 0;
83+
for (int x : s1) {
84+
if (!s2.contains(x)) {
85+
++a;
86+
}
87+
}
88+
for (int x : s2) {
89+
if (!s1.contains(x)) {
90+
++b;
91+
} else {
92+
++c;
93+
}
94+
}
95+
a = Math.min(a, n / 2);
96+
b = Math.min(b, n / 2);
97+
return Math.min(a + b + c, n);
98+
}
99+
}
65100
```
66101

67102
### **C++**
68103

69104
```cpp
70-
105+
class Solution {
106+
public:
107+
int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {
108+
unordered_set<int> s1(nums1.begin(), nums1.end());
109+
unordered_set<int> s2(nums2.begin(), nums2.end());
110+
int n = nums1.size();
111+
int a = 0, b = 0, c = 0;
112+
for (int x : s1) {
113+
if (!s2.count(x)) {
114+
++a;
115+
}
116+
}
117+
for (int x : s2) {
118+
if (!s1.count(x)) {
119+
++b;
120+
} else {
121+
++c;
122+
}
123+
}
124+
a = min(a, n / 2);
125+
b = min(b, n / 2);
126+
return min(a + b + c, n);
127+
}
128+
};
71129
```
72130
73131
### **Go**
74132
75133
```go
134+
func maximumSetSize(nums1 []int, nums2 []int) int {
135+
s1 := map[int]bool{}
136+
s2 := map[int]bool{}
137+
for _, x := range nums1 {
138+
s1[x] = true
139+
}
140+
for _, x := range nums2 {
141+
s2[x] = true
142+
}
143+
a, b, c := 0, 0, 0
144+
for x := range s1 {
145+
if !s2[x] {
146+
a++
147+
}
148+
}
149+
for x := range s2 {
150+
if !s1[x] {
151+
b++
152+
} else {
153+
c++
154+
}
155+
}
156+
n := len(nums1)
157+
a = min(a, n/2)
158+
b = min(b, n/2)
159+
return min(a+b+c, n)
160+
}
161+
```
76162

163+
### **TypeScript**
164+
165+
```ts
166+
function maximumSetSize(nums1: number[], nums2: number[]): number {
167+
const s1: Set<number> = new Set(nums1);
168+
const s2: Set<number> = new Set(nums2);
169+
const n = nums1.length;
170+
let [a, b, c] = [0, 0, 0];
171+
for (const x of s1) {
172+
if (!s2.has(x)) {
173+
++a;
174+
}
175+
}
176+
for (const x of s2) {
177+
if (!s1.has(x)) {
178+
++b;
179+
} else {
180+
++c;
181+
}
182+
}
183+
a = Math.min(a, n >> 1);
184+
b = Math.min(b, n >> 1);
185+
return Math.min(a + b + c, n);
186+
}
77187
```
78188

79189
### **...**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {
4+
unordered_set<int> s1(nums1.begin(), nums1.end());
5+
unordered_set<int> s2(nums2.begin(), nums2.end());
6+
int n = nums1.size();
7+
int a = 0, b = 0, c = 0;
8+
for (int x : s1) {
9+
if (!s2.count(x)) {
10+
++a;
11+
}
12+
}
13+
for (int x : s2) {
14+
if (!s1.count(x)) {
15+
++b;
16+
} else {
17+
++c;
18+
}
19+
}
20+
a = min(a, n / 2);
21+
b = min(b, n / 2);
22+
return min(a + b + c, n);
23+
}
24+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
func maximumSetSize(nums1 []int, nums2 []int) int {
2+
s1 := map[int]bool{}
3+
s2 := map[int]bool{}
4+
for _, x := range nums1 {
5+
s1[x] = true
6+
}
7+
for _, x := range nums2 {
8+
s2[x] = true
9+
}
10+
a, b, c := 0, 0, 0
11+
for x := range s1 {
12+
if !s2[x] {
13+
a++
14+
}
15+
}
16+
for x := range s2 {
17+
if !s1[x] {
18+
b++
19+
} else {
20+
c++
21+
}
22+
}
23+
n := len(nums1)
24+
a = min(a, n/2)
25+
b = min(b, n/2)
26+
return min(a+b+c, n)
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int maximumSetSize(int[] nums1, int[] nums2) {
3+
Set<Integer> s1 = new HashSet<>();
4+
Set<Integer> s2 = new HashSet<>();
5+
for (int x : nums1) {
6+
s1.add(x);
7+
}
8+
for (int x : nums2) {
9+
s2.add(x);
10+
}
11+
int n = nums1.length;
12+
int a = 0, b = 0, c = 0;
13+
for (int x : s1) {
14+
if (!s2.contains(x)) {
15+
++a;
16+
}
17+
}
18+
for (int x : s2) {
19+
if (!s1.contains(x)) {
20+
++b;
21+
} else {
22+
++c;
23+
}
24+
}
25+
a = Math.min(a, n / 2);
26+
b = Math.min(b, n / 2);
27+
return Math.min(a + b + c, n);
28+
}
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int:
3+
s1 = set(nums1)
4+
s2 = set(nums2)
5+
n = len(nums1)
6+
a = min(len(s1 - s2), n // 2)
7+
b = min(len(s2 - s1), n // 2)
8+
return min(a + b + len(s1 & s2), n)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function maximumSetSize(nums1: number[], nums2: number[]): number {
2+
const s1: Set<number> = new Set(nums1);
3+
const s2: Set<number> = new Set(nums2);
4+
const n = nums1.length;
5+
let [a, b, c] = [0, 0, 0];
6+
for (const x of s1) {
7+
if (!s2.has(x)) {
8+
++a;
9+
}
10+
}
11+
for (const x of s2) {
12+
if (!s1.has(x)) {
13+
++b;
14+
} else {
15+
++c;
16+
}
17+
}
18+
a = Math.min(a, n >> 1);
19+
b = Math.min(b, n >> 1);
20+
return Math.min(a + b + c, n);
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /