inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import Counter
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
if not A: return True
c = Counter(A)
keys = sorted(c.keys())
for k in keys:
if k == 0:
if c[k] % 2 == 0:
c[k] = 0
else:
return False
if k < 0 and c[k] > 0:
if k/2 in c:
c[k/2] -= c[k]
c[k] = 0
if k > 0 and c[k] > 0:
if k*2 in c:
c[k*2] -= c[k]
c[k] = 0
return set(c.values()) == {0}
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
cache=collections.Counter(A)
c_list=sorted(list(cache),key=lambda x: (abs(x),x))
for x in c_list:
if cache[x]>cache[2*x]:
return False
cache[2*x]-=cache[x]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
d = collections.Counter(A)
if 0 in d:
if d[0] & 1 == 1:
return False
d.pop(0)
keys = sorted(d.keys())
for i in keys:
if i in d:
if i < 0:
if i / 2 not in d:
return False
if d[i] > d[i/2]:
return False
d[i/2] -= d[i]
if d[i/2] == 0:
d.pop(i/2)
d.pop(i)
else:
if i * 2 not in d:
return False
if d[i] > d[i*2]:
return False
d[i*2] -= d[i]
if d[i*2] == 0:
d.pop(i*2)
d.pop(i)
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
B = OrderedDict()
for x in A:
if x in B:
B[x] += 1
else:
B[x] = 1
while B:
x = next(iter(B))
freq = B.pop(x)
#print(x,freq)
if x<0:
if x%2:
return False
if (x//2 not in B) or B[x//2]<freq:
return False
B[x//2] -= freq
if B[x//2] == 0:
B.pop(x//2)
elif x==0:
if freq%2:
return False
else:
if (x*2 not in B) or B[x*2]<freq:
return False
B[x*2] -= freq
if B[x*2] == 0:
B.pop(x*2)
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
c = collections.Counter(A)
cc = sorted(c, key=abs)
#print(cc)
for x in cc:
#print(str(x), end=\": \")
#print(c)
if c[x] > c[2 * x]:
return False
c[2 * x] -= c[x]
#print(c)
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
neg = dict()
pos = dict()
zero = 0
for n in A:
if n < 0:
if -n not in neg:
neg[-n] = 0
neg[-n] += 1
elif n >0:
if n not in pos:
pos[n] = 0
pos[n] += 1
else:
zero += 1
if zero % 2 != 0:
return False
#print(pos,neg)
def helper(nums):
num_sorted = sorted(list(nums.keys()),reverse = True)
while num_sorted:
a = num_sorted.pop()
if nums[a] == 0:
continue
if 2*a not in nums or nums[a]>nums[2*a]:
return False
nums[2*a] -= nums[a]
return True
return helper(pos) and helper(neg)
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import defaultdict
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
s = defaultdict(int)
for a in A:
if a <= 0:
if 2 * a in s:
s[2*a] -= 1
if not s[2*a]:
del s[2*a]
else:
s[a] += 1
else:
if a % 2 == 0 and a // 2 in s:
s[a // 2] -= 1
if not s[a // 2]:
del s[a // 2]
else:
s[a] += 1
return not s
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
hashtable = {}
zero_count = 0
for i in range(len(A)):
if A[i]==0:
zero_count+=1
continue
if A[i] in list(hashtable.keys()):
hashtable[A[i]]+=1
else:
hashtable[A[i]]=1
if zero_count%2==1:
return False
else:
key_list = []
pair_count = 0
for key in list(hashtable.keys()):
key_list.append(key)
key_list.sort()
for key in key_list:
if key%2==0 and key//2 in list(hashtable.keys()):
m = min(hashtable[key],hashtable[key//2])
hashtable[key]-=m
hashtable[key//2]-=m
pair_count+=m
if pair_count*2 + zero_count==len(A):
return True
else:
return False
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import Counter
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
# O(NlogN) time
# O(N) space
if not A:
return True
counts = Counter(A)
for key in sorted(counts.keys()):
if counts[key] == 0:
continue
factor = .5 if key < 0 else 2
if counts[key] > counts[factor*key]:
return False
counts[factor*key] -= counts[key]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort(key=lambda x: abs(x))
freq = dict()
for i in A:
freq[i] = freq.get(i, 0) + 1
for k in sorted(list(freq.keys()), key=lambda x: abs(x)):
if freq[k] == 0: continue
if freq[k] > freq.get(2*k, 0):
return False
freq[2*k] -= freq[k]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
D = {}
for x in A:
D[x] = D.get(x, 0) + 1
D = dict([x for x in sorted(list(D.items()), key =lambda x:x[0] )])
for x in D:
while D[x] > 0:
D[x] -= 1
if x <= 0:
pair_x = x / 2
else:
pair_x = x * 2
if D.get(pair_x, 0) > 0:
D[pair_x] -= 1
else:
return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
cnt = collections.Counter(A)
for k in sorted(cnt, key=abs):
a = cnt[k]
if cnt[k] == 0:
continue
b = cnt.get(2 * k, 0)
if b < a:
return False
cnt[2 * k] -= a
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
def update_map(count, small, large):
small_count = count[small]
large_count = count[large]
min_count = min(small_count, large_count)
count[small] += -min_count
if small != large:
count[large] += -min_count
if not A:
return True
element_count = {}
ordered_elements = list(set(A))
ordered_elements.sort()
for number in A:
if number not in element_count:
element_count[number] = 0
element_count[number] += 1
for number in ordered_elements:
number_count = element_count[number]
double_number = number * 2
if double_number in element_count:
update_map(element_count, number, double_number)
for number in element_count:
if element_count[number] != 0:
return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import Counter
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
c=Counter(A)
A=sorted(A)
for num,v in sorted(list(c.items()), key= lambda item:item[0]):
if num>0 and c[num]>0:
if c[num]!=0 and c[num]>c[2*num]:
return False
c[2*num]-=c[num]
c[num]=0
elif num<0 and c[num]>0:
if num%2==1:
return False
if c[num]!=0 and c[num]>c[num//2]:
return False
c[num//2]-=c[num]
c[num]=0
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
if not A:
return True
positive_heap = []
negative_heap = []
zero = 0
positive_d = defaultdict(int)
negative_d = defaultdict(int)
for i in A:
if i == 0:
zero += 1
elif i < 0:
heappush(negative_heap, -i)
negative_d[-i] += 1
else:
heappush(positive_heap, i)
positive_d[i] += 1
if zero % 2 != 0:
return False
if not self.check(positive_heap, positive_d):
return False
if not self.check(negative_heap, negative_d):
return False
return True
def check(self, h, d):
for _ in range(len(h)):
i = heappop(h)
if d[i] == 0:
continue
if 2*i not in d:
return False
elif d[2*i] < d[i]:
return False
else:
d[2*i] -= d[i]
d[i] = 0
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
zcount = 0
pos = []
neg = []
for n in A:
if n==0:
zcount+=1
elif n>0:
pos.append(n)
else:
neg.append(-n)
def verifypos(A):
count = collections.Counter(A)
for x in sorted(A, key = abs):
if count[x] == 0: continue
if count[2*x] == 0: return False
count[x] -= 1
count[2*x] -= 1
return True
return verifypos(pos) and verifypos(neg)
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = collections.Counter(A)
for x in sorted(A, key=abs):
if count[x] == 0:
continue
if x == 0 and x % 2:
return False
if count[x] > count[2 * x]:
return False
count[x], count[2 * x] = 0, count[2 * x] - count[x]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
D = {}
for x in A:
D[x] = D.get(x, 0) + 1
D = dict([kv for kv in sorted(list(D.items()), key =lambda x:x[0] )])
for x in D:
while D[x] > 0:
D[x] -= 1
if x <= 0:
pair_x = x / 2
else:
pair_x = x * 2
if D.get(pair_x, 0) > 0:
D[pair_x] -= 1
else:
return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
dic = {}
for i in A:
if i % 2 == 0 and i/2 in dic:
i = i / 2
dic[i] -= 1
if dic[i] == 0:
del dic[i]
elif i * 2 in dic:
i = i * 2
dic[i] -= 1
if dic[i] == 0:
del dic[i]
else:
dic[i] = dic.setdefault(i, 0) + 1
continue
return len(dic) == 0
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
# https://blog.csdn.net/fuxuemingzhu/article/details/84925747
A.sort()
N = len(A)
count = collections.Counter(A)
for i in range(N):
if A[i] == 0 or A[i] not in count: continue
elif A[i] < 0:
if A[i] % 2 == 1 or count[A[i] / 2] == 0:
return False
else:
count[A[i] / 2] -= count[A[i]]
if count[A[i] / 2] == 0:
del count[A[i] / 2]
del count[A[i]]
else:
if count[A[i] * 2] == 0:
return False
else:
count[A[i] * 2] -= count[A[i]]
if count[A[i] * 2] == 0:
del count[A[i] * 2]
del count[A[i]]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
c = collections.Counter(A)
for x in sorted(c):
if c[x] == 0: continue
if c[x] < 0:
return False
if x == 0:
if c[x] % 2 != 0:
return False
else:
continue
if x > 0:
temp = 2*x
elif x % 2:
return False
else:
temp = x//2
if c[temp] < c[x]:
return False
else:
c[temp] -= c[x]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
pos_set = set()
neg_set = set()
pos_dict = {}
neg_dict = {}
pos_count = 0
for num in A:
if num >= 0:
pos_set.add(num)
pos_dict[num] = 1 + pos_dict.get(num,0)
pos_count += 1
else:
neg_set.add(abs(num))
neg_dict[abs(num)] = 1 + neg_dict.get(abs(num),0)
if pos_count % 2 != 0:
return False
else:
return self.helper(pos_set,pos_dict) and self.helper(neg_set,neg_dict)
def helper(self,set_,dict_):
sorted_ = sorted(list(set_))
for num in sorted_:
if num * 2 in sorted_ and num != 0:
small_ = dict_[num]
large_ = dict_[num * 2]
usage = min(small_,large_)
dict_[num] -= usage
dict_[num * 2] -= usage
elif num == 0:
if dict_[0] % 2 != 0:
return False
else:
dict_[0] = 0
for key in dict_:
if dict_[key] != 0:
return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import Counter
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
if not A: return True
C = Counter(A)
K = sorted(C.keys())
for k in K:
if k == 0:
if C[k] & 1:
return False
else:
C[k] = 0
if k < 0 and C[k] > 0:
if k % 2 == 0 and (k // 2 in C):
C[k//2] -= C[k]
C[k] = 0
if k > 0 and C[k] > 0:
if k * 2 in C:
C[k*2] -= C[k]
C[k] = 0
return set(C.values()) == {0}
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
c = Counter(A)
for n in sorted(list(c.keys()), key=abs):
while c[n] > 0 and c[(double := 2 * n)] > 0:
c[n] -= 1
c[double] -= 1
return all(not v for v in list(c.values()))
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
def factorize(num):
power = 0
while num % 2 == 0:
num, power = num//2, power+1
return (num, power)
counts = defaultdict(lambda : [0]*17)
zeros = 0
for a in A:
if a == 0:
zeros += 1
else:
n, p = factorize(a)
counts[n][p] += 1
if zeros % 2 == 1:
return False
for key in counts:
carry = 0
for p in counts[key]:
carry = p - carry
if carry < 0: return False
if carry != 0: return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
# https://www.cnblogs.com/seyjs/p/10092569.html
A.sort()
dic = {}
for i in A:
if i % 2 == 0 and i/2 in dic:
i = i / 2
dic[i] -= 1
if dic[i] == 0:
del dic[i]
elif i * 2 in dic:
i = i * 2
dic[i] -= 1
if dic[i] == 0:
del dic[i]
else:
dic[i] = dic.setdefault(i, 0) + 1
continue
return len(dic) == 0
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort(key=lambda x: abs(x))
c={}
for v in A:
if v/2 in c:
c[v/2]-=1
if not c[v/2]:
del c[v/2]
else:
if v not in c: c[v]=0
c[v]+=1
return len(c)==0
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
import collections
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
neg = collections.Counter()
pos = collections.Counter()
for n in A:
if n >= 0:
pos[n] += 1
else:
neg[n] += 1
for n in sorted(neg, reverse = True):
if neg[n*2] < neg[n]:
return False
neg[n*2] -= neg[n]
del neg[n]
if neg[n*2] == 0:
del neg[n*2]
if len(neg) != 0:
return False
for n in sorted(pos):
if pos[n*2] < pos[n]:
return False
pos[n*2] -= pos[n]
del pos[n]
if pos[n*2] == 0:
del pos[n*2]
if len(pos) != 0:
return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
freq = dict()
for i in A:
freq[i] = freq.get(i, 0) + 1
for i in A:
if freq.get(i, 0) == 0: continue
if i < 0:
if i % 2 != 0:
return False
elif not freq.get(i // 2, 0) > 0:
return False
elif i > 0 and not freq.get(i * 2, 0) > 0:
return False
if i < 0:
freq[i // 2] -= 1
else:
freq[i*2] -=1
freq[i] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import Counter
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
negs = [a for a in A if a < 0]
pos = [a for a in A if a > 0]
zeros = [a for a in A if a == 0]
if any(map(lambda x: len(x) % 2 != 0, [negs, pos, zeros])):
return False
if not self.is_valid(negs, True) or not self.is_valid(pos, False):
return False
return True
def is_valid(self, A, neg=False):
A = sorted(A)
N = len(A)
if neg:
A = A[::-1]
c = Counter(A)
for a in A:
if c[a] == 0:
continue
target = a * 2
if c[target] == 0:
return False
c[a] -= 1
c[target] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import defaultdict
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
arr = defaultdict(int)
for item in A:
if 2*item in arr and arr[2*item] > 0:
arr[2*item] -= 1
elif item % 2 == 0 and (item // 2) in arr and arr[item // 2] > 0:
arr[item // 2] -= 1
else:
arr[item] += 1
return sum([x for x in arr.values() if x != 0]) == 0
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = collections.Counter(A)
for x in sorted(A, key = abs):
if count[x] == 0: continue
if count[2*x] == 0: return False
count[x] -= 1
count[2*x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = Counter(A)
for x in sorted(A, key = abs):
if count[x]==0: continue # x may be end of a pair
if count[2*x]==0:
return False # x exsist, we have to match it with a 2x
count[2*x] -= 1
count[x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
cache=Counter(A)
c_list=sorted(list(cache),key=abs)
for x in c_list:
if cache[x]>cache[2*x]:
return False
cache[2*x]-=cache[x]
return True
'''
if not A:
return True
positive_heap = []
negative_heap = []
zero = 0
positive_d = defaultdict(int)
negative_d = defaultdict(int)
for i in A:
if i == 0:
zero += 1
elif i < 0:
heappush(negative_heap, -i)
negative_d[-i] += 1
else:
heappush(positive_heap, i)
positive_d[i] += 1
if zero % 2 != 0:
return False
if not self.check(positive_heap, positive_d):
return False
if not self.check(negative_heap, negative_d):
return False
return True
def check(self, h, d):
for _ in range(len(h)):
i = heappop(h)
if d[i] == 0:
continue
if 2*i not in d:
return False
elif d[2*i] < d[i]:
return False
else:
d[2*i] -= d[i]
d[i] = 0
return True
'''
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
table = collections.defaultdict(int)
for num in A:
table[num] += 1
A.sort()
for num in A:
if table[num] == 0:
continue
pair = num * 2
if num < 0:
pair = num / 2
if table[pair] < 1:
return False
table[num] -= 1
table[pair] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count=collections.Counter(A)
for x in sorted(A,key=abs):
if count[x]==0:
continue
if count[2*x]==0:
return False
count[x]-=1
count[2*x]-=1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
## Time Complexity: O(N LOG N) , Space: O(N)
count = collections.Counter(A)
for x in sorted(A, key = abs):
if count[x] == 0: continue
if count[2*x] == 0: return False
count[x] -= 1
count[2*x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = Counter(A)
for x in sorted(A, key = abs):
if count[x] == 0: continue
if count[2*x] == 0: return False
count[x] -= 1
count[2*x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
# Array of Doubled Pairs 9/24 8/31/20
# 1:43 9/1/2020
# c = collections.Counter(A)
# for x in sorted(c, key=abs):
# if c[x] > c[x * 2]:
# return False
# c[x*2] -= c[x]
# return True
A = [abs(num) for num in A ]
A.sort()
dic = collections.defaultdict(collections.deque)
for i, num in enumerate(A):
dic[num].append(i)
visited = [0]*len(A)
cnt = 0
i = 0
# while cnt < len(A) // 2 :
while i < len(A):
if visited[i]:
i += 1
continue
val, val_double = A[i], 2 * A[i]
if val_double in dic and dic[val_double]:
if val not in dic or not dic[val]:
return False
dic[val].popleft()
if not dic[val_double]:
return False
index = dic[val_double].popleft()
visited[index] = 1
else:
return False
i += 1
# cnt += 1
return True
# A = [abs(num) for num in A ]
# A.sort()
# dic = collections.defaultdict(int)
# for num in A:
# dic[num] += 1
# visited = [0]*len(A)
# cnt = 0
# i = 0
# while cnt < len(A) // 2 :
# if visited[i]:
# i += 1
# continue
# val_small = A[i]
# if 2*val_small in dic:
# dic[2*val_small] -= 1
# dic[val_small] -= 1
# if dic[2*val_small] < 0 or dic[val_small] < 0:
# return False
# search_index = A.index(2*val_small, i+1)
# while visited[search_index]:
# search_index = A.index(2*val_small, search_index+1)
# visited[search_index] = 1
# else:
# return False
# i += 1
# cnt += 1
# return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
counts = collections.Counter(A)
count = 0
A.sort()
for num in A: #range(0,len(A)//2):
#num = A[i]
if counts[num] and counts[num * 2]:
counts[num*2]-=1
counts[num]-=1
count+=1
return count*2 == len(A)
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
counter = collections.Counter(A)
for e in sorted(A, key=abs):
if 2*e in counter and counter[2*e] > 0 and counter[e] > 0:
counter[e] -= 1
counter[2*e] -= 1
print(counter)
return sum(counter.values()) == 0
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
seen = Counter()
for num in A:
if num>0 and num%2:
seen[num]+=1
continue
elem = 2*num if num<0 else num//2
if seen[elem]>0:
seen[elem]-=1
else:
seen[num]+=1
if sum(seen.values()):
return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import Counter
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
# if len(A)%2==1:
# return False
# counter = Counter(A)
# # print(counter)
# Skey = set(counter.keys())
# key = counter.keys()
# for i in key:
# if 2*i in Skey:
# counter[i]=0
# counter[i*2]-=counter[i]
# val = list(counter.values())
# print(val)
# for i in val:
# if i !=0:
# return False
# return True
cnt = collections.Counter(A)
for a in sorted(A, key = abs):
if cnt[a] and cnt[a * 2]:
cnt[a] -= 1
cnt[a * 2] -= 1
return all(cnt[a] == 0 for a in A)
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
cache=collections.Counter(A)
c_list=sorted(cache, key=lambda x: abs(x))
print(cache, c_list)
for x in c_list:
if cache[x]>cache[2*x]:
return False
cache[2*x]-=cache[x]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
import numpy as np
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
statdict = {}
flag = True
# print(A)
for i in A:
# print(statdict)
if i in statdict:
statdict[i] = statdict[i] + 1
elif i < 0:
if i*2 in statdict:
statdict[i*2] = statdict[i*2] - 1
if statdict[i*2] == 0:
statdict.pop(i*2)
else:
statdict[i] = 1
else:
if (i%2 == 0) and i/2 in statdict:
statdict[int(i/2)] = statdict[int(i/2)] - 1
if statdict[int(i/2)] == 0:
statdict.pop(int(i/2))
else:
statdict[i] = 1
return (len(statdict) == 0) or (len(statdict) == 1 and 0 in statdict and statdict[0] % 2 == 0)
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
c = collections.Counter(A)
for i in sorted(A, key=abs):
if c[i] and c[i*2]:
c[i] -= 1
c[i*2] -= 1
return all(c[i] == 0 for i in A)
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
B = []
for x in A:
if x>0:
B.append(x)
else:
B.append(-x)
count = collections.Counter(B)
for x in sorted(B):
if count[x] == 0: continue
if count[2*x] == 0: return False
else:
count[x] -=1
count[2*x] -=1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
D1 = collections.Counter()
D2 = collections.Counter()
zero = 0
for a in A:
if a < 0:
D1[a] += 1
else:
D2[a] += 1
for d in sorted(D1, reverse=1):
if D1[d]:
if D1[ d*2 ] < D1[d]:
return False
D1[ d*2 ] -= D1[d]
for d in sorted(D2):
if D2[d]:
if D2[ d*2 ] < D2[d]:
return False
D2[ d*2 ] -= D2[d]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
n = len(A)
#A[2*i+1] = 2*A[2*i]
#[-2,-4,2,4]
#A[1] == 2*A[0]
#A[3] == 2*A[2]
neg = sorted(list([x for x in A if x<0]))
pos = sorted(list([x for x in A if x>0]), reverse=True)
nl, pl = len(neg), len(pos)
if nl % 2 or pl % 2: return False
d1 = collections.Counter(neg)
cnt = 0
for i in range(nl):
if d1[2*neg[i]] > 0:
d1[2*neg[i]] -= 1
d1[neg[i]] -= 1
cnt += 1
if cnt < nl // 2: return False
d2 = collections.Counter(pos)
cnt = 0
for i in range(pl):
if d2[2*pos[i]] > 0:
print((pos[i]*2, pos[i]))
d2[2*pos[i]] -= 1
d2[pos[i]] -= 1
cnt += 1
if cnt < pl // 2: return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution(object):
def canReorderDoubled(self, A):
count = collections.Counter(A)
for x in sorted(A, key = abs):
if count[x] == 0: continue
if count[2*x] == 0: return False
count[x] -= 1
count[2*x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import Counter
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
d=Counter()
c=[]
e=[]
for i in A:
if(i<0):
c.append(i)
elif(i>=0):
e.append(i)
c.sort(reverse=True)
c.extend(e)
A=c
print(A)
for i in A:
d[i]+=1
if(d[0]%2!=0):
return False
d[0]=0
for i in A:
if(2*i in d)and(d[i]<=d[2*i]):
d[2*i]-=d[i]
d[i]=0
elif(d[i]==0):
continue
else:
return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import Counter
from typing import List
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
d = dict()
num_zeros = 0
for x in A:
if x == 0:
num_zeros += 1
continue
x = abs(x)
y = x
while y % 2 == 0:
y //= 2
if y in d:
d[y].update([x])
else:
d[y] = Counter([x])
if num_zeros % 2:
return False
for base, counter in d.items():
keys = sorted(counter)
for key in keys:
if counter[key] <= counter[2 * key]:
counter[2 * key] -= counter[key]
counter[key] -= counter[key]
else:
return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
c=collections.Counter(A)
for x in sorted(A,key=abs):
if c[x]==0:
continue
if c[2*x]==0:
return False
c[x]-=1
c[2*x]-=1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
# 20201007 12:08 选参考答案
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
cache=Counter(A)
c_list=sorted(list(cache),key=abs)
for x in c_list:
if cache[x]>cache[2*x]:
return False
cache[2*x]-=cache[x]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
counter=collections.Counter(A)
counter=dict(sorted(counter.items(),key=lambda x:abs(x[0])))
nums=[]
for num in counter.keys():
for _ in range(counter[num]):
nums.append(num)
for num in nums:
if counter[num]:
if 2*num in counter and counter[2*num]!=0:
counter[num]-=1
counter[num*2]-=1
else:
return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = collections.Counter(A)
for x in sorted(A, key = abs):
if count[x] == 0: continue
if count[2*x] == 0: return False
count[x] -= 1
count[2*x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
if len(A) == 0 :
return True
mydict = {}
for _ in range(0,len(A)):
if A[_] in mydict:
mydict[A[_]] += 1
else:
mydict[A[_]] = 1
A_sorted = sorted(A)
#print(A_sorted)
#print(mydict)
not_valid = set()
for num in A_sorted:
if len(mydict) == 0:
return True
if num in not_valid:
continue
if (2 * num) in mydict and num in mydict:
double = 2*num
mydict[num] -= 1
mydict[double] -= 1
if mydict[num] == 0:
mydict.pop(num)
not_valid.add(num)
#deals with duplicates
if num == double:
continue
elif mydict[2*num] == 0:
mydict.pop(2*num)
not_valid.add(2*num)
elif (num/2 in mydict and num in mydict):
half = num/2
mydict[num] -= 1
mydict[num/2] -= 1
if mydict[num] == 0:
mydict.pop(num)
not_valid.add(num)
if mydict[num/2] == 0:
mydict.pop(num/2)
not_valid.add(num/2)
else:
return False
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
# Divide an conquer
# Sort items according to their magnitude
# For each item, make sure it's double exists later in the list
# When a double is used, mark it with a mask
used = [False]*len(A)
A.sort(key = lambda n: abs(n))
bank = collections.Counter(A)
for num in A:
if bank[num] == 0:
continue
bank[num] -= 1
if bank.get(2*num, 0) == 0:
return False
bank[2*num] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import Counter
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
# Greedy
# Time complexity: O(NlogN)
# Space complexity: O(N)
count = Counter(A)
for x in sorted(A, key=abs):
if count[x] == 0: continue
if count[2 * x] == 0: return False
count[x] -= 1
count[2 * x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A_sorted = sorted(A, key=abs)
count = collections.Counter(A)
for a in A_sorted:
if count[a] == 0:
continue
if count[a * 2] == 0:
return False
count[a] -= 1
count[a * 2] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution(object):
def canReorderDoubled(self, A):
count = collections.Counter(A)
for x in sorted(A, key = abs):
if count[x] == 0: continue
if count[2*x] == 0: return False
count[x] -= 1
count[2*x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution(object):
def canReorderDoubled(self, A):
count = collections.Counter(A)
for x in sorted(A, key = abs):
if count[x] == 0: continue
if count[2*x] == 0: return False
count[x] -= 1
count[2*x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
c = collections.Counter(A)
for k in sorted(c, key=abs):
if c[k] > c.get(2*k, 0):
return False
c[2*k] -= c[k]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
st = {}
A = sorted(A,reverse=True)
for i in A:
if i in st:st[i]+=1
else: st[i] = 1
#print(st)
for num in A:
if (num*2) in st and st[num] > 0 and st[(num*2)] > 0:
st[num*2]-=1
st[num]-=1
#print(num,st)
for num in st:
if st[num] > 0:return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import Counter
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count_a = Counter(A)
A.sort(key=lambda x: abs(x))
for elem in A:
if count_a[elem] == 0: continue
if count_a[elem * 2] == 0: return False
count_a[elem] -= 1
count_a[elem * 2] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = collections.Counter(A)
for x in sorted(A, key = abs):
if count[x] == 0:
continue
if count[x*2] == 0:
return False
count[x] -= 1
count[2*x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
map = {}
for item in A:
if item in map : map[item] +=1
else : map[item] =1
for item in A:
if map[item]==0 : continue
if item <0 :
if item/2 in map :
if map[item/2]==0 : return False
map[item] -=1
map[item/2] -=1
else : return False
else :
if item *2 in map:
if map[2*item]==0: return False
map[item] -=1
map[item*2] -=1
else : return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
seen = dict()
for x in A:
if x not in seen:
seen[x] = 0
seen[x] += 1
for x in sorted(seen, key=lambda x : x**2):
if seen[x] == 0:
continue
if (2*x not in seen) or (seen[x] > seen[2*x]):
return False
else:
seen[2*x] -= seen[x]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = collections.Counter(A)
for x in sorted(A, key = abs):
print(x)
if count[x] == 0: continue
if count[2*x] == 0: return False
count[x] -= 1
count[2*x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
from collections import Counter
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
a = sorted(A,key=lambda x:abs(x),reverse = True)
b = dict(Counter(A))
while a:
aa = a.pop()
if aa in b:
b[aa] -= 1
if b[aa]==0:
del b[aa]
if 2*aa in b:
b[2*aa] -= 1
else:
return False
if b[2*aa]==0:
del b[2*aa]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = collections.Counter(A)
for x in sorted(A, key = abs):
if count[x] == 0: continue
if count[2*x] == 0: return False
count[x] -= 1
count[2*x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
counter = collections.Counter(A)
for x in sorted(counter,key=abs):
if counter[x] > counter[2*x]:
return False
counter[2*x] -= counter[x]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
if len(A) % 2 == 1:
return False
count = collections.defaultdict(int)
for num in A:
count[num] += 1
A.sort()
for num in A:
if count[num] > 0:
double = num * 2
if double in count and count[double] > 0:
# print(\"found\", double)
count[num] -= 1
count[double] -= 1
for num in count:
if count[num] != 0:
return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
c = collections.Counter(A)
for x in sorted(c, key=abs):
if c[x] > c[2 * x]:
return False
c[2 * x] -= c[x]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
d={}
for i in A:
if i in d:
d[i]+=1
else:
d[i]=1
A.sort()
if 0 in d:
if d[0]%2!=0:
return False
d.pop(0)
for i in A:
#print(i,d)
if i not in d:
continue
if i*2 in d:
if d[i]>d[i*2]:
d[i]-=d[i*2]
d[i*2]=0
else:
d[i*2]-=d[i]
d[i]=0
if d[i]==0:
d.pop(i)
if d[i*2]==0:
d.pop(i*2)
elif i%2==0 and i//2 in d:
if d[i]>d[i//2]:
d[i]-=d[i//2]
d[i//2]=0
else:
d[i//2]-=d[i]
d[i]=0
if d[i]==0:
d.pop(i)
if d[i//2]==0:
d.pop(i//2)
else:
return False
return len(d)==0
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
import collections
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
''' Place all values into dicts and start counting from the bottom
'''
counter = collections.Counter(A)
for v in sorted(counter, key = abs):
if counter[2 * v] < counter[v]:
return False
counter[2 * v] -= counter[v]
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A = sorted(A, key=lambda x: abs(x))
M = Counter()
for e in A:
M[e] += 1
# print(M)
# print(A)
for e in A:
# print(e, M[2*e], M)
if M[e] and M[2*e]:
M[2*e] -= 1
M[e] -= 1
# print('->', 2*e, M[2*e])
elif M[e]:
# print(M, e)
return False
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = collections.Counter(A)
for x in sorted(A, key = abs):
if count[x]==0: continue
if count[2*x]==0: return False
count[x] -= 1
count[2*x] -= 1
return True
|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105
|
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
dic=collections.defaultdict(int)
for i in range(len(A)):
dic[A[i]]+=1
ki=list(dic.keys())
ki.sort(key=lambda x:abs(x))
for k in ki:
if dic[2*k]<dic[k]:
return False
dic[2*k]-=dic[k]
dic[k]=0
return True
|
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Note:
The length of num is less than 10002 and will be ≥ k.
The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.
|
class Solution:
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
out=[]
for digit in num:
while k and out and out[-1] > digit:
out.pop()
k-=1
out.append(digit)
return ''.join(out[:-k or None]).lstrip('0') or "0"
|
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Note:
The length of num is less than 10002 and will be ≥ k.
The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.
|
class Solution:
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
if(num == ""): return ""
arr = [i for i in list(num)]
stack = [arr[0]]
out = 0
for i in range(1, len(arr)):
while(out < k and len(stack)>0 and arr[i] < stack[-1]):
stack.pop()
out += 1
stack.append(arr[i])
diff = k - out
for i in range(diff): stack.pop()
res = "".join(stack).lstrip("0")
if(res == ""): return "0"
return res
|
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Note:
The length of num is less than 10002 and will be ≥ k.
The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.
|
class Solution(object):
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
if k == len(num):
return "0"
stack = []
cnt = 0
for i, ch in enumerate(num):
if cnt == k:
for j in range(i, len(num)):
stack.append(num[j])
break
if not stack or ch >= stack[-1]:
stack.append(ch)
else:
while stack and ch < stack[-1] and cnt < k:
stack.pop()
cnt += 1
stack.append(ch)
while cnt < k:
stack.pop()
cnt += 1
i = 0
while i < len(stack) and stack[i] == '0':
i += 1
if i == len(stack):
return "0"
return ''.join(stack[i:])
|
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Note:
The length of num is less than 10002 and will be ≥ k.
The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.
|
class Solution:
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
while True:
if k == 0:
return num if num != "" else "0"
if num == "":
return "0"
index = 0
num_len = len(num)
while True:
if index == num_len - 1:
num = num[:-1]
k -= 1
break
if num[index] > num[index + 1]:
new_num = num[:index] + num[index+1:]
num = new_num.lstrip("0")
k -= 1
break
index += 1
|
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Note:
The length of num is less than 10002 and will be ≥ k.
The given num does not contain any leading zero.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.
|
class Solution:
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
stack = []
i = 0
num += '0'
while i < len(num):
while stack and stack[-1] > num[i] and k > 0:
stack.pop()
k -= 1
if (stack or num[i] != '0') and i < len(num)-1:
stack.append(num[i])
i += 1
res = ''.join(stack)
return res if res else '0'
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums = sorted(set(nums), key=lambda x: x)
result = 0
for i in range(len(nums)):
if nums[i] <= 0:
continue
elif nums[i] == result + 1:
result += 1
else:
break
return result + 1
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 1
for index in range(len(nums)):
while nums[index] != index + 1:
tmp = nums[index]
if tmp <= 0 or tmp > len(nums) or tmp == nums[tmp - 1]:
break
nums[index] = nums[tmp - 1]
nums[tmp - 1] = tmp
for index in range(len(nums)):
if nums[index] != index + 1:
return index + 1
return len(nums) + 1
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
minSet = set()
maxSet = set()
nums = set(nums)
for num in nums:
if num > 0:
if num + 1 in minSet:
minSet.remove(num + 1)
minSet.add(num)
if num - 1 in maxSet:
maxSet.remove(num - 1)
minSet.remove(num)
elif num - 1 in maxSet:
maxSet.remove(num - 1)
maxSet.add(num)
if num + 1 in minSet:
maxSet.remove(num)
minSet.remove(num + 1)
else:
minSet.add(num)
maxSet.add(num)
# print("min set:", minSet)
# print("max set:", maxSet)
if len(minSet) == 0 or len(maxSet) == 0:
return 1
return min(maxSet) + 1 if min(minSet) == 1 else 1
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
i = 0
n = len(nums)
if not nums:
return(1)
while (i <n):
while ((nums[i] != i) and (nums[i] > 0) and (i<n) and (nums[i] < n) and (nums[i] != nums[nums[i]])):
temp = nums[nums[i]]
nums[nums[i]] = nums[i]
nums[i] = temp
i = i + 1
i = 1
while(i<n):
if(nums[i] != i):
return(i)
i=i+1
if(nums[0] == n):
return(n+1)
else:
return(n)
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
for i in range(len(nums)):
while 0 <= nums[i]-1 < len(nums) and nums[nums[i]-1] != nums[i]:
tmp = nums[i]-1
nums[i], nums[tmp] = nums[tmp], nums[i]
for i in range(len(nums)):
if nums[i] != i+1:
return i+1
return len(nums)+1
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if nums == []:
return(1)
for _ in range(5):
for i in range(len(nums)):
if ((i + 1) != nums[i]) and (0 < nums[i] <= len(nums)):
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
#print(nums, 'up')
else:
pass
#print(nums)
for i in range(len(nums))[::-1]:
if ((i + 1) != nums[i]) and (0 < nums[i] <= len(nums)):
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
else:
pass
#print(nums)
for i in range(len(nums)):
if not i+1 == nums[i]:
#print(nums)
return(i+1)
if sorted(nums) == nums:
return(len(nums)+1)
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
A=nums
n = len(A)
for index in range(n):
element = A[index]
while True:
if element <= 0 or element > n or element == A[element - 1]:
break
A[element - 1], element = element, A[element - 1]
for index in range(n):
if A[index] != index + 1:
return index + 1
return n + 1
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
i = 0
while i < len(nums):
tmp = nums[i]
# exchange to its right idx
while tmp != i + 1 and tmp > 0 and tmp < len(nums):
nums[i] = nums[tmp-1]
nums[tmp-1] = tmp
if tmp == nums[i]:
break
tmp = nums[i]
i += 1
print(nums)
for i in range(len(nums)):
if nums[i] != i+1:
return i+1
return len(nums) + 1
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# put numbers to their places
# [-1,0,2,1] -> [1,2,0,0]
for i in range(len(nums)):
if nums[i] < 0:
nums[i] = 0
if nums[i] > 0 and nums[i] != i + 1:
j, nums[i] = nums[i], 0
while 0 < j <= len(nums) and nums[j-1] != j:
next_j = nums[j-1]
nums[j-1] = j
j = next_j
for i, num in enumerate(nums):
if not num:
return i+1
if nums and nums[-1] > 0:
return nums[-1] + 1
return 1
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
minSet = set()
maxSet = set()
nums = set(nums)
for num in nums:
if num > 0:
if num + 1 in minSet:
minSet.remove(num + 1)
minSet.add(num)
if num - 1 in maxSet:
maxSet.remove(num - 1)
minSet.remove(num)
elif num - 1 in maxSet:
maxSet.remove(num - 1)
maxSet.add(num)
if num + 1 in minSet:
maxSet.remove(num)
minSet.remove(num + 1)
else:
minSet.add(num)
maxSet.add(num)
print("min set:", minSet)
print("max set:", maxSet)
if len(minSet) == 0 or len(maxSet) == 0:
return 1
return min(maxSet) + 1 if min(minSet) == 1 else 1
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.append(0)
for i in range(len(nums)):
while 0 < nums[i] < len(nums) and nums[i] != i and nums[i] != nums[nums[i]]:
temp = nums[nums[i]]
nums[nums[i]] = nums[i]
nums[i] = temp
print((temp, nums))
for i in range(1, len(nums)):
if nums[i] != i:
return i
return len(nums)
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 1
n = len(nums)
for i in range(len(nums)):
while nums[i] > 0 and nums[i] <= n and nums[i] != nums[nums[i] - 1]:
tmp = nums[i]
nums[i] = nums[tmp - 1]
nums[tmp - 1] = tmp
for j in range(len(nums)):
if nums[j] != j + 1: return j + 1
return n + 1
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
lnums=len(nums)
for i in range(lnums):
while 0<nums[i]<=lnums and nums[i]!=nums[nums[i]-1]:
tmp=nums[i]
print((i,tmp,nums))
nums[tmp-1],nums[i]=nums[i],nums[tmp-1]
print((i,tmp,nums))
for i in range(lnums):
if nums[i]!=i+1:
return i+1
return lnums+1
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if nums == []:
return(1)
for _ in range(10):
for i in range(len(nums)):
if ((i + 1) != nums[i]) and (0 < nums[i] <= len(nums)):
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
#print(nums, 'up')
else:
pass
#print(nums)
for i in range(len(nums))[::-1]:
if ((i + 1) != nums[i]) and (0 < nums[i] <= len(nums)):
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
else:
pass
#print(nums)
for i in range(len(nums)):
if not i+1 == nums[i]:
#print(nums)
return(i+1)
if sorted(nums) == nums:
return(len(nums)+1)
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0:
return 1
nums.append(0)
for i in reversed(range(len(nums))):
n = nums[i]
while n>=0 and n<len(nums) and i != n:
nums[n], nums[i] = nums[i], nums[n]
if n == nums[i]:
break
n = nums[i]
print(nums)
for i,n in enumerate(nums):
if i==0:
continue
if i != n:
return i
return nums[-1]+1
|
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0]
Output: 3
Example 2:
Input: [3,4,-1,1]
Output: 2
Example 3:
Input: [7,8,9,11,12]
Output: 1
Note:
Your algorithm should run in O(n) time and uses constant extra space.
|
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
for i in range(n):
while nums[i] > 0 and nums[i] <= n and nums[i] != i+1 and nums[i] != nums[nums[i]-1]:
nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i]-1]
for i in range(n):
if nums[i] != i + 1:
return i + 1
return n + 1
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.