-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3002-MaximumSizeOfASetAfterRemovals.go
More file actions
100 lines (88 loc) · 4.21 KB
/
3002-MaximumSizeOfASetAfterRemovals.go
File metadata and controls
100 lines (88 loc) · 4.21 KB
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
package main
// 3002. Maximum Size of a Set After Removals
// You are given two 0-indexed integer arrays nums1 and nums2 of even length n.
// You must remove n / 2 elements from nums1 and n / 2 elements from nums2.
// After the removals, you insert the remaining elements of nums1 and nums2 into a set s.
// Return the maximum possible size of the set s.
// Example 1:
// Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1]
// Output: 2
// Explanation: We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
// It can be shown that 2 is the maximum possible size of the set s after the removals.
// Example 2:
// Input: nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
// Output: 5
// Explanation: We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
// It can be shown that 5 is the maximum possible size of the set s after the removals.
// Example 3:
// Input: nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
// Output: 6
// Explanation: We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
// It can be shown that 6 is the maximum possible size of the set s after the removals.
// Constraints:
// n == nums1.length == nums2.length
// 1 <= n <= 2 * 10^4
// n is even.
// 1 <= nums1[i], nums2[i] <= 10^9
import "fmt"
func maximumSetSize(nums1 []int, nums2 []int) int {
n := len(nums1)
set1, set2 := make(map[int]bool, n), make(map[int]bool, n)
for i := 0; i < n; i++ {
set1[nums1[i]], set2[nums2[i]] = true, true
}
half := n / 2
for i := range set1 {
if len(set1) <= half { break }
if set2[i] {
delete(set1, i)
}
}
for i := range set1 {
if len(set1) > half {
delete(set1, i)
} else {
break
}
}
res, count := len(set1), 0
for i := range set2 {
if !set1[i] && count < half {
res++
count++
}
}
return res
}
func maximumSetSize1(nums1 []int, nums2 []int) int {
n := len(nums1)
all, set1, set2 := make(map[int]bool), make(map[int]bool), make(map[int]bool)
for i := 0; i < n; i++ {
set1[nums1[i]], all[nums1[i]], set2[nums2[i]], all[nums2[i]] = true, true, true, true
}
min := func (x, y int) int { if x < y { return x; }; return y; }
return min(len(all), min(n / 2, len(set1)) + min(n / 2, len(set2)))
}
func main() {
// Example 1:
// Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1]
// Output: 2
// Explanation: We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
// It can be shown that 2 is the maximum possible size of the set s after the removals.
fmt.Println(maximumSetSize([]int{1,2,1,2}, []int{1,1,1,1})) // 2
// Example 2:
// Input: nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
// Output: 5
// Explanation: We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
// It can be shown that 5 is the maximum possible size of the set s after the removals.
fmt.Println(maximumSetSize([]int{1,2,3,4,5,6}, []int{2,3,2,3,2,3})) // 5
// Example 3:
// Input: nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
// Output: 6
// Explanation: We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
// It can be shown that 6 is the maximum possible size of the set s after the removals.
fmt.Println(maximumSetSize([]int{1,1,2,2,3,3}, []int{4,4,5,5,6,6})) // 6
fmt.Println(maximumSetSize1([]int{1,2,1,2}, []int{1,1,1,1})) // 2
fmt.Println(maximumSetSize1([]int{1,2,3,4,5,6}, []int{2,3,2,3,2,3})) // 5
fmt.Println(maximumSetSize1([]int{1,1,2,2,3,3}, []int{4,4,5,5,6,6})) // 6
}