-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3046-SplitTheArray.go
More file actions
55 lines (46 loc) · 1.79 KB
/
3046-SplitTheArray.go
File metadata and controls
55 lines (46 loc) · 1.79 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
package main
// 3046. Split the Array
// You are given an integer array nums of even length.
// You have to split the array into two parts nums1 and nums2 such that:
// nums1.length == nums2.length == nums.length / 2.
// nums1 should contain distinct elements.
// nums2 should also contain distinct elements.
// Return true if it is possible to split the array, and false otherwise.
// Example 1:
// Input: nums = [1,1,2,2,3,4]
// Output: true
// Explanation: One of the possible ways to split nums is nums1 = [1,2,3] and nums2 = [1,2,4].
// Example 2:
// Input: nums = [1,1,1,1]
// Output: false
// Explanation: The only possible way to split nums is nums1 = [1,1] and nums2 = [1,1]. Both nums1 and nums2 do not contain distinct elements. Therefore, we return false.
// Constraints:
// 1 <= nums.length <= 100
// nums.length % 2 == 0
// 1 <= nums[i] <= 100
import "fmt"
func isPossibleToSplit(nums []int) bool {
mp := make(map[int]int)
for _, v := range nums {
mp[v]++
}
for _, v := range mp {
if v > 2 { // 超过两个说明分出的两个数组必然有至少两个相同的
return false
}
}
return true
}
func main() {
// Example 1:
// Input: nums = [1,1,2,2,3,4]
// Output: true
// Explanation: One of the possible ways to split nums is nums1 = [1,2,3] and nums2 = [1,2,4].
fmt.Println(isPossibleToSplit([]int{1,1,2,2,3,4})) // true
// Example 2:
// Input: nums = [1,1,1,1]
// Output: false
// Explanation: The only possible way to split nums is nums1 = [1,1] and nums2 = [1,1]. Both nums1 and nums2 do not contain distinct elements. Therefore, we return false.
fmt.Println(isPossibleToSplit([]int{1,1,1,1})) // false
fmt.Println(isPossibleToSplit([]int{1,2,3,4,5,6,7,8,9})) // true
}