-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2784-CheckIfArrayIsGood.go
More file actions
102 lines (87 loc) · 4.63 KB
/
2784-CheckIfArrayIsGood.go
File metadata and controls
102 lines (87 loc) · 4.63 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
package main
// 2784. Check if Array is Good
// You are given an integer array nums.
// We consider an array good if it is a permutation of an array base[n].
// base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrences of n).
// For example, base[1] = [1, 1] and base[3] = [1, 2, 3, 3].
// Return true if the given array is good, otherwise return false.
// Note: A permutation of integers represents an arrangement of these numbers.
// Example 1:
// Input: nums = [2, 1, 3]
// Output: false
// Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false.
// Example 2:
// Input: nums = [1, 3, 3, 2]
// Output: true
// Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true.
// Example 3:
// Input: nums = [1, 1]
// Output: true
// Explanation: Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true.
// Example 4:
// Input: nums = [3, 4, 4, 1, 2, 1]
// Output: false
// Explanation: Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false.
// Constraints:
// 1 <= nums.length <= 100
// 1 <= num[i] <= 200
import "fmt"
import "sort"
func isGood(nums []int) bool {
sort.Ints(nums)
n := len(nums) - 1
if nums[n] != n { return false }
for i := 0; i < n; i++ {
if nums[i] != i + 1 {
return false
}
}
return true
}
func isGood1(nums []int) bool {
mp := make(map[int]int)
mx, mul, n := 0, 0, len(nums)
for _, v := range nums {
mp[v]++
if mx < v {
mx = v
}
if mp[v] > 1 {
mul = v
}
}
if n == len(mp) + 1 && mx == len(mp) && mul == mx {
return true
}
return false
}
func main() {
// Example 1:
// Input: nums = [2, 1, 3]
// Output: false
// Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false.
fmt.Println(isGood([]int{2, 1, 3})) // false
// Example 2:
// Input: nums = [1, 3, 3, 2]
// Output: true
// Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true.
fmt.Println(isGood([]int{1, 3, 3, 2})) // true
// Example 3:
// Input: nums = [1, 1]
// Output: true
// Explanation: Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true.
fmt.Println(isGood([]int{1, 1})) // true
// Example 4:
// Input: nums = [3, 4, 4, 1, 2, 1]
// Output: false
// Explanation: Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false.
fmt.Println(isGood([]int{3, 4, 4, 1, 2, 1})) // false
fmt.Println(isGood([]int{1,2,3,4,5,6,7,8,9})) // false
fmt.Println(isGood([]int{9,8,7,6,5,4,3,2,1})) // false
fmt.Println(isGood1([]int{2, 1, 3})) // false
fmt.Println(isGood1([]int{1, 3, 3, 2})) // true
fmt.Println(isGood1([]int{1, 1})) // true
fmt.Println(isGood1([]int{3, 4, 4, 1, 2, 1})) // false
fmt.Println(isGood1([]int{1,2,3,4,5,6,7,8,9})) // false
fmt.Println(isGood1([]int{9,8,7,6,5,4,3,2,1})) // false
}