-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2870-MinimumNumberOfOperationsToMakeArrayEmpty.go
More file actions
115 lines (100 loc) · 3.95 KB
/
2870-MinimumNumberOfOperationsToMakeArrayEmpty.go
File metadata and controls
115 lines (100 loc) · 3.95 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
// 2870. Minimum Number of Operations to Make Array Empty
// You are given a 0-indexed array nums consisting of positive integers.
// There are two types of operations that you can apply on the array any number of times:
// 1. Choose two elements with equal values and delete them from the array.
// 2. Choose three elements with equal values and delete them from the array.
// Return the minimum number of operations required to make the array empty, or -1 if it is not possible.
// Example 1:
// Input: nums = [2,3,3,2,2,4,2,3,4]
// Output: 4
// Explanation: We can apply the following operations to make the array empty:
// - Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
// - Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
// - Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
// - Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
// It can be shown that we cannot make the array empty in less than 4 operations.
// Example 2:
// Input: nums = [2,1,2,2,3,3]
// Output: -1
// Explanation: It is impossible to empty the array.
// Constraints:
// 2 <= nums.length <= 10^5
// 1 <= nums[i] <= 10^6
// Note: This question is the same as 2244: Minimum Rounds to Complete All Tasks.
import "fmt"
func minOperations(nums []int) int {
res, mp := 0, make(map[int]int)
for _, v := range nums {
mp[v]++
}
for _, v := range mp {
if v == 1 {
return -1
}
if v % 3 != 1 {
res += (v + 1) / 3
} else { // 余1的情况 3 + 1 分成 2 + 2 来处理
res += (v + 1) / 3 + 1
}
}
return res
}
func minOperations1(nums []int) int {
res, mp := 0, make(map[int]int)
for _, v := range nums {
mp[v]++
}
for _, v := range mp {
if v == 1 {
return -1
}
rem, val := v % 3, v / 3
if rem == 0 {
res += val
} else {
res += val + 1
}
}
return res
}
func minOperations2(nums []int) int {
res, mp := 0, make(map[int]int)
for _, v := range nums {
mp[v]++
}
for _, v := range mp {
if v == 1 {
return -1
}
res += (v + 2) / 3
}
return res
}
func main() {
// Example 1:
// Input: nums = [2,3,3,2,2,4,2,3,4]
// Output: 4
// Explanation: We can apply the following operations to make the array empty:
// - Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
// - Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
// - Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
// - Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
// It can be shown that we cannot make the array empty in less than 4 operations.
fmt.Println(minOperations([]int{2,3,3,2,2,4,2,3,4})) // 4
// Example 2:
// Input: nums = [2,1,2,2,3,3]
// Output: -1
// Explanation: It is impossible to empty the array.
fmt.Println(minOperations([]int{2,1,2,2,3,3})) // -1
fmt.Println(minOperations([]int{1,2,3,4,5,6,7,8,9})) // -1
fmt.Println(minOperations([]int{9,8,7,6,5,4,3,2,1})) // -1
fmt.Println(minOperations1([]int{2,3,3,2,2,4,2,3,4})) // 4
fmt.Println(minOperations1([]int{2,1,2,2,3,3})) // -1
fmt.Println(minOperations1([]int{1,2,3,4,5,6,7,8,9})) // -1
fmt.Println(minOperations1([]int{9,8,7,6,5,4,3,2,1})) // -1
fmt.Println(minOperations2([]int{2,3,3,2,2,4,2,3,4})) // 4
fmt.Println(minOperations2([]int{2,1,2,2,3,3})) // -1
fmt.Println(minOperations2([]int{1,2,3,4,5,6,7,8,9})) // -1
fmt.Println(minOperations2([]int{9,8,7,6,5,4,3,2,1})) // -1
}