-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck if Array is Good.py
48 lines (34 loc) · 1.32 KB
/
Check if Array is Good.py
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
'''
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.
'''
class Solution:
def isGood(self, nums: List[int]) -> bool:
n =max(nums)
basen = [i for i in range(1,n)]
basen.append(n)
basen.append(n)
s1 = Counter(nums)
s2 = Counter(basen)
if len(basen)!= len(nums):
return False
for k,v in s1.items():
if s1[k] != s2[k]:
return False
return True
-------------------------------------------------------------------
class Solution:
def isGood(self, nums: List[int]) -> bool:
base = len(nums)-1
hashmap = defaultdict(int)
for i in nums:
if i > base:
return False
hashmap[i]+=1
if i!=base and hashmap[i]>1:
return False
if hashmap[base]>2:
return False
return True