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 trueif the given array is good, otherwise return __false.
Note: A permutation of integers represents an arrangement of these numbers.
Input: nums =[2,1,3]Output: falseExplanation: Since the maximum element of the array is3, 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 isfalse.
Input: nums =[1,3,3,2]Output: trueExplanation: Since the maximum element of the array is3, 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 istrue.
Input: nums =[1,1]Output: trueExplanation: Since the maximum element of the array is1, 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 istrue.
Input: nums =[3,4,4,1,2,1]Output: falseExplanation: Since the maximum element of the array is4, 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 isfalse.
The array is good if it is a permutation of [1, 2, …, n-1, n, n], i.e., it contains all numbers from 1 to n-1 exactly once, and n exactly twice. We can check this by counting the frequency of each number and comparing with the expected pattern.
classSolution {
public:bool isGood(vector<int>& nums) {
int n =*max_element(nums.begin(), nums.end());
if (nums.size() != n +1) return false;
vector<int> cnt(n +1);
for (int x : nums) cnt[x]++;
for (int i =1; i < n; ++i) if (cnt[i] !=1) return false;
return cnt[n] ==2;
}
};
1
2
3
4
5
6
7
8
9
10
11
funcisGood(nums []int) bool {
n:=0for_, v:=rangenums {
ifv > n { n = v }
}
if len(nums) !=n+1 { returnfalse }
cnt:= make([]int, n+1)
for_, v:=rangenums { cnt[v]++ }
fori:=1; i < n; i++ { ifcnt[i] !=1 { returnfalse } }
returncnt[n] ==2}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
publicbooleanisGood(int[] nums) {
int n = 0;
for (int x : nums) n = Math.max(n, x);
if (nums.length!= n + 1) returnfalse;
int[] cnt =newint[n + 1];
for (int x : nums) cnt[x]++;
for (int i = 1; i < n; ++i) if (cnt[i]!= 1) returnfalse;
return cnt[n]== 2;
}
}
1
2
3
4
5
6
7
8
9
10
classSolution {
funisGood(nums: IntArray): Boolean {
val n = nums.maxOrNull() ?:0if (nums.size != n + 1) returnfalseval cnt = IntArray(n + 1)
for (x in nums) cnt[x]++for (i in1 until n) if (cnt[i] !=1) returnfalsereturn cnt[n] ==2 }
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
defisGood(self, nums: list[int]) -> bool:
n = max(nums)
if len(nums) != n +1:
returnFalse cnt = [0] * (n +1)
for x in nums:
cnt[x] +=1for i in range(1, n):
if cnt[i] !=1:
returnFalsereturn cnt[n] ==2
1
2
3
4
5
6
7
8
9
10
impl Solution {
pubfnis_good(nums: Vec<i32>) -> bool {
let n =*nums.iter().max().unwrap();
if nums.len() asi32!= n +1 { returnfalse; }
letmut cnt =vec![0; (n +1) asusize];
for&x in&nums { cnt[x asusize] +=1; }
for i in1..n asusize { if cnt[i] !=1 { returnfalse; } }
cnt[n asusize] ==2 }
}