inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
from math import ceil
class Solution:
def minSetSize(self, arr: List[int]) -> int:
arr.sort()
brr = []
count = 1
cur = arr[0]
for i in range(1,len(arr)):
if cur == arr[i]:
count += 1
else:
brr.append([cur,count])
cur = arr[i]
count = 1
brr.append([cur,count])
brr.sort(key = lambda x:x[1],reverse = True)
temp = 0
temp1 = 0
for i in brr:
if temp >=ceil(len(arr)/2):
return temp1
temp1 += 1
temp += i[1]
return 1
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
numsdict = {}
for i in arr:
numsdict[i] = numsdict.get(i,0) + 1
sortarr = sorted(numsdict.items(), key = lambda x: -x[1])
count = 0
i = 0
while 2*count < len(arr):
count += sortarr[i][1]
i += 1
return i
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = collections.defaultdict(int)
for i in arr:
d[i] += 1
s = sorted([(d[i],i) for i in d],reverse=True)
res = 0
ans = 0
for i,v in s:
res += i
ans += 1
if res >= (len(arr) + 1) // 2: return ans
return len(d)
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
c = Counter(arr)
l = len(arr)
tup = c.most_common(len(c))
i = 0
while l > len(arr)/2:
l -= tup[i][1]
i += 1
return i
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
import numpy as np
class Solution:
def minSetSize(self, arr: List[int]) -> int:
n = len(arr)
dict_model = {}
for it in arr:
if it in dict_model.keys():
dict_model[it] += 1
else:
dict_model[it] = 1
min_deactivate = np.ceil(n/2)
generators = list(dict_model.values())
generators.sort()
turn_off = 0
i = 1
while turn_off < min_deactivate:
turn_off += generators[-i]
i += 1
return i-1
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
if not arr: return 0
int_freq = collections.Counter(arr)
size = len(arr)
cur_size = size
count = 0
for num, freq in int_freq.most_common():
cur_size -= freq
count += 1
if cur_size <= size // 2:
return count
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
# L, C = len(arr), collections.Counter(arr)
# S = sorted(C.values(), reverse = True)
# T = itertools.accumulate(S)
# for i,v in enumerate(T):
# if v >= len(arr)//2: return i + 1
h = [(val*(-1),key) for key,val in list(Counter(arr).items())]
heapify(h)
#print(h)
num_int = 0
count = 0
while 2*count*(-1) < len(arr):
c,i = heappop(h)
count += c
num_int +=1
#print(seen)
return num_int
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
l=len(arr)
s=0
cnt= Counter(arr)
cnt= [j for i,j in cnt.items()]
cnt.sort(reverse=True)
for ind,i in enumerate(cnt):
s+=i
if s>=l//2:
break
return ind+1
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
# L, C = len(arr), collections.Counter(arr)
# S = sorted(C.values(), reverse = True)
# T = itertools.accumulate(S)
# for i,v in enumerate(T):
# if v >= len(arr)//2: return i + 1
dic = Counter(arr)
h = [(val*(-1),key) for key,val in list(dic.items())]
heapify(h)
#print(h)
num_int = 0
count = 0
while 2*count*(-1) < len(arr):
c,i = heappop(h)
count += c
num_int +=1
#print(seen)
return num_int
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = {}
n,half = len(arr), len(arr) // 2
seen = set()
for i in arr:
d[i] = d.get(i,0) + 1
d = sorted(d.items(), key=lambda x: -x[1])
for k,v in d:
if n - v > half:
n -= v
seen.add(k)
else:
seen.add(k)
return len(seen)
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
h = {}
for el in arr:
if el not in h:
h[el] = 0
h[el] += 1
tmp = []
for key in list(h.keys()):
tmp.append(h[key])
tmp.sort()
count = 0
size = 0
for i in range(len(tmp) - 1, -1, -1):
size += tmp[i]
count += 1
if size >= math.ceil(len(arr) / 2):
break
return count
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
dic = Counter(arr)
h = [(val*(-1),key) for key,val in list(dic.items())]
heapify(h)
#print(h)
seen = set()
count = 0
while 2*count*(-1) < len(arr):
c,i = heappop(h)
count += c
seen.add(i)
#print(seen)
return len(seen)
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
from collections import defaultdict
class Solution:
def minSetSize(self, arr: List[int]) -> int:
# we will store the frequency of all the elements in a dictionary and from the dictionary we will pick the ones with the maximum value and remove them till we do not get half size
# we keep two dictionaries ones from number to their frequency and another from frequency to number for better manipulation
num_fre = defaultdict(int)
fre_num = defaultdict(set)
for i in range(len(arr)):
num_fre[arr[i]] += 1
fre_num[num_fre[arr[i]]].add(arr[i])
if fre_num[num_fre[arr[i]] - 1] != set():
fre_num[num_fre[arr[i]] - 1].remove(arr[i])
del num_fre
answer = set()
removed_elements = 0
for fre in sorted(fre_num.keys(),reverse = True):
for element in fre_num[fre]:
if removed_elements < len(arr) // 2:
removed_elements += fre
answer.add(element)
else:
break
return len(answer)
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
from collections import Counter
class Solution:
def minSetSize(self, arr: List[int]) -> int:
tot = n = len(arr)
count = Counter(arr)
res = 0
for k, v in sorted(count.items(), key=lambda x: -x[1]):
# print(k,v)
tot -= v
res += 1
if tot <= n // 2:
return res
return n
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
from collections import defaultdict
class Solution:
def minSetSize(self, arr: List[int]) -> int:
# we will store the frequency of all the elements in a dictionary and from the dictionary we will pick the ones with the maximum value and remove them till we do not get half size
# we keep two dictionaries ones from number to their frequency and another from frequency to number for better manipulation
num_fre = defaultdict(int)
fre_num = defaultdict(set)
for i in range(len(arr)):
num_fre[arr[i]] += 1
fre_num[num_fre[arr[i]]].add(arr[i])
if fre_num[num_fre[arr[i]] - 1] != set():
fre_num[num_fre[arr[i]] - 1].remove(arr[i])
answer = set()
removed_elements = 0
for fre in sorted(fre_num.keys(),reverse = True):
for element in fre_num[fre]:
if removed_elements < len(arr) // 2:
removed_elements += fre
answer.add(element)
else:
break
return len(answer)
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
# count = Counter(arr)
# count = sorted(count.items(), key=lambda item: item[1], reverse=1)
# total = len(arr)
# i = 0
# res = set()
# while total > len(arr) // 2:
# total -= count[i][1]
# res.add(count[i][0])
# i += 1
# return res
count = sorted(Counter(arr).values(), reverse=1)
target = (len(arr)) // 2
res = curr = 0
for val in count:
curr += val
res += 1
if curr >= target:
break
return res
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
N = len(arr)
count = collections.Counter()
for n in arr:
count[n] += 1
sorted_count = sorted(list(count.keys()), key=lambda c: count[c], reverse=True)
removed_count = 0
res = 0
for c in sorted_count:
removed_count += count[c]
res += 1
if removed_count >= (N + 1) // 2:
break
return res
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = {}
h = []
for num in arr:
if num not in d:
d[num] = 1
else:
d[num] += 1
for k, v in list(d.items()):
h.append((-v, k))
heapq.heapify(h)
ans = 0
count = 0
while count < len(arr)//2:
v, k = heapq.heappop(h)
count -= v
ans += 1
return ans
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
freqs = Counter(arr)
sorted_freqs = sorted(list(freqs.items()), key = lambda x : x[1], reverse=True)
print(sorted_freqs)
half_len = len(arr) / 2
current_len = 0
res = 0
for (i, j) in sorted_freqs:
res += 1
current_len += j
if current_len >= half_len:
return res
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, a: List[int]) -> int:
d={}
n=len(a)//2
for i in a:
if (i not in d):
d[i]=1
else:
d[i]+=1
d=sorted(d.items(),key=lambda x:x[1], reverse=True)
print(d)
sum=0
count=0
for i in d:
x=i[1]
sum+=x
count+=1
if(sum>=n):
break
return count
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
hashMap = dict()
for item in arr:
if item not in hashMap:
hashMap[item] = 0
hashMap[item] += 1
length = len(arr)
itemToBeRemoved = 0
halfOflength = len(arr) // 2
for value in sorted(list(hashMap.values()), reverse = True):
length -= value
itemToBeRemoved += 1
if length <= halfOflength:
return itemToBeRemoved
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
counter = {}
for num in arr:
if num in counter:
counter[num] += 1
else:
counter[num] = 1
heap = []
for num in list(counter.keys()):
heapq.heappush(heap, (-counter[num], num))
n = len(arr)
i = 0
cumSum = 0
while i < n and cumSum < n//2:
freq, num = heapq.heappop(heap)
cumSum += abs(freq)
i+=1
return i
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
checked, answer = [],0
count_d,total = [],0
if len(arr) == len(set(arr)):
return int(len(arr)/2)
for num in arr:
if num not in checked:
count_d.append(arr.count(num))
checked.append(num)
#count_d = sorted(count_d.items(), key=operator.itemgetter(1))
count_d.sort()
for d in range(1, len(count_d)+1):
total += count_d[-d]
answer += 1
if total >= len(arr)/2:
return answer
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = {}
for el in arr:
if d.get(el): d[el] += 1
else: d[el] = 1
s = sorted(d.values())
l = len(arr) / 2
c = 0
for i in reversed(range(len(s))):
l -= s[i]
c += 1
if l <= 0: break
return c
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
numsdict = {}
for i in arr: numsdict[i] = numsdict.get(i,0) + 1
sortarr = sorted(numsdict.values(), reverse = True)
count = 0
i = 0
while 2*count < len(arr):
count += sortarr[i]
i += 1
return i
|
Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Example 3:
Input: arr = [1,9]
Output: 1
Example 4:
Input: arr = [1000,1000,3,7]
Output: 1
Example 5:
Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5
Constraints:
1 <= arr.length <= 10^5
arr.length is even.
1 <= arr[i] <= 10^5
|
class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = {}
for i in arr:
if i in d:
d[i]+=1
else:
d[i] = 1
d = sorted(d.items(), key = lambda x:x[1], reverse=True)
print(d)
total, count = 0, 0
for i in d:
total += i[1]
count+=1
if total >= len(arr)//2:
return count
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
target,rem=divmod(sum(nums),k)
if rem or max(nums)>target: return False
n=len(nums)
seen=[0]*n
nums.sort(reverse=True)
def dfs(k,index,current_sum):
if k==1:
return True
if current_sum==target:
return dfs(k-1,0,0)
for i in range(index,n):
if not seen[i] and current_sum+nums[i]<=target:
seen[i]=1
if dfs(k,i+1,current_sum+nums[i]):
return True
seen[i]=0
return False
return dfs(k,0,0)
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
quotient = sum(nums)/k
if quotient % 1 != 0:
return False
for num in nums:
if num > quotient:
return False
if num == quotient:
nums.remove(quotient)
k -= 1
nums.sort(reverse=True)
self.nums = nums
self.quotient = quotient
print(self.nums)
answer = self.dfs([0]*len(nums), 0, k)
return answer
# print(answer)
def dfs(self, visit, accu, k):
if k == 1:
# print(visit)
return True
for i in range(len(self.nums)):
if not visit[i]:
#print(visit)
if self.nums[i] + accu < self.quotient:
visit[i] = 1
if self.dfs(visit, self.nums[i] + accu, k):
return True
visit[i] = 0
if self.nums[i] + accu == self.quotient:
visit[i] = 1
accu = self.nums[i] + accu
return self.dfs(visit, 0, k-1)
return False
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def partition(self, nums, target, current, pos, fill):
if fill == 0:
return True
for i in range(pos, len(nums)):
next = nums[:i] + nums[i+1:]
if current + nums[i] == target:
if self.partition(next, target, 0, 0, fill - 1):
return True
elif current + nums[i] < target:
if self.partition(next, target, current + nums[i], i, fill):
return True
elif current == 0:
return False
return False
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
s = sum(nums)
if s % k > 0:
return False
target = s // k
return self.partition(nums, target, 0, 0, k)
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
if sum(nums) % k or len(nums) < k: return False
if k == 1: return True
target, used = sum(nums) / k, [False for i in range(len(nums))]
def dfs(start, sum_, k):
if k == 1:
return True
if sum_ == target:
return dfs(0, 0, k - 1)
for i in range(start, len(nums)):
if not used[i] and sum_ < target:
used[i] = True
if dfs(i + 1, sum_ + nums[i], k):
return True
used[i] = False
return False
return dfs(0, 0, k)
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
if sum(nums) % k or len(nums) < k: return False
if k == 1: return True
target, used = sum(nums) / k, [False for i in range(len(nums))]
def dfs(start, sum_, k):
if k == 1: return True
if sum_ == target: return dfs(0, 0, k - 1)
for i in range(start, len(nums)):
if not used[i] and sum_ < target:
used[i] = True
if dfs(i + 1, sum_ + nums[i], k):
return True
used[i] = False
return False
return dfs(0, 0, k)
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
target, rem = divmod(sum(nums), k)
if rem: return False
def dfs(groups):
if not nums: return True
v = nums.pop()
for i, group in enumerate(groups):
if group + v <= target:
groups[i] += v
if dfs(groups): return True
groups[i] -= v
if not group: break
nums.append(v)
return False
nums.sort()
if nums[-1] > target: return False
while nums and nums[-1] == target:
k -= 1
nums.pop()
return dfs([0] * k)
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
total = sum(nums)
if total%k != 0: return False
target = total//k
nums.sort()
if nums[-1] > target:
return False
while nums and nums[-1] == target:
nums.pop()
k -= 1
return self.helper(nums, target, [0]*k)
def helper(self, nums, target, dp):
if not nums: return True
num = nums.pop()
for i in range(len(dp)):
if dp[i] + num <= target:
dp[i] += num
if self.helper(nums, target, dp):
return True
dp[i] -= num
if dp[i] == 0:
break
nums.append(num)
return False
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
if not nums:
return True
if sum(nums) % k != 0:
return False
target = sum(nums) / k
nums.sort()
if nums[-1] > target:
return False
while nums and nums[-1] == target:
nums.pop()
k -= 1
def partition(nums, subsets, target):
if not nums:
return True
selected = nums.pop()
for i in range(len(subsets)):
if subsets[i] + selected <= target:
subsets[i] += selected
if partition(nums, subsets, target):
return True
subsets[i] -= selected
if subsets[i] == 0:
# this line is important, otherwise TLE.
# if subsets[i] is 0 then later subsets are all zeros. No need to try them all.
break
nums.append(selected)
return False
return partition(nums, subsets=[0]*k, target=target)
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
total = sum(nums)
if total%k!=0:
return False
target = total/k
used = [False]*len(nums)
nums = sorted(nums)
# print(nums)
def check(nums, k, cur, pos):
# print(used, k, cur, pos)
if k==1:
return True
for i in range(pos, -1, -1):
if not used[i]:
if nums[i]+cur<target:
used[i] = True
if check(nums, k, cur+nums[i], i-1):
return True
used[i] = False
elif nums[i]+cur==target:
used[i] = True
if check(nums, k-1, 0, len(nums)-1):
return True
used[i] = False
return False
return check(nums, k, 0, len(nums)-1)
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
target, rem = divmod(sum(nums), k)
if rem: return False
def search(groups):
if not nums: return True
v = nums.pop()
for i, group in enumerate(groups):
if group + v <= target:
groups[i] += v
if search(groups): return True
groups[i] -= v
if not group: break
nums.append(v)
return False
nums.sort()
if nums[-1] > target: return False
while nums and nums[-1] == target:
nums.pop()
k -= 1
return search([0] * k)
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution(object):
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
he = sum(nums)
if he % k != 0:
return False
target = he // k
visit = [0 for _ in range(len(nums))]
def dfs(k,ind,cur,cnt):
if k == 0:return True
if cur == target and cnt > 0:
return dfs(k-1,0,0,0)
for i in range(ind,len(nums)):
if not visit[i] and cur+nums[i] <= target:
visit[i] = 1
if dfs(k,i+1,cur+nums[i],cnt+1):
return True
visit[i] = 0
return False
return dfs(k,0,0,0)
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
total = sum(nums)
if total % k != 0:
return False
subsetTotal = total // k
visited = [0] * len(nums)
return self.helper(k, 0, 0, visited, nums, k, subsetTotal)
def helper(self, remainingSets, index, s, visited, nums, k, subsetTotal):
if remainingSets == 1:
return True
if s == subsetTotal:
return self.helper(remainingSets - 1, 0, 0, visited, nums, k, subsetTotal)
for i in range(index, len(nums)):
if visited[i] == 0 and s + nums[i] <= subsetTotal:
visited[i] = 1
if self.helper(remainingSets, i + 1, s + nums[i], visited, nums, k, subsetTotal):
return True
visited[i] = 0
return False
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
tot = sum(nums)
if k <= 0 or tot % k != 0:
return False
visited = [0] * len(nums)
def canPart(nums, visited, idx, curSum, k, target):
if k == 1:
return True
if curSum == target: return canPart(nums, visited, 0, 0, k - 1, target)
if curSum > target:
return False
for i in range(idx, len(nums)):
if not visited[i]:
visited[i] = 1
if canPart(nums, visited, i + 1, curSum + nums[i], k, target):
return True
visited[i] = 0
return False
return canPart(nums, visited, 0, 0, k, tot / k)
|
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Note:
1 .
0 < nums[i] < 10000.
|
class Solution:
def canPartitionKSubsets(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
if k <= 0:
return False
nums_sum = sum(nums)
if nums_sum % k != 0:
return False
visited = [False] * len(nums)
nums.sort(reverse=True)
return self.canPartition(nums, visited, k, 0, 0, nums_sum/k)
def canPartition(self, nums, visited, k, currentIndex, currentSum, target):
if k == 1:
return True
if currentSum == target:
return self.canPartition(nums, visited, k-1, 0, 0, target)
for i in range(currentIndex, len(nums)):
if visited[i] is False and currentSum + nums[i] <= target:
visited[i] = True
if(self.canPartition(nums, visited, k, i+1, currentSum + nums[i], target)):
return True
visited[i] = False
return False
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
counters = [0] * (1 << 16)
counters[0] = len(A)
for num in A:
mask = (~num) & ((1 << 16) - 1)
sm = mask
while sm != 0:
counters[sm] += 1
sm = (sm - 1) & mask
return sum(counters[num1 & num2] for num1 in A for num2 in A)
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
d = defaultdict(int)
for a in A:
for b in A:
d[a & b] += 1
return sum(d[ab] for c in A for ab in d if not(ab & c))
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import Counter
class Solution(object):
def countTriplets(self, A):
c = Counter(x & y for x in A for y in A)
return sum(c[xy] for xy in c for z in A if xy & z == 0)
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
and_values = {}
for x in A:
for y in A:
c = x & y
if c in and_values:
and_values[c] += 1
else:
and_values[c] = 1
triplet_count = 0
for a in and_values:
for z in A:
if a & z == 0:
triplet_count += and_values[a]
return triplet_count
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import Counter
from functools import lru_cache
from itertools import product
class Solution:
def countTriplets(self, A: List[int]) -> int:
@lru_cache(None)
def count_and_zero(x):
return sum(1 for a in A if a & x == 0)
pair_and_count = Counter(
x & y
for x, y in product(A, A)
)
return sum(
count_and_zero(x) * cnt
for x, cnt in list(pair_and_count.items())
)
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
memo = {}
for i in A:
for j in A:
memo[i&j] = memo.get(i&j, 0)+1
res = 0
for num in A:
for k in memo:
if num&k==0:
res += memo[k]
return res
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
t=0
d={}
for i in A:
for j in A:
a=i&j
if a in d:
d[a]+=1
else:
d[a]=1
for k in d:
for i in A:
if k&i==0:
t+=d[k]
return t
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
count =collections.defaultdict(int)
ret = 0
for i in A:
for j in A:
count[i & j] += 1
for i in A:
for j in count:
if i & j == 0: ret += count[j]
return ret
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
n=len(A)
count=0
dic=collections.defaultdict(int)
for i in range(n):
for j in range(n):
dic[(A[i]&A[j])]+=1
ans=0
# print(dic)
for ele in A:
for res in dic:
if res&ele==0:
ans+=dic[res]
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
c = Counter(x&y for x in A for y in A)
return sum([c[xy] for xy in c for z in A if xy&z == 0])
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import defaultdict
class Solution:
def countTriplets(self, A: List[int]) -> int:
d = defaultdict(int)
mask = (1<<16) - 1
for i in A:
high = mask ^ i
j = high
while j:
d[j] += 1
j = (j-1) & high
d[0] += 1
res = 0
for i in A:
for j in A:
res += d[i&j]
return res
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
#
# @lc app=leetcode id=982 lang=python3
#
# [982] Triples with Bitwise AND Equal To Zero
#
# https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/description/
#
# algorithms
# Hard (54.38%)
# Likes: 67
# Dislikes: 91
# Total Accepted: 5.5K
# Total Submissions: 10.2K
# Testcase Example: '[2,1,3]'
#
# Given an array of integers A, find the number of triples of indices (i, j, k)
# such that:
#
#
# 0 <= i < A.length
# 0 <= j < A.length
# 0 <= k < A.length
# A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
#
#
#
#
# Example 1:
#
#
# Input: [2,1,3]
# Output: 12
# Explanation: We could choose the following i, j, k triples:
# (i=0, j=0, k=1) : 2 & 2 & 1
# (i=0, j=1, k=0) : 2 & 1 & 2
# (i=0, j=1, k=1) : 2 & 1 & 1
# (i=0, j=1, k=2) : 2 & 1 & 3
# (i=0, j=2, k=1) : 2 & 3 & 1
# (i=1, j=0, k=0) : 1 & 2 & 2
# (i=1, j=0, k=1) : 1 & 2 & 1
# (i=1, j=0, k=2) : 1 & 2 & 3
# (i=1, j=1, k=0) : 1 & 1 & 2
# (i=1, j=2, k=0) : 1 & 3 & 2
# (i=2, j=0, k=1) : 3 & 2 & 1
# (i=2, j=1, k=0) : 3 & 1 & 2
#
#
#
#
# Note:
#
#
# 1 <= A.length <= 1000
# 0 <= A[i] < 2^16
#
#
#
from collections import Counter
class Solution:
def countTriplets(self, A: List[int]) -> int:
c = Counter(x & y for x in A for y in A)
return sum(c[xy] for xy in c for z in A if xy & z == 0)
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
l = len(A)
Memo = {}
for i in range(l):
for j in range(i+1):
t = A[i]&A[j]
if t not in Memo:
Memo[t] = 0
if i == j:
Memo[t] += 1
else:
Memo[t] += 2
r = 0
for a in A:
for key in Memo:
if key&a == 0:
r += Memo[key]
return r
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
d={}
for i in A:
for j in A:
if(i&j in d):
d[i&j]+=1
else:
d[i&j]=1
c=0
for i in A:
for j in d:
if(i&j==0):
c+=d[j]
return c
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
tmp = {}
for a in A:
for b in A:
if a&b in tmp:
tmp[a&b]+=1
else:
tmp[a&b]=1
ans = 0
for k, t in tmp.items():
for c in A:
if c&k==0:
ans +=t
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
mp = {}
for i in A:
for j in A:
k = i & j
mp[k] = mp.get(k, 0) + 1
result = 0
for i in A:
for j in mp:
if i & j == 0:
result += mp[j]
return result
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
counter = Counter()
for a in A:
for b in A:
counter[a&b] += 1
res = 0
for a in A:
for b in counter:
if a&b==0:
res += counter[b]
return res
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
dic = defaultdict(int)
res = 0
for i in A:
for j in A:
tmp = i&j
dic[tmp] += 1
for i in A:
for j in dic:
if i&j == 0:
res += dic[j]
return res
'''
class Solution:
def countTriplets(self, A: List[int]) -> int:
dic = defaultdict(int)
res = 0
for i in range(len(A)):
for j in range(len(A)):
num = A[i] & A[j]
dic[num] += 1
for i in range(len(A)):
for j in dic:
if A[i] & j == 0:
res += dic[j]
return res
'''
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
cnt = collections.defaultdict(int)
for i in A:
for j in A:
cnt[i&j]+=1
res = 0
for i in A:
for j in cnt:
if i & j == 0:
res += cnt[j]
return res
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import defaultdict
class Solution:
def countTriplets(self, A: List[int]) -> int:
counts = defaultdict(int)
for x in A:
for y in A:
counts[x & y] += 1
def dfs(z, xy, k):
if k == -1:
return counts[xy]
answer = dfs(z, xy << 1, k - 1)
if (z >> k & 1) == 0:
answer += dfs(z, (xy << 1 | 1), k - 1)
return answer
answer = 0
for z in A:
answer += dfs(z, 0, 16)
return answer
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import defaultdict
class Solution:
def countTriplets(self, A: List[int]) -> int:
counts = defaultdict(int)
for x in A:
for y in A:
counts[x & y] += 1
def dfs(xy, z, k):
if k == -1:
return counts[xy]
answer = dfs(xy << 1, z, k - 1)
if (z >> k & 1) == 0:
answer += dfs(xy << 1 | 1, z, k - 1)
return answer
answer = 0
for z in A:
answer += dfs(0, z, 16)
return answer
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
B = [bin(a)[2:] for a in A]
M, N = len(B), max(list(map(len, B)))
B = [b.zfill(N) for b in B]
dic = collections.defaultdict(set)
for i in range(M):
for j in range(N):
if B[i][j] == '1':
dic[j].add(i)
Venn = collections.defaultdict(list)
cnt = 0
for j in range(N):
if len(dic[j]):
cnt += (len(dic[j])) ** 3
for i in range(j, 0, -1):
for prv in Venn[i]:
intersec = prv & dic[j]
if len(intersec):
cnt += ((-1) ** i) * (len(intersec)) ** 3
Venn[i + 1].append(intersec)
Venn[1].append(dic[j])
return M ** 3 - cnt
# # ans = 0
# n = len(A)
# @lru_cache(None)
# def dfs(i, pre):
# if i == 4 and not pre:
# return 1
# ans = 0
# for a in A:
# if (i > 1 and not pre & a) or (i == 1 and not a):
# ans += n ** (3 - i)
# elif i < 3:
# ans += dfs(i + 1, pre & a if i > 1 else a)
# return ans
# return dfs(1, None)
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, nums):
count_map = collections.Counter(i & j for i in nums for j in nums)
return sum(count_map[key] for num in nums for key in count_map if num & key == 0)
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
# O(2**16*n), since a&b<= a,b<= 2**16
dp = collections.defaultdict(int)
for n1 in A:
for n2 in A:
dp[n1&n2]+=1
res = 0
for n in A:
for k,v in dp.items():
if not k&n:
res+=v
return res
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import defaultdict
class Solution:
def countTriplets(self, A: List[int]) -> int:
pairs = defaultdict(int)
for a in A:
for b in A:
pairs[a & b] += 1
res = 0
for a in A:
for p, cnt in pairs.items():
if not a & p:
res += cnt
return res
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
# O(N^2)
two_and_count = collections.Counter()
res = 0
for idx, x in enumerate(A):
if x == 0: # x & x & x
res += 1
# y & x & x -> 3
new_two_and = collections.Counter([x])
for idy in range(idx):
if x & A[idy] == 0:
res += 3
new_two_and[A[idy] & x] += 2
for v, c in two_and_count.items():
if x & v == 0:
res += 3 * c
two_and_count += new_two_and
return res
def countTriplets_II(self, A: List[int]) -> int:
M = 3
N = 1 << 16
dp = [[0] * (N) for _ in range(M + 1)]
dp[0][N - 1] = 1
for m in range(1, M + 1):
for v in range(N):
for a in A:
dp[m][v & a] += dp[m - 1][v]
return dp[M][0]
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
counter = collections.Counter()
for i in A:
for j in A:
counter[i&j] += 1
result = 0
for k in A:
for v in counter:
if k & v == 0:
result += counter[v]
return result
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
import numpy as np
class Solution:
def countTriplets(self, A: List[int]) -> int:
memo=dict()
for i in range(len(A)):
for j in range(i,len(A)):
r=A[i]&A[j]
memo[r]=memo.get(r,0)+(1 if i==j else 2)
ret=0
for i in range(len(A)):
for k in memo:
if A[i]&k==0:
ret+=memo[k]
return ret
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
d={}
res=0
for a in A:
for b in A:
t=a&b
if t in d:
d[t]+=1
else:
d[t]=1
for a in A:
for k,v in list(d.items()):
if a&k==0:
res+=v
return res
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
cnt = collections.Counter()
A = list(collections.Counter(A).items())
result = 0
for i, k1 in A:
for j, k2 in A:
cnt[i & j] += k1 * k2
cnt = list(cnt.items())
for i, k1 in A:
if i == 0:
result += k1 * n * n
continue
for j, k2 in cnt:
if i & j == 0:
result += k1 * k2
return result
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
# N = 1 << 16
# M = 3
# dp = [[0]*N for _ in range(M+1)]
# dp[0][N - 1] = 1
# for i in range(M):
# for k in range(N):
# for a in A:
# dp[i+1][k&a] += dp[i][k]
# return dp[M][0]
N = len(A)
ans = 0
count = collections.Counter()
for i in range(N):
for j in range(N):
count[A[i]&A[j]] += 1
for k in range(N):
for v in count:
if A[k] & v == 0:
ans += count[v]
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
umap = collections.Counter(A)
n = len(A)
mask = (1 << 16) - 1
for i in range(n):
for j in range(i+1, n):
key = A[i] & A[j]
if key not in umap:
umap[key] = 0
umap[key] += 2
result = 0
for a in A:
d = (~a) & mask
key = d
result += umap.get(d, 0)
while d > 0:
d = (d-1)&key
result += umap.get(d, 0)
return result
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
N = len(A)
ans = 0
count = collections.Counter()
for i in range(N):
for j in range(N):
count[A[i] & A[j]] += 1
for k in range(N):
for v in count:
if A[k] & v == 0:
ans += count[v]
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
cnt = collections.Counter()
result = 0
for i in A:
for j in A:
cnt[i & j] += 1
for i in A:
for j, k in cnt.items():
if i & j == 0:
result += k
return result
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
cnt=0
d={}
for i in range(len(A)):
for j in range(len(A)):
if A[i]&A[j] not in d:
d[A[i]&A[j]]=1
else:
d[A[i]&A[j]]+=1
for i in range(len(A)):
for j in d:
if A[i]&j==0:
cnt+=d[j]
return cnt
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
# class Solution:
# def countTriplets(self, A: List[int]) -> int:
# N, M = 1<<16, 3
# dp = [[0]*N for _ in range(M+1)]
# dp[0][-1] = 1
# for m in range(M):
# for n in range(N):
# for a in A:
# dp[m+1][a&n] += dp[m][n]
# return dp[-1][0]
class Solution:
def countTriplets(self, A: 'List[int]') -> 'int':
N = len(A)
ans = 0
count = collections.Counter()
for i in range(N):
for j in range(N):
count[A[i]&A[j]] += 1
for k in range(N):
for v in count:
if A[k] & v == 0:
ans += count[v]
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import defaultdict
from collections import Counter
class Solution:
def countTriplets(self, A: List[int]) -> int:
combo = collections.Counter(x&y for x in A for y in A)
res = 0
for a in A:
for k,v in list(combo.items()):
if a&k==0:
res+=v
return res
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
combo = collections.Counter(x&y for x in A for y in A)
return sum(combo[k] for z in A for k in combo if z&k == 0)
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import defaultdict
class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
ans = 0
A_counts = defaultdict(lambda: 0)
for num in A:
A_counts[num] += 1
counts = defaultdict(lambda: 0)
for n1, count1 in list(A_counts.items()):
for n2, count2 in list(A_counts.items()):
counts[n1&n2] += count1*count2
for n1, count1 in list(A_counts.items()):
for n2, count2 in list(counts.items()):
if n1 & n2 == 0:
ans += count1*count2
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import Counter
class Solution:
def countTriplets(self, A: List[int]) -> int:
## https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/discuss/227309/C%2B%2B-naive-O(n-*-n)
cnt = Counter()
for a in A:
for b in A:
cnt[a&b] += 1
res = 0
for a in A:
for k, v in cnt.items():
if a&k==0:
res += v
return res
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import defaultdict
class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
ans = 0
A_counts = defaultdict(lambda: 0)
for num in A:
A_counts[num] += 1
counts = defaultdict(lambda: 0)
for n1, count1 in list(A_counts.items()):
for n2, count2 in list(A_counts.items()):
counts[n1&n2] += count1*count2
for n in A:
for num, count in list(counts.items()):
if num & n == 0:
ans += count
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
count=0
dic=dict()
for i in range(len(A)):
for j in range(i,len(A)):
r=A[i]&A[j]
dic[r]=dic.get(r,0)+(1 if i==j else 2)
result=0
for i in range(len(A)):
for k in dic:
if A[i]&k==0:
result+=dic[k]
return result
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
counters = defaultdict(int)
counters[0] = len(A)
for num in A:
mask = (~num) & ((1 << 16) - 1)
sm = mask
while sm != 0:
counters[sm] += 1
sm = (sm - 1) & mask
return sum(counters[num1 & num2] for num1 in A for num2 in A)
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
N = len(A)
ans = 0
count = dict()
for i in range(N):
for j in range(N):
tmp = A[i]&A[j]
if tmp not in count:
count[tmp] = 1
else:
count[tmp] += 1
for k in range(N):
for v in count:
if A[k] & v == 0:
ans += count[v]
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
d={}
ans=0
for i in range(len(A)):
for j in range(len(A)):
a=A[i]&A[j]
d[a]=d.get(a,0)+1
for i in range(len(A)):
for j in list(d.keys()):
if A[i]&j==0:
ans+=d[j]
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
import collections
class Solution:
def countTriplets(self, A: List[int]) -> int:
N = len(A)
ans = 0
count = collections.Counter()
for i in range(N):
for j in range(N):
count[A[i]&A[j]] += 1
for k in range(N):
for v in count:
if A[k] & v == 0:
ans += count[v]
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
C = defaultdict(int)
for i in range(n):
C[A[i]] += 1
for j in range(i + 1, n):
C[A[i] & A[j]] += 2
return sum(c * sum((x & y) == 0 for y in A) for x, c in C.items())
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
C = defaultdict(int)
for i, x in enumerate(A):
C[x] += 1
for j in range(i + 1, n):
C[x & A[j]] += 2
return sum(c * sum((x & y) == 0 for y in A) for x, c in C.items())
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
C = defaultdict(int)
for i in range(n):
C[A[i]] += 1
for j in range(i + 1, n):
C[A[i] & A[j]] += 2
res = 0
for x, c in C.items():
res += c * sum((x & y) == 0 for y in A)
return res
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
memo = dict()
for i in range(len(A)):
for j in range(i,len(A)):
r=A[i]&A[j]
memo[r]=memo.get(r,0)+(1 if i==j else 2)
ret=0
for i in range(len(A)):
for k in memo:
if A[i]&k==0:
ret+=memo[k]
return ret
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
n2 = n*n
dp = {}
ways = 0
for i in range(n):
for j in range(n):
res = A[i] & A[j]
dp[res] = dp.get(res, 0) + 1
for i in range(n):
for tgt, ct in dp.items():
if A[i] & tgt == 0:
ways += ct
#print(A[i], tgt, ct)
return ways
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import defaultdict
class Solution:
def countTriplets(self, A: List[int]) -> int:
n = len(A)
ans = 0
counts = defaultdict(lambda: 0)
for i in range(n):
for j in range(n):
counts[A[i]&A[j]] += 1
for k in range(n):
for num, count in list(counts.items()):
if num & A[k] == 0:
ans += count
return ans
# (a & b) & c== 0
# # 0010
# # 0001
# # 1101
# # 0011
# # 1110
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import defaultdict
class Solution:
def countTriplets(self, A: List[int]) -> int:
mp = defaultdict(int) # key: elem_value, value: number_of_complement_elements
mask = (1 << 16) - 1
for x in A:
y = mask ^ x
s = y
while s:
mp[s] += 1
s = (s - 1) & y
n = len(A)
cnt = 0
for i in range(n):
if A[i] == 0:
cnt += n * n
continue
for j in range(n):
if A[i] & A[j] == 0:
cnt += n
continue
cnt += mp[A[i] & A[j]]
return cnt
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
from collections import defaultdict
dic=defaultdict(int)
n=len(A)
dp=defaultdict(int)
for i in range(n):
for j in range(n):
dic[(i,j)] = A[i]&A[j]
dp[A[i]&A[j]]+=1
ans=0
for i in range(n):
for x in dp:
if x&A[i]==0:
ans+=dp[x]
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
d = dict()
for i in range(len(A)):
for j in range(len(A)):
product = A[i] & A[j]
if product in d:
d[product]+=1
else:
d[product] = 1
ans = 0
for i in range(len(A)):
for k,v in d.items():
if A[i]& k == 0:
ans+=v
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
c = Counter(x & y for x in A for y in A)
return sum(c[xy] for xy in c for z in A if xy & z == 0)
# bitsOneDict = {\"@\": 20}
# ans = 0
# ALen = len(A)
# for j, num in enumerate(A):
# i = 0
# pointer = bitsOneDict
# while num > 0:
# if num & 1:
# if i not in pointer:
# pointer[i] = {}
# if \"@\" not in pointer:
# pointer[\"@\"] = i
# else:
# pointer[\"@\"] = max(i, pointer[\"@\"])
# pointer = pointer[i]
# num >>= 1
# i += 1
# if \"$\" in pointer:
# pointer[\"$\"].add(j)
# else:
# pointer[\"$\"] = set([j])
# if \"@\" not in pointer:
# pointer[\"@\"] = i+1
# else:
# pointer[\"@\"] = max(i+1, pointer[\"@\"])
# for j1, num1 in enumerate(A):
# for j2 in range(j1, ALen):
# num2 = A[j2]
# num12 = num1 & num2
# if num12 == 0:
# if j1 == j2:
# ans += ALen
# else:
# ans += ALen*2
# else:
# pointers = [bitsOneDict]
# i = 0
# subAns = set()
# while i < 16:
# if num12 & 1 == 0:
# newPointers = []
# for k in range(len(pointers)-1, -1, -1):
# pointer = pointers[k]
# if i in pointer:
# newPointers.append(pointer[i])
# else:
# if i > pointer[\"@\"]:
# if \"$\" in pointer:
# subAns |= pointer[\"$\"]
# pointers.pop(k)
# pointers += newPointers
# num12 >>= 1
# i += 1
# for pointer in pointers:
# if \"$\" in pointer:
# subAns |= pointer[\"$\"]
# if j1 == j2:
# ans += len(subAns)
# else:
# ans += len(subAns)*2
# return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
# ans = 0
n = len(A)
@lru_cache(None)
def dfs(i, pre):
if i == 4 and not pre:
return 1
ans = 0
for a in A:
if (i > 1 and not pre & a) or (i == 1 and not a):
ans += n ** (3 - i)
elif i < 3:
ans += dfs(i + 1, pre & a if i > 1 else a)
return ans
return dfs(1, None)
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
memo=dict()
for i in range(len(A)):
for j in range(i,len(A)):
r=A[i]&A[j]
memo[r]=memo.get(r,0)+(1 if i==j else 2)
ret=0
for i in range(len(A)):
for k in memo:
if A[i]&k==0:
ret+=memo[k]
return ret
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
from collections import Counter
class Solution:
def countTriplets(self, A: List[int]) -> int:
c = Counter()
for val in A:
val = (~val) & 0xffff
mask = val
c[val] += 1
while val > 0:
val = (val-1) & mask
c[val] += 1
ans = 0
for v1 in A:
for v2 in A:
val = v1 & v2
if val in c:
ans += c[val]
return ans
|
Given an array of integers A, find the number of triples of indices (i, j, k) such that:
0 <= i < A.length
0 <= j < A.length
0 <= k < A.length
A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.
Example 1:
Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2
Note:
1 <= A.length <= 1000
0 <= A[i] < 2^16
|
class Solution:
def countTriplets(self, A: List[int]) -> int:
tot = 1<<16
cnt = [0 for _ in range(tot)]
for a in A:
for b in A:
cnt[a&b]+=1
ans = 0
for e in A:
s = 0
while s<tot:
if s&e==0:
ans += cnt[s]
s += 1
else:
s += (e&s)
return ans
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.