Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: gcd = nums[0] for a in nums: while a: gcd, a = a, gcd % a return gcd == 1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def gcd(self,a,b): return a if b==0 else self.gcd(b,a%b) def isGoodArray(self, nums: List[int]) -> bool: nums=list(set(nums)) n=len(nums) if n==1 and nums[0]==1:return True cur=nums[0] for i in range(1,n): cur=self.gcd(cur,nums[i]) if cur==1:return True return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def gcd(self, a, b): if a == 0: return b if b == 0: return a if a == 1 or b == 1: return 1 while b != 0: a, b = b, a % b return a def isGoodArray(self, nums: List[int]) -> bool: g = 0 for n in nums: if g == 0: g = n elif n == 1: return True else: while n: g, n = n, g % n if g == 1: return True return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def gcd(self, a:int, b:int) -> int: a,b = max(a,b),min(a,b) while b>0: a,b = b,a%b return a def isGoodArray(self, nums: List[int]) -> bool: lowest = nums[0] for i in nums[1:]: lowest = self.gcd(lowest,i) return lowest==1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: if len(nums) == 1: return nums[0] == 1 def gcd(a, b): while b: a, b = b, a % b return a gcd_val = nums[0] for i in range(1, len(nums)): gcd_val = gcd(gcd_val, nums[i]) if gcd_val == 1: return True return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
def gcd(a,b): if(b==0): return a return gcd(b,a%b) class Solution: def isGoodArray(self, nums: List[int]) -> bool: if(nums==[1]): return True i=0 a=nums[i] for j in range(i+1,len(nums)): if(a<nums[j]): b=gcd(nums[j],a) if(b==1): return True else: a=b else: b=(gcd(a,nums[j])) if(b==1): return True else: a=b return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: gcd = nums[0] for n in nums: while n: gcd, n = n, gcd % n return gcd == 1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: def gcd(a, b): a, b = max(a,b), min(a,b) while b: a, b = b, a%b return a if len(nums) == 1: if nums != [1]: return False else: return True i = 0 d = nums[0] while i < len(nums): d = gcd(nums[i],d) if d == 1: return True i += 1 return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: def gcd(x, y): if x < y: x, y = y, x if not x % y: return y return gcd(x % y, y) if len(nums) == 1: if nums[0] == 1: return True return False while True: x = nums.pop(-1) y = nums.pop(-1) z = gcd(x, y) if not nums: return z == 1 nums.append(z)
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: def euclid(a,b): q, r = max(a,b), min(a,b) while r > 1 and q > 0: r, q = q % r, r return r if r > 0 else q if len(nums) == 1: return True if nums[0] == 1 else False nums.sort() for i in range(1,len(nums)): nums[i] = euclid(nums[i],nums[i-1]) if nums[i] == 1: return True return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: def euclid(a,b): q, r = max(a,b), min(a,b) while r > 1 and q > 0: r, q = q % r, r return r if r > 0 else q if len(nums) == 1: return True if nums[0] == 1 else False for i in range(1,len(nums)): nums[i] = euclid(nums[i],nums[i-1]) if nums[i] == 1: return True return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: def gcd(a, b): if b>0: return gcd(b, a%b) return a if not nums: return False if len(nums)==1: return nums[0]==1 num = nums[0] for i in range(1, len(nums)): ans = gcd(num, nums[i]) if ans==1: return True num = ans return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: if len(nums) == 1: if nums[0] == 1: return True else: return False ans = self.gcd(nums[0],nums[1]) for num in nums[2:]: ans = self.gcd(ans,num) if ans == 1: return True return ans == 1 def gcd(self, a,b) : if b == 0: return a return self.gcd(b,a%b)
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
from math import gcd from functools import reduce class Solution: def isGoodArray(self, nums: List[int]) -> bool: return (reduce((lambda x, y: gcd(x,y)), nums) == 1) # class Solution: # def gcd(x, y): # gcd_ = i # for i in range(2, min(x, y)): # if x%i == 0 and y%i == 0: # gcd_ = i # return gcd_ # def isGoodArray(self, nums: List[int]) -> bool: # itergcd = nums[0] # for i in nums: # itergcd = gcd(itergcd, i) # return itergcd == 1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: import math curr_gcd = nums[0] for i in range(1, len(nums)): curr_gcd = math.gcd(curr_gcd, nums[i]) if curr_gcd == 1: return True return curr_gcd == 1 # if 1 in nums: return True # def find_factors(num): # sol = set() # for i in range(1,math.ceil(math.sqrt(num))): # if num % i == 0: # sol.add(i) # sol.add(num/i) # # sol.remove(num) # sol.remove(1) # return sol # all_items = set() # factors_list = [] # for num in nums: # temp_set = find_factors(num) # for it in temp_set: # all_items.add(it) # factors_list.append(temp_set) # # print(all_items) # # print(factors_list) # for it in all_items: # if it == 1: continue # if all([it in lis for lis in factors_list]): # return False # return True ''' 29, 6, 10 29, 3*2, 5*2 '''
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: for i in range(len(nums)): nums[i] = gcd(nums[i],nums[i-1]) if i > 0 else nums[0] return nums[-1]==1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: def gcd(x,y): if y > x: return gcd(y,x) if y == 0: return x return gcd(y,x%y) cur = nums[0] for i in range(1, len(nums)): cur = gcd(cur,nums[i]) return cur == 1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: g=nums[0] for i in nums:g=gcd(i,g) return g==1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: gcd = nums[0] for num in nums: while num: gcd, num = num, gcd % num if gcd == 1: return True return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: def gcd(a,b): if b > a: a,b = b,a while (b > 0): r = divmod(a,b)[1] a = b b = r return a if len(nums) == 0: return 0 if len(nums) == 1: return nums[0] == 1 ans = nums[0] for i in range(1,len(nums)): ans = gcd(ans,nums[i]) if ans == 1: return True return ans == 1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def gcd(self, nums): g = nums[0] for x in nums: while x: g, x = x, g%x return g # a: list of integers # d: desired GCD of the integers in a def bezout(self, a: [int], d): if self.gcd(a) == d: return True return False def isGoodArray(self, nums: [int]) -> bool: return self.bezout(nums, 1)
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: gcd = nums[0] for n in nums: while n: gcd, n = n, gcd % n return gcd == 1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def getGCD(self, x: int, y: int) -> int: while y: x, y = y, x % y return x def getLCM(self, x: int, y: int) -> int: return x * y // self.getGCD(x, y) def isGoodArray(self, nums: List[int]) -> bool: # question is equivalent to have any substr with gcd equals 1, iff array has gcd equals to 1 n = len(nums) x, i = nums[0], 1 while i < n: x = self.getGCD(x, nums[i]) if x == 1: return True i += 1 return x == 1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, A: List[int]) -> bool: gcd = A[0] for a in A: while a: gcd, a = a, gcd % a if gcd == 1: return gcd return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
from fractions import gcd from functools import reduce class Solution: def isGoodArray(self, nums: List[int]) -> bool: def find_gcd(list): x = reduce(gcd, list) return x if find_gcd(nums)==1: return True return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
# from math import gcd # from functools import reduce # class Solution: # def isGoodArray(self, nums: List[int]) -> bool: # return (reduce((lambda x, y: gcd(x,y)), nums) == 1) class Solution: def gcd(x, y): gcd_ = i for i in range(2, min(x, y)): if x%i == 0 and y%i == 0: gcd_ = i return gcd_ def isGoodArray(self, nums: List[int]) -> bool: start = nums[0] for i in nums: start = gcd(start, i) return start == 1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: g=nums[0] for i in nums: g=self.hcfnaive(i,g) return g==1 def hcfnaive(self,a,b): if(b==0): return a else: return self.hcfnaive(b,a%b)
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, x: List[int]) -> bool: g=x[0] for i in x:g=gcd(i,g) return g==1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
from functools import reduce class Solution: def isGoodArray(self, nums: List[int]) -> bool: return reduce(gcd, nums) < 2
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: if len(nums) == 1: if nums[0] == 1: return True else: return False now = -1 for i in range(1, len(nums)): if i == 1: now = math.gcd(nums[i-1], nums[i]) else: now = math.gcd(nums[i],now) if now == 1: return True return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: res = nums[0] for i in range(1, len(nums)): res = math.gcd(res, nums[i]) if res == 1: return True return res == 1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: if len(nums) == 1: return True if nums[0] == 1 else False for i in range(1,len(nums)): nums[i] = gcd(nums[i],nums[i-1]) if nums[i] == 1: return True return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: # primeSet = set() # def isPrime(x): # if x < 2 or x in primeSet: # return False # i = 2 # limit = int(x**0.5) # while i < limit +1: # if x%i == 0: # return False # limit = x//i # i += 1 # primeSet.add(x) # return True # numsLen = len(nums) # primeCounter = 0 # for i in range(numsLen): # if nums[i] == 1: # return True # if isPrime(nums[i]): # primeCounter += 1 # if primeCounter == 2: # return True # else: # for j in range(i): # if isPrime(abs(nums[j] - nums[i])): # primeCounter += 1 # if primeCounter == 2: # return True # return False g=nums[0] for i in nums: g=gcd(i,g) return g==1
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, nums: List[int]) -> bool: for i in range(len(nums)): nums[i] = gcd(nums[i],nums[i-1]) if i > 0 else nums[0] if nums[i] == 1: return True return False
Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. Return True if the array is good otherwise return False.   Example 1: Input: nums = [12,5,7,23] Output: true Explanation: Pick numbers 5 and 7. 5*3 + 7*(-2) = 1 Example 2: Input: nums = [29,6,10] Output: true Explanation: Pick numbers 29, 6 and 10. 29*1 + 6*(-3) + 10*(-1) = 1 Example 3: Input: nums = [3,6] Output: false   Constraints: 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^9
class Solution: def isGoodArray(self, A: List[int]) -> bool: if len(A) == 0: return False gcd = A[0] for a in A: while a: gcd, a = a, gcd % a return gcd == 1
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: # valid string? can be seperated into full croaks: ### dict of letters. c, r, o, a, k should all be equal, nothing else in if len(croakOfFrogs)%5!=0 or croakOfFrogs[0]!='c' or croakOfFrogs[-1]!='k': return -1 letters = { 'c': 0, 'r': 0, 'o': 0, 'a': 0, 'k': 0 } frogs = 0 temp = 0 for l in croakOfFrogs: letters[l] += 1 temp = letters['c'] - letters['k'] if temp > frogs: frogs = temp c_count = letters['c'] for letter in letters: if letters[letter] != c_count: return -1 return frogs
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: croaks = {'c':[], 'r':[], 'o':[], 'a':[]} nfrogs = 0 icroak = 0 curfrogs = 0 for sound in croakOfFrogs: if sound == 'c': icroak += 1 croaks['c'].append(icroak) curfrogs += 1 nfrogs = max(curfrogs, nfrogs) elif sound == 'r': if croaks['c']: croaks['r'].append(croaks['c'].pop()) else: return -1 elif sound == 'o': if croaks['r']: croaks['o'].append(croaks['r'].pop()) else: return -1 elif sound == 'a': if croaks['o']: croaks['a'].append(croaks['o'].pop()) else: return -1 elif sound == 'k': if croaks['a']: croaks['a'].pop() curfrogs -= 1 else: return -1 else: return -1 for sound in croaks: if croaks[sound] != []: return -1 return nfrogs
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: croaks = {'c':[], 'r':[], 'o':[], 'a':[]} maxfrogs = 0 i_croak = 0 nfrogs = 0 for sound in croakOfFrogs: if sound == 'c': i_croak += 1 croaks['c'].append(i_croak) nfrogs += 1 maxfrogs = max(nfrogs, maxfrogs) elif sound == 'r': if croaks['c']: croaks['r'].append(croaks['c'].pop()) else: return -1 elif sound == 'o': if croaks['r']: croaks['o'].append(croaks['r'].pop()) else: return -1 elif sound == 'a': if croaks['o']: croaks['a'].append(croaks['o'].pop()) else: return -1 elif sound == 'k': if croaks['a']: croaks['a'].pop() nfrogs -= 1 else: return -1 else: return -1 for sound in croaks: if croaks[sound] != []: return -1 return maxfrogs
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: ans = 1 waitingFor = defaultdict(int) croak = 'croak' croakDict= {'c': 0, 'r': 1, 'o': 2, 'a': 3, 'k': 4} # waitingFor['c'] = 0 for char in croakOfFrogs: if char == 'c': if waitingFor[char] != 0 and ans <= waitingFor[char]: ans += 1 waitingFor[char] += 1 waitingFor[croak[croakDict[char]+1]] += 1 else: if waitingFor[char] == 0: return -1 waitingFor[char] -= 1 if char != 'k': waitingFor[croak[croakDict[char]+1]] += 1 else: waitingFor['c'] -= 1 # print(waitingFor) for char in waitingFor: if waitingFor[char] != 0: return -1 return ans
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: if len(croakOfFrogs)%5!=0 or croakOfFrogs[0]!='c' or croakOfFrogs[-1]!='k': return -1 max_frog_croak = 0 present_frog_croak = 0 d={} d['c']=0 d['r']=0 d['o']=0 d['a']=0 d['k']=0 for ch in croakOfFrogs: d[ch]+=1 if ch=='c': present_frog_croak+=1 if ch=='k': present_frog_croak-=1 max_frog_croak = max(max_frog_croak,present_frog_croak) if d['c']!=d['r'] or d['r']!=d['o'] or d['o']!=d['a'] or d['a']!=d['k']: return -1 return max_frog_croak
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croak: str) -> int: if len(croak) == 0: return 0 if croak[0] != 'c': return -1 if len(croak) % 5 != 0: return -1 arr = [0 for _ in range(6)] arr[0] = 9999999 index = {'c':1, 'r':2, 'o':3, 'a':4, 'k':5} for letter in croak: i = index[letter] if arr[i-1] > 0: if i == 1 and arr[5]: arr[5] -= 1 arr[i-1] -= 1 arr[i] += 1 else: return -1 for i in arr[1:-1]: if i != 0: return -1 return arr[-1]
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: chr_c, chr_r, chr_o, chr_a = 0, 0, 0, 0 count = 0 for s in croakOfFrogs: if s == 'c': chr_c += 1 elif s == 'r': if chr_c <= 0: return -1 chr_c -= 1 chr_r += 1 elif s == 'o': if chr_r <= 0: return -1 chr_r -= 1 chr_o += 1 elif s == 'a': if chr_o <= 0: return -1 chr_o -= 1 chr_a += 1 else: if chr_a <= 0: return -1 chr_a -= 1 count = max(count, chr_c + chr_r + chr_o + chr_a) # 没有唱完整五个字符,那么必然会大于0 if chr_c + chr_r + chr_o + chr_a > 0: return -1 return count
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: # valid string? can be seperated into full croaks: ### dict of letters. c, r, o, a, k should all be equal, nothing else in if len(croakOfFrogs)%5!=0 or croakOfFrogs[0]!='c' or croakOfFrogs[-1]!='k': return -1 letters = { 'c': 0, 'r': 0, 'o': 0, 'a': 0, 'k': 0 } frogs = 0 temp = 0 for l in croakOfFrogs: letters[l] += 1 temp = letters['c'] - letters['k'] if temp > frogs: frogs = temp c_count = letters['c'] for letter in letters: if letters[letter] != c_count: return -1 return frogs
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: curr=0 maxcurr=0 c=r=o=a=k=0 for i in croakOfFrogs: if i=='c': c+=1 curr+=1 elif i=='r': r+=1 elif i=='o': o+=1 elif i=='a': a+=1 else: k+=1 curr-=1 maxcurr=max(curr,maxcurr) if c<r or r<o or o<a or a<k: return -1 if curr==0 and c==r and r==o and o==a and a==k: return maxcurr return -1
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: if len(croakOfFrogs)%5!=0 or croakOfFrogs[0]!='c' or croakOfFrogs[-1]!='k': return -1 max_frog_croak = 0 present_frog_croak = 0 d={} d['c']=0 d['r']=0 d['o']=0 d['a']=0 d['k']=0 for ch in croakOfFrogs: d[ch]+=1 if ch=='c': present_frog_croak+=1 if ch=='k': present_frog_croak-=1 max_frog_croak = max(max_frog_croak,present_frog_croak) if d['c']!=d['r'] or d['r']!=d['o'] or d['o']!=d['a'] or d['a']!=d['k']: return -1 return max_frog_croak
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: neighcount = [0,0,0,0,0] minhorses = 0 nstarted = 0 nended = 0 for s in croakOfFrogs: if s == 'c': neighcount[0] += 1 nstarted += 1 elif s == 'r': neighcount[1] += 1 neighcount[0] -= 1 elif s == 'o': neighcount[2] += 1 neighcount[1] -= 1 elif s == 'a': neighcount[3] += 1 neighcount[2] -= 1 elif s == 'k': neighcount[4] += 1 neighcount[3] -= 1 nstarted -= 1 else: return -1 if nstarted > minhorses: minhorses = nstarted if neighcount[0] < 0 or neighcount[1] < 0 or neighcount[2] < 0 or neighcount[3] < 0: return -1 if neighcount[0] == 0 and neighcount[1] == 0 and neighcount[2] == 0 and neighcount[3] == 0: return minhorses else: return -1
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: if not len(croakOfFrogs): return 0 c = 0 r = 0 o = 0 a = 0 k = 0 count = 0 for ch in croakOfFrogs: if ch == 'c': count += 1 c += 1 elif ch == 'r': if not c: return -1 c -= 1 r += 1 elif ch == 'o': if not r: return -1 r -= 1 o += 1 elif ch == 'a': if not o: return -1 o -= 1 a += 1 elif ch == 'k': if not a: return -1 a -= 1 k = max(k, count) count -= 1 else: return -1 if not c and not r and not o and not a: return k else: return -1
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: # print(len(croakOfFrogs)) if len(croakOfFrogs) % 5 > 0: return -1 pos = {'c': 0, 'r': 1, 'o': 2, 'a': 3, 'k': 4} count = [0]*5 res = 1 for c in croakOfFrogs: count[pos[c]] += 1 # print(count) if pos[c] > 0: if count[pos[c]] > count[pos[c]-1]: return -1 if pos[c] == 4: for i in range(5): count[i] -= 1 elif pos[c] == 0: res = max(res, count[0]) for i in range(4): if count[i+1] != count[i]: return -1 return res
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: frogs, d = [], {c: [] for c in 'croak'} for c in croakOfFrogs: bucket = 'croak'['croak'.find(c)-1] if not d[bucket]: if bucket != 'k': return -1 frogs.append([]) d[c].append(len(frogs) - 1) else: d[c].append(d[bucket].pop()) if c == 'c': frogs[d[c][-1]].clear() frogs[d[c][-1]].append(c) return len(frogs) if all(len(frog) == 5 for frog in frogs) else -1
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: r = 0 o = 0 a = 0 k = 0 frogs = 0 max_frogs = 0 for L in croakOfFrogs: if L == 'c': frogs += 1 max_frogs = max(max_frogs, frogs) r += 1 elif L == 'r': if r == 0: return -1 else: r -= 1 o += 1 elif L == 'o': if o == 0: return -1 else: o -= 1 a += 1 elif L == 'a': if a == 0: return -1 else: a -= 1 k += 1 else: if k == 0: return -1 else: k -= 1 frogs -= 1 if r + o + a + k != 0: return -1 return max_frogs
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: if len(croakOfFrogs) % 5 != 0: return -1 min_val = 0 cnt = {char: 0 for char in 'croak'} for char in croakOfFrogs: if char not in 'croak' or not cnt['c'] >= cnt['r'] >= cnt['o'] >= cnt['a'] >= cnt['k']: return -1 cnt[char] += 1 min_val = max(min_val, cnt['c'] - cnt['k']) return min_val if cnt['c'] == cnt['r'] == cnt['o'] == cnt['a'] == cnt['k'] else -1
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: # def minNumberOfFrogs(self, croakOfFrogs: str) -> int: # neighcount = [0,0,0,0,0] # minhorses = 0 # nstarted = 0 # nended = 0 # for s in croakOfFrogs: # if s == 'c': # neighcount[0] += 1 # nstarted += 1 # elif s == 'r': # neighcount[1] += 1 # neighcount[0] -= 1 # if neighcount[0] < 0: # return -1 # elif s == 'o': # neighcount[2] += 1 # neighcount[1] -= 1 # if neighcount[1] < 0: # return -1 # elif s == 'a': # neighcount[3] += 1 # neighcount[2] -= 1 # if neighcount[2] < 0: # return -1 # elif s == 'k': # neighcount[4] += 1 # neighcount[3] -= 1 # nstarted -= 1 # if neighcount[3] < 0: # return -1 # else: # return -1 # if nstarted > minhorses: # minhorses = nstarted # neighcount[4] = 0 # if max(neighcount) == 0: # return minhorses # else: # return -1 def minNumberOfFrogs(self, croakOfFrogs: str) -> int: if len(croakOfFrogs)%5!=0 or croakOfFrogs[0]!='c' or croakOfFrogs[-1]!='k': return -1 max_frog_croak = 0 present_frog_croak = 0 d={} d['c']=0 d['r']=0 d['o']=0 d['a']=0 d['k']=0 for ch in croakOfFrogs: d[ch]+=1 if ch=='c': present_frog_croak+=1 if ch=='k': present_frog_croak-=1 max_frog_croak = max(max_frog_croak,present_frog_croak) if d['c']!=d['r'] or d['r']!=d['o'] or d['o']!=d['a'] or d['a']!=d['k']: return -1 return max_frog_croak
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: if len(croakOfFrogs) % 5 != 0: return -1 nums = [0] * 5 dic = {'c': 0, 'r': 1, 'o':2, 'a': 3, 'k' : 4} res = 1 for ch in croakOfFrogs: if dic[ch] != 0 and nums[dic[ch]] >= nums[dic[ch] - 1]: return -1 nums[dic[ch]] += 1 res = max(res, nums[0]) if ch == 'k': for i in range(5): nums[i] -= 1 return res
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: c, r, o, a, k, in_use, answer = 0, 0, 0, 0, 0, 0, 0 for d in croakOfFrogs: if d == 'c': c, in_use = c+1, in_use+1 elif d == 'r': r += 1 elif d == 'o': o += 1 elif d == 'a': a += 1 else: k, in_use = k+1, in_use-1 answer = max(answer, in_use) if c < r or r < o or o < a or a < k: return -1 if in_use == 0 and c == r and r == o and o == a and a == k: return answer return -1
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: arr = [0] * 5 for item in croakOfFrogs: if item == 'c': arr[0] += 1 elif item == 'r': arr[1] += 1 for i in range(0, 1): if arr[i] < arr[1]: return -1 elif item == 'o': arr[2] += 1 for i in range(0, 1): if arr[i] < arr[2]: return -1 elif item == 'a': arr[3] += 1 for i in range(0, 1): if arr[i] < arr[3]: return -1 else: arr[4] += 1 for i in range(0, 1): if arr[i] < arr[4]: return -1 cur = arr[0] for i in range(1, 5): if arr[i] != cur: return -1 ''' At this point there is a valid string ''' cur_frogs = 0 result = float('-inf') p = 0 while p < len(croakOfFrogs): if croakOfFrogs[p] == 'c': cur_frogs += 1 elif croakOfFrogs[p] == 'k': cur_frogs -= 1 result = max(cur_frogs, result) p += 1 return result
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: dic = {'c': 0, 'r': 1, 'o': 2, 'a': 3, 'k': 4} num = 0 num_c = 0 num_k = 0 freq = [0] * 5 if not croakOfFrogs[0] == 'c': return -1 # count the number of c before the first k shows up for ch in croakOfFrogs: if 'c' == ch: if num_k == 0: num_c += 1 else: num_k -= 1 num = max(num_c, num) if 'k' == ch: num_k += 1 # check validation for ch in croakOfFrogs: freq[dic[ch]] += 1 for idx in range(1, 5): if freq[idx] > freq[idx - 1]: return -1 for idx in range(1, 5): if not freq[idx] == freq[idx-1]: return -1 return num
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, s: str) -> int: rec = defaultdict(int) count = 0 if not s.startswith('c') or not s.endswith('k'): return -1 for i in range(len(s)): if rec['c']>=rec['r']>=rec['o']>=rec['a']>=rec['k']: rec[s[i]] += 1 if count < rec['c']: count += 1 if s[i] == 'k': for k in rec: rec[k] -= 1 else: return -1 return count # frog_counter = 0 # To count the minimum no. of frogs croaking # crk_dict = {'c':0,'r':0,'o':0,'a':0,'k':0} # create dictionary initializing the values to 0 # # The string must always start with 'c' # if croakOfFrogs[0] != 'c': # return (-1) # # The string must always end with 'k' # elif croakOfFrogs[-1] != 'k': # return (-1) # else: # for i in range(len(croakOfFrogs)): # # As the craok always start with c and the characters are sequential always the count of each of the char should greater than or equal to the next char of string 'craok' # if crk_dict['c'] >= crk_dict['r'] >= crk_dict['o'] >= crk_dict['a'] >= crk_dict['k']: # crk_dict[croakOfFrogs[i]] += 1 # # if 'c' is encountered in string before the completion of croak, that mean it is the other frog, hence add 1 to the frog counter # if frog_counter < crk_dict['c']: # frog_counter += 1 # # if the iterable gets 'k' that means the frog is free to croak again # if croakOfFrogs[i] == 'k': # for m in crk_dict: # crk_dict[m] -= 1 # else: # return (-1) # return frog_counter
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: croak = 'croak' frogs = 0 openCroaks = list() char2count = {'c':0, 'r':0, 'o':0, 'a':0, 'k':0} for char in croakOfFrogs: if char not in croak: return -1 if char == 'c': char2count[char]+=1 elif char2count[croak[croak.find(char)-1]]>0: char2count[croak[croak.find(char)-1]]-=1 if char!= 'k': char2count[char]+=1 else: return -1 frogs = max(frogs, sum(char2count.values())) return frogs if sum(char2count.values())==0 else -1
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
chars = ['c','r','o','a','k'] C = len(chars) cs = set(chars) class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: N=len(croakOfFrogs) if N==0: return 0 if N%5!=0: return -1 counter = Counter(croakOfFrogs) if len(counter)!=5: return -1 totFreq=None for c in counter: if not totFreq: totFreq==counter[c] elif totFreq!=counter[c]: return -1 dic = [0]* C ans=0 for i in range(N): c = croakOfFrogs[i] if c not in cs: return -1 if c == chars[0]: for j in range(1,len(chars)): dic[j]+=1 ans=max(ans,dic[C-1]) else: j = chars.index(c) dic[j]-=1 prev = dic[j] for k in range(j-1,-1,-1): if dic[k]>prev: return -1 prev=dic[k] return ans
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: if len(croakOfFrogs) % 5 != 0: return -1 dic = {'c': 0, 'r': 1, 'o': 2, 'a':3, 'k':4} numsCount = [0]*5 max_val = numOfFrogs = 0 for i in range(len(croakOfFrogs)): if croakOfFrogs[i] == 'c': numOfFrogs +=1 # print(numOfFrogs) if croakOfFrogs[i] == 'k': numOfFrogs -=1 max_val = max(numOfFrogs, max_val) numsCount[dic[croakOfFrogs[i]]] += 1 for j in range(0, 4): if numsCount[j] < numsCount[j+1]: return -1 return max_val
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: frog_counter = 0 # To count the minimum no. of frogs croaking crk_dict = {'c':0,'r':0,'o':0,'a':0,'k':0} # create dictionary initializing the values to 0 # The string must always start with 'c' if croakOfFrogs[0] != 'c': return (-1) # The string must always end with 'k' elif croakOfFrogs[-1] != 'k': return (-1) else: for i in range(len(croakOfFrogs)): # As the craok always start with c and the characters are sequential always the count of each of the char should greater than or equal to the next char of string 'craok' if crk_dict['c'] >= crk_dict['r'] >= crk_dict['o'] >= crk_dict['a'] >= crk_dict['k']: crk_dict[croakOfFrogs[i]] += 1 # if 'c' is encountered in string before the completion of croak, that mean it is the other frog, hence add 1 to the frog counter if frog_counter < crk_dict['c']: frog_counter += 1 # if the iterable gets 'k' that means the frog is free to croak again if croakOfFrogs[i] == 'k': for m in crk_dict: crk_dict[m] -= 1 else: return (-1) return frog_counter
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: croak = 'croak' croak_positions = {char: 0 for char in croak} max_size = 0 completed = 0 for char in croakOfFrogs: if char in croak_positions: croak_positions[char] += 1 else: return -1 for char, count in croak_positions.items(): if char != croak[0]: diff = croak_positions[croak[0]] - count if diff < 0: return -1 if diff > max_size: max_size = diff for char, count in croak_positions.items(): if count != croak_positions[croak[0]]: return -1 return max_size
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: res = croaking = c = r = o = a = k = 0 for x in croakOfFrogs: if x == 'c': c, croaking = c+1, croaking+1 res = max(res, croaking) elif x == 'r': r += 1 elif x == 'o': o += 1 elif x == 'a': a += 1 else: k, croaking = k+1, croaking-1 if c < r or r < o or o < a or a < k: return -1 return res if c == r == o == a == k and croaking == 0 else -1
Given the string croakOfFrogs, which represents a combination of the string "croak" from different frogs, that is, multiple frogs can croak at the same time, so multiple “croak” are mixed. Return the minimum number of different frogs to finish all the croak in the given string. A valid "croak" means a frog is printing 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to print all five letters to finish a croak. If the given string is not a combination of valid "croak" return -1.   Example 1: Input: croakOfFrogs = "croakcroak" Output: 1 Explanation: One frog yelling "croak" twice. Example 2: Input: croakOfFrogs = "crcoakroak" Output: 2 Explanation: The minimum number of frogs is two.  The first frog could yell "crcoakroak". The second frog could yell later "crcoakroak". Example 3: Input: croakOfFrogs = "croakcrook" Output: -1 Explanation: The given string is an invalid combination of "croak" from different frogs. Example 4: Input: croakOfFrogs = "croakcroa" Output: -1   Constraints: 1 <= croakOfFrogs.length <= 10^5 All characters in the string are: 'c', 'r', 'o', 'a' or 'k'.
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: frogs, c, r, o, a, k = 0, 0, 0, 0, 0, 0 for char in croakOfFrogs: if char == 'c': c += 1 frogs = max(frogs, c - k) elif char == 'r': r += 1 elif char == 'o': o += 1 elif char == 'a': a += 1 elif char == 'k': k += 1 else: return -1 if not c >= r >= o >= a >= k: return -1 if c == k: return frogs return -1
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: res = set() cur = set() for a in A: cur = {a | i for i in cur} cur |= {a} res |= cur return len(res)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: s = set() c = set() for x in A: c = {x|y for y in c} c.add(x) s|=c return len(s)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = set() cur = set([0]) for x in A: cur = {x | y for y in cur} | {x} ans |= cur return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: prev = set() curr = set() vals = set(A) for x in A: curr.add(x) for y in prev: curr.add(x|y) vals.add(x|y) prev = curr curr = set() return len(vals)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: curr = set() ans = set() for x in A: curr = {x | y for y in curr} | {x} ans |= curr return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: results = set() curr = {0} for a in A: curr = {a | b for b in curr} | {a} results |= curr return len(results)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: res, cur = set(), set() for i in A: cur = {i | j for j in cur} | {i} res |= cur return len(res)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: # O(N^3) to try oring every possible subarray # key fact: for any number k , k | k = k # also 0 | k = k for all k # one small optimization: scan through and delete any 0s or consecutive repeats # keep a list of or results s.t. or[i] = set of all possible results from oring subarrays that end at i # then or[i] = [A[i]] + [A[i] | res for res in or[i - 1]] -> use a set to prune dups at each step # you can just keep or[i] for most recent two i's # if not A: return [] # [1, 2, 4] # all_outcomes: {1, 2, 3, 4, 6, 7} # outcomes: {2, 3} # 4: temp {4, 6, 7} # todo(mywang): preprocess array and remove unnecessary stuff for i in range(len(A))[::-1]: if (i > 0 and A[i] == A[i - 1]): A.pop(i) all_outcomes = set([A[0]]) outcomes = set([A[0]]) for elt in A[1:]: outcomes = {elt} | {elt | val for val in outcomes} all_outcomes |= outcomes return len(all_outcomes)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans=set() cur=set() for i in A: cur = set([i|j for j in cur]) cur.add(i) ans|=cur return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: cur = set() all = set() for v in A: cur = set(e | v for e in cur) cur.add(v) all |= cur return len(all)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = set() cur = {0} for i in A: cur = {i | y for y in cur} | {i} ans |= cur return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: cur, res = set(), set() for i in A: cur = {i | j for j in cur} | {i} res |= cur return len(res)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
import math class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: res = set([]) tmp = [] maximpact = 0 for i in A: newtmp = [] newtmp_set = set([]) maximpact = max(maximpact, int(math.log(i, 2)) + 1 if i > 0 else 0) if i not in newtmp_set: newtmp_set.add(i) newtmp.append(i) res.add(i) if maximpact > 0: for j in tmp: k = i|j if k not in newtmp_set: newtmp_set.add(k) newtmp.append(k) res.add(k) if len(newtmp) >= maximpact: break tmp = newtmp return len(res)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution(object): def subarrayBitwiseORs(self, A): ans = set() cur = {0} for x in A: cur = {x | y for y in cur} | {x} ans |= cur return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: result, curr = set(), set() for a in A: curr = {a | j for j in curr} | {a} result |= curr return len(result)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: results = set() temp = {0} for cur in A: # print(temp) temp = {cur} | {cur | t for t in temp} results.update(temp) return len(results)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: s = set() global_set = set() ans = 0 for v in A: s.add(v) s = {x|v for x in s} global_set |= s return len(global_set)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
import math class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = {A[0]} pre = {A[0]} for a in A[1:]: pre = {a|x for x in pre} pre.add(a) ans.update(pre) return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: S, last = set(), set() for i in range(len(A)): last = {a|A[i] for a in last} last.add(A[i]) S |= last return len(S)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = set() cur = {0} for x in A: cur = {x | y for y in cur} | {x} ans |= cur return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: # my solution ... 760 ms ... 98 % ... 40.6 MB ... 15 % # time: O(nlogn) # space: O(logn) res = set() tot = set() for a in A: res = {prev | a for prev in res} | {a} # 得出以当前 a 结尾的 subarray tot |= res # 把结果并入 tot return len(tot)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = set() cur = set([0]) for x in A: cur = set([x | y for y in cur]) | {x} ans |= cur return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = set() previous = set() for v in A: current = set([i|v for i in previous]) current.add(v) ans.update(current) previous = current return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: st = set() cur = set() for a in A: cur = {b | a for b in cur} | {a} st |= cur return len(st)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
from typing import List class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: seen = set() cur = set() for num in A: cur_new = set() for num_seen in cur: cur_new.add(num_seen | num) cur_new.add(num) cur = cur_new seen |= cur return len(seen) def subarrayBitwiseORsN2(self, A: List[int]) -> int: dp = [[0] * len(A) for _ in range(len(A))] seen = set() count = 0 for start in reversed(list(range(len(A)))): for end in range(start, len(A)): if start == end: dp[start][end] = A[start] else: dp[start][end] = dp[start + 1][end] | A[start] if dp[start][end] not in seen: count += 1 seen.add(dp[start][end]) return count
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = set() cur = {0} for x in A: cur = {x|y for y in cur} | {x} ans |= cur return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: # Brute force with set, with list we can use if in list conditional statement # Slow & bad.... # rset = set() # for i in range(len(A)): # for j in range(i+1, len(A)+1): # res = 0 # for k in range(i, j): # res |= A[k] # rset.add(res) # return len(rset) # A cleaner code # Assume B[i][j] = A[i] | A[i+1] | ... | A[j] # Hash set cur stores all wise B[0][i], B[1][i], B[2][i], B[i][i]. # When we handle the A[i+1], we want to update cur # So we need operate bitwise OR on all elements in cur. # Also we need to add A[i+1] to cur. # In each turn, we add all elements in cur to res. # This method is better since it preserves the results of previous operation and make them useful res, cur = set(), set() for i in A: cur = {i|j for j in cur} | {i} res |= cur return len(res)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution(object): def subarrayBitwiseORs(self, A): ans = set() cur = {0} for x in A: cur = {x | y for y in cur} | {x} ans|=cur #print(cur) #print(\"ans\",ans) return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: result = set() cur = {0} for x in A: cur = {x | y for y in cur} | {x} result |= cur return len(result)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: s = set() res = set() for x in A: new_s = {x} for xx in s: new_s.add(xx | x) s = new_s res |= s return len(res)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
import itertools as it class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: res, cur = set(), set() for i in A: cur = {i | j for j in cur} | {i} res |= cur return len(res)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: cur, res = set(), set() for a in A: cur = {a|b for b in cur} | {a} res |= cur return len(res)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = set() cur = {0} for x in A: cur = {x|y for y in cur} | {x} ans |= cur return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: # 12:57 8/24/20 cur, res = set(),set() for num in A: cur = {num | j for j in cur} | {num} # res |= cur # cur = set(num | k for k in A) # cur.add(num) res.update(cur) return len(res) # res, cur = set(), set() # for i in A: # cur = {i | j for j in cur} | {i} # res |= cur # return len(res)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: # def findUnique(self, l): # u = [] # for num in l: # if num not in u: # u.append(num) # return self.findOR(u) # def findOR(self, l): # ret = 0 # for num in l: # ret = self.doBin(ret, num) # return ret # def doBin(self, a, b): # p = 0 # ret = 0 # while a or b: # if a%2 or b%2: # ret += pow(2,p) # p +=1 # a, b = a//2, b//2 # return ret # def subarrayBitwiseORs(self, A: List[int]) -> int: # #just unique combinations # res = 0 # seen = [] # #brute force # for i in range(len(A)): # for j in range(i,len(A)): # uniq = self.findUnique(A[i:j+1]) # if uniq not in seen: # seen.append(uniq) # res +=1 # return res def subarrayBitwiseORs(self, A): ans = set() cur = {0} for x in A: cur = {x | y for y in cur} | {x} ans |= cur return len(ans)
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: n = len(A) dp = set() ans = set() for j in range(n): dp_new = set() dp_new.add(A[j]) for x in dp: dp_new.add(x | A[j]) dp = dp_new ans.update(dp) return len(ans)