Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
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, dp = set(), set() for x in A: ndp = set() ndp.add(x) for y in dp: ndp.add(x|y) ans.add(y) dp = ndp for x in dp: ans.add(x) 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: # 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:]: temp = set([elt]) for out in outcomes: temp.add(out | elt) outcomes = temp 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() tp = set() tp.add(0) for i in A: tpp = set() tpp.add(i) for ii in tp: tpp.add(ii|i) tp=tpp for ii in tp: ans.add(ii) 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 = set() res = set() for n in A: s = {n} | {n | el for el in 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
class Solution: def subarrayBitwiseORs(self, arr: List[int]) -> int: ans = set() current = set() current.add(0) for i in range(len(arr)): a = arr[i] s = set() s.add(a) for y in current: s.add(a | y) current = s ans |= s return len(ans) # for x in arr: # current = {x | y for y in current} | {x} # # print(current) # ans |= current # # print(ans) # return len(ans) # brute force time complexity :O(n^2) # space complexity:O(n) if not arr:return 0 l ,ans = len(arr) ,set() for i in range(l): res = arr[i] ans.add(res) for j in range(i+1,l): res |= arr[j] ans.add(res) 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: fullSet = set() setAtI = {0} for i in range(len(A)): setAtI = {A[i]| y for y in setAtI} | {A[i]} fullSet |=setAtI return len(fullSet)
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: setA = set(A) prev = set() prev.add(A[0]) for val in A[1:]: temp = set() for p in prev: temp.add(p|val) setA.add(p|val) prev = temp prev.add(val) return len(setA)
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) for i in pre: ans.add(i) 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() vset=set() for a in A: newvset=set() for v in vset: newvset.add(a|v) ans.add(a|v) newvset.add(a) vset=newvset ans.add(a) 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() res = set() for i in A: temp = set([i]) for j in cur: temp.add(i|j) cur = temp for j in cur: res.add(j) 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: result, prev = set(), set() for num in A: temp = set() temp.add(num) result.add(num) for p in prev: temp.add(p|num) result.add(p|num) prev = temp 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, prev = set(), set() for a in A: temp = set() temp.add(a) S.add(a) for p in prev: temp.add(a|p) S.add(a|p) prev = temp 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: alive = set() dead = set() for num1 in A: tmp = {num1} for num2 in alive: dead.add(num2) tmp.add(num1|num2) alive = tmp return len(alive|dead)
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: arr_len = len(A) final_result_set = set() pre_result_set = set() pre_result_set.add(A[0]) final_result_set.add(A[0]) for i in range (1, arr_len): cur_result_set = set() for pre_result in pre_result_set: one_cur_result = pre_result | A[i] cur_result_set.add(one_cur_result) final_result_set.add(one_cur_result) cur_result_set.add(A[i]) final_result_set.add(A[i]) pre_result_set = cur_result_set return len(final_result_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
class Solution: def subarrayBitwiseORs(self, A): nums, n, pre = set(), len(A), set() for a in A: pre = {a} | {num | a for num in pre} nums |= pre return len(nums) # @param {Integer[]} a # @return {Integer} #def subarray_bitwise_o_rs(a) # Get subarrays # solutions = {} # iterating_array = a.clone # a.each do |m| # num = m # iterating_array.each do |m| # solutions[num] = 1 # num |= m # end # solutions[num] = 1 # iterating_array.shift # end # solutions.keys.length #end
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 a in A: cur = {p | a for p in cur} | {a} 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_set = [set([A[i]]) for i in range(len(A))] for i in range(1, len(A)): for prev_result in my_set[i-1]: my_set[i].add(A[i] | prev_result) return len(set.union(*my_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
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: curr=set() ans=set() for a in A: s=set() s.add(a) for i in curr: s.add(a|i) curr=s for t in curr: ans.add(t|a) 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_set = [set([A[i]]) for i in range(len(A))] for i in range(1, len(A)): for prev_result in my_set[i-1]: res = A[i] | prev_result my_set[i].add(res) return len(set.union(*my_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
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: sets=[{i} for i in A] for i in range(1,len(A)): for prev in sets[i-1]: sets[i].add(A[i]|prev) for num in sets[1:]: sets[0]|=num return len(sets[0])
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, Set class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: all_results: Set[int] = set() prev_results: Set[int] = set() for start_index in range(len(A)): curr_results: Set[int] = set() curr_results.add(A[start_index]) for prev_result in prev_results: curr_results.add(prev_result | A[start_index]) all_results |= curr_results prev_results = curr_results return len(all_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, curr = set(), set() for a in A: temp = {a} for b in curr: temp.add(a | b) curr = temp for num in curr: res.add(num) 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: sets=[{i} for i in A] for i in range(1,len(A)): for prev in sets[i-1]: sets[i].add(A[i]|prev) return len(set.union(*sets))
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() prev = set() prev.add(0) for i in range(len(A)): cur = set() cur.add(A[i]) for p in prev: res = p | A[i] if res != p: ans.add(p) cur.add(res) prev = cur for p in prev: ans.add(p) if 0 not in A: ans.remove(0) 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() cur = set() for x in A: cur = {y|x for y in cur} | set([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_set = set(A) curr = 0 prev = set() prev.add(A[0]) for num in A[1:]: temp = set() for p in prev: temp.add(num | p) my_set.add(num | p) prev = temp prev.add(num) return len(my_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
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: if A == []: return 0 # count = [0] # dic = {} # def OR(s): # if len(s) == 1: # return s[0] # res = s[0] # for item in s[1:]: # res = res | item # return res # for i in range(len(A)): # for j in range(i, len(A)): # res = OR(A[i:j+1]) # if res in dic: # continue # dic[res] = 0 # count[0] += 1 # return count[0] my_set = set(A) curr = 0 prev = set() prev.add(A[0]) for num in A[1:]: temp = set() for p in prev: temp.add(num | p) my_set.add(num | p) prev = temp prev.add(num) return len(my_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
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = set(A) prev = set([A[0]]) for i in A[1:]: temp = set() for p in prev: temp.add(i|p) ans.add(i|p) prev = temp prev.add(i) 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: # res, cur = set(), set() # for num in A: # cur = {val | num for val in cur} | {num} # res |= cur # return len(res) # list res = [] left, right = 0, 0 for val in A: right = len(res) res.append(val) for i in range(left, right): if res[-1] != (res[i] | val): res.append(res[i] | val) left = right return len(set(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: res = set() last = set() for x in A: new = {x} for y in last: new.add(x | y) res |= new last = new 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: vals = set() if len(A) < 2: return len(A) cur = {0} for a in A: cur = {a| y for y in cur} | {a} vals |= cur 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): # Tabulation is a list of sets, one for each number in A. # Each set, at position i, is initialised to containing the element at A[i] tabulation = [set([A[i]]) for i in range(len(A))] # And now we need to go through, updating the sets based on the previous set. for i in range(1, len(A)): for previous_result in tabulation[i - 1]: tabulation[i].add(A[i] | previous_result) # Return the number of unique numbers in the tabulation list. return len(set.union(*tabulation)) if len(A) > 0 else 0
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: results = set() pre = {0} for a in A: ''' cur = {a} for prev in pre: cur.add(prev | a) results |= cur pre = cur ''' pre = {a | p for p in pre} | {a} results |= pre 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, ans = set(), set() for a in A: s = {a} | {a | b for b in s} ans |= s 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: # https://blog.csdn.net/fuxuemingzhu/article/details/83511833 res = set() cur = set() for a in A: cur = {n | a for n 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: s = set() s1 = set() for n in A: s2 = set() for e in s1: s2.add(e|n) s1=s2 s1.add(n) s|=s1 # print(s) 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: res = set([]) tmp = set([]) for i in A: newtmp = set([]) for j in tmp: newtmp.add(i|j) res.add(i|j) newtmp.add(i) res.add(i) 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: def subarrayBitwiseORs(self, A: List[int]) -> int: # 12:57 8/24/20 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: ans, frontier = set(), set() for n in A: frontier = {x | n for x in frontier if x|n != -1} frontier.add(n) ans |= frontier 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: curr = {0} ans = set() for a in A: curr = {x|a for x in curr } | {a} 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: my_set = [set([A[i]]) for i in range(len(A))] ans = set(A) for i in range(1, len(A)): for prev_result in my_set[i-1]: res = A[i] | prev_result my_set[i].add(res) ans.add(res) 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_set = set(A) curr = 0 prev = set() prev.add(A[0]) for num in A[1:]: temp = set() curr |= num my_set.add(curr) prev.add(num) for p in prev: temp.add(num | p) my_set.add(num | p) prev = temp return len(my_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
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: dp = [] for i in range(len(A)): if i == 0: dp.append(set([A[i]])) else: inner = set([A[i]]) for d in dp[-1]: inner.add(d | A[i]) dp.append(inner) total = set() for d in dp: total.update(d) return len(total)
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() curr = set() for num in A: curr = {num | prev_num for prev_num in curr} | {num} result |= curr return len(result) # result = set() # prev = set([0]) # for num in A: # curr = set([num]) # for prev_num in prev: # curr.add(prev_num | num) # result |= curr # prev = 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, arr: List[int]) -> int: ans = set() current = {0} for x in arr: # print(x,current) current = {x | y for y in current} | {x} # print(current) ans |= current # print(ans) return len(ans) # brute force time complexity :O(n^2) # space complexity:O(n) if not arr:return 0 l ,ans = len(arr) ,set() for i in range(l): res = arr[i] ans.add(res) for j in range(i+1,l): res |= arr[j] ans.add(res) 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: res, prev = set(), set() res.add(a[0]) prev.add(a[0]) for i in range(1, len(a)): cur = set() cur.add(a[i]) res.add(a[i]) for j in prev: cur.add(j|a[i]) res.add(j|a[i]) prev = 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): final = set() actual = set() for el in A: actual = {el} | {el2 | el for el2 in actual} final |= actual return len(final)
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: mp={0:1} ans={} for a in A: temp={} for m in mp: temp[m|a]=1 temp[a]=1 for t in temp: ans[t]=1 mp={} for t in temp: mp[t]=1 cnt=0 for c in ans: cnt+=1 return cnt
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 = set() results = set() for num in A: results = {j | num for j in results} results.add(num) result_set.update(results) return len(result_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
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = set() prev = set() for a in A: cur = set(a | b for b in prev) cur.add(a) prev = cur 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: # 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() outcomes = set() for elt in A: 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: # 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() outcomes = set() for elt in A: 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: # 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:]: temp = {elt} | {elt | val for val in outcomes} outcomes = temp 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: cur,res = set(),set() for i in A: cur = { i|j for j in cur} | {i} res |= cur return len(res) # nums, n = set(), len(A) # for i in range(n): # num = A[i] # for j in range(i, n): # num |= A[j] # nums.add(num) # return len(nums)
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 ... 736 ms ... 100 % ... 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: res = set() cur = set() for x in A: cur = {x} | {x | y for y in cur} 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: # Frontier Set # Time complexity: O(NlogW), where N is the length of A, and W is the maximum size of elements in A. # Space complexity: O(NlogW) 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} # 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 subarrayBitwiseORs(self, A: List[int]) -> int: self.res = set() def helper(idx): if idx < 0: return set() if idx==0: self.res |= {A[idx]} return {A[idx]} val = {A[idx]}|{x|A[idx] for x in helper(idx-1)} self.res |= val return val helper(len(A)-1) return len(self.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: result = set() curr = set() for num in A: curr = {num | prev_num for prev_num in curr} curr.add(num) result.update(curr) return len(result) # result = set() # prev = set([0]) # for num in A: # curr = set([num]) # for prev_num in prev: # curr.add(prev_num | num) # result |= curr # prev = 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: my_set = set(A) curr = 0 prev = set() prev.add(A[0]) for num in A[1:]: temp = set() #curr |= num #my_set.add(curr) for p in prev: temp.add(num | p) my_set.add(num | p) #print(temp) prev = temp prev.add(num) return len(my_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
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: # Frontier Set # Time complexity: O(NlogW) = O(30N), where N is the length of A, and W is the maximum size of elements in A. # Space complexity: O(NlogW) 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, arr: List[int]) -> int: s = set() prev = set() prev.add(arr[0]) s.add(arr[0]) for i in range(len(arr)): temp = set() for val in prev: temp.add(val|arr[i]) s.add(val|arr[i]) prev = temp prev.add(arr[i]) s.add(arr[i]) return len(s)
A string S of lowercase letters is given.  Then, we may make any number of moves. In each move, we choose one of the first K letters (starting from the left), remove it, and place it at the end of the string. Return the lexicographically smallest string we could have after any number of moves.   Example 1: Input: S = "cba", K = 1 Output: "acb" Explanation: In the first move, we move the 1st character ("c") to the end, obtaining the string "bac". In the second move, we move the 1st character ("b") to the end, obtaining the final result "acb". Example 2: Input: S = "baaca", K = 3 Output: "aaabc" Explanation: In the first move, we move the 1st character ("b") to the end, obtaining the string "aacab". In the second move, we move the 3rd character ("c") to the end, obtaining the final result "aaabc".   Note: 1 <= K <= S.length <= 1000 S consists of lowercase letters only.
class Solution: def orderlyQueue(self, S: str, K: int) -> str: if K >= 2: return ''.join(sorted(S)) length = len(S) S = S + S i, j, k = 0, 1, 0 while j + k < len(S) and k < length: if S[i + k] == S[j + k]: k += 1 continue elif S[i + k] < S[j + k]: j = j + k + 1 else: i = max(i + k + 1, j) j = i + 1 k = 0 return S[i : i + length]
A string S of lowercase letters is given.  Then, we may make any number of moves. In each move, we choose one of the first K letters (starting from the left), remove it, and place it at the end of the string. Return the lexicographically smallest string we could have after any number of moves.   Example 1: Input: S = "cba", K = 1 Output: "acb" Explanation: In the first move, we move the 1st character ("c") to the end, obtaining the string "bac". In the second move, we move the 1st character ("b") to the end, obtaining the final result "acb". Example 2: Input: S = "baaca", K = 3 Output: "aaabc" Explanation: In the first move, we move the 1st character ("b") to the end, obtaining the string "aacab". In the second move, we move the 3rd character ("c") to the end, obtaining the final result "aaabc".   Note: 1 <= K <= S.length <= 1000 S consists of lowercase letters only.
class Solution: def orderlyQueue(self, S: str, K: int) -> str: if K==1: tmp = S for i in range(len(S)): S = S[1:] + str(S[0]) if S<tmp: tmp = S return tmp else: return ''.join(sorted(S))
A string S of lowercase letters is given.  Then, we may make any number of moves. In each move, we choose one of the first K letters (starting from the left), remove it, and place it at the end of the string. Return the lexicographically smallest string we could have after any number of moves.   Example 1: Input: S = "cba", K = 1 Output: "acb" Explanation: In the first move, we move the 1st character ("c") to the end, obtaining the string "bac". In the second move, we move the 1st character ("b") to the end, obtaining the final result "acb". Example 2: Input: S = "baaca", K = 3 Output: "aaabc" Explanation: In the first move, we move the 1st character ("b") to the end, obtaining the string "aacab". In the second move, we move the 3rd character ("c") to the end, obtaining the final result "aaabc".   Note: 1 <= K <= S.length <= 1000 S consists of lowercase letters only.
class Solution: def orderlyQueue(self, s: str, k: int) -> str: s = [c for c in s] if k == 1: temp = [] for i in range(len(s)): temp_s = s[i:] + s[:i] temp.append(''.join(temp_s)) temp.sort() return temp[0] else: return ''.join(sorted(s)) # if k>=2 then you can basically swap adjacent elements, so you can do bubble sort as swapping of adjcaent elements is possible. so the ans will be the sorted string if k>=2.
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: ans, count, seen = 0, 0, {} for i, hour in enumerate(hours): count = count + 1 if hour > 8 else count - 1 if count > 0: ans = i + 1 else: if count not in seen: seen[count] = i if count - 1 in seen: ans = max(ans, i - seen[count - 1]) return ans
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: ans, cum, seen = 0, 0, {} for i, hour in enumerate(hours): cum = cum + 1 if hour > 8 else cum - 1 if cum > 0: ans = i + 1 else: if cum not in seen: seen[cum] = i if cum - 1 in seen: ans = max(ans, i - seen[cum - 1]) return ans
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def findslavedays(self,hours): a=0 b=0 slave=0 nonslave=0 slavedays=0 while b<len(hours): # print(a,b,slave,nonslave,slavedays) if slave>nonslave: slavedays=max(slavedays,b-a+slave-nonslave-1) if a==b: if hours[a]<=8: nonslave+=1 else: slave+=1 b+=1 continue if slave<nonslave: if hours[a]<=8: nonslave-=1 else: slave-=1 a+=1 elif slave>=nonslave: if hours[b]<=8: nonslave+=1 else: slave+=1 b+=1 if slave>nonslave: slavedays=max(slavedays,b-a+slave-nonslave-1) print(a,b) while a>0: a-=1 if hours[a]<=8: nonslave+=1 else: slave+=1 if slave>nonslave: slavedays=max(slavedays,b-a+slave-nonslave-1) if slave>nonslave: slavedays=max(slavedays,b-a+slave-nonslave-1) return slavedays def longestWPI(self, hours: List[int]) -> int: isslave=[-1]*len(hours) sumslave=0 for i in range(len(isslave)): if hours[i]>8: isslave[i]=1 dic={} maxslavedays=0 print(isslave) for i in range(len(isslave)): sumslave=isslave[i]+sumslave # print(sumslave) if sumslave>0: maxslavedays=max(maxslavedays,i+1) if sumslave not in dic: dic[sumslave]=i if sumslave-1 in dic: maxslavedays=max(maxslavedays,i-dic[sumslave-1]) print(dic) return maxslavedays print(isslave,sumslave) a=-1 b=0 dic={} dic[0]=-1 for i in range(len(isslave)): if sumslave[i]>0: b=i maxslavedays=max(maxslavedays,b-a) if sumslave[i]<=0: a=i return maxslavedays #for i in range(1,len(isslave)): return 0 slavelength1=self.findslavedays(hours) slavelength2=self.findslavedays(hours[::-1]) return max(slavelength1,slavelength2)
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: score=0 dic={} result=0 for i,hour in enumerate(hours): if hour >8: score +=1 else: score -=1 if score>0: result = i+1 if score not in dic: dic[score] = i if score-1 in dic: result=max(result, i -dic[score-1]) return result
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: res = score = 0 seen = {} for i in range(len(hours)): h = hours[i] score = score + 1 if h > 8 else score - 1 if score > 0: res = i + 1 seen.setdefault(score, i) if score - 1 in seen: res = max(res, i - seen[score - 1]) return res
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
import bisect class Solution: def longestWPI(self, hours: List[int]) -> int: r, l, l_idx = [], [], [] d = [] for i, x in enumerate(hours): if i == 0: if x > 8: d.append(1) else: d.append(-1) else: if x > 8: d.append(d[-1] + 1) else: d.append(d[-1] - 1) for i, x in enumerate(d): while len(r) > 0 and r[-1][0] <= x: r.pop() r.append((x, i)) if len(l) == 0 or x < l[-1]: l.append(x) l_idx.append(i) ans = 0 l.reverse() l_idx.reverse() for x in r: if x[0] > 0: ans = max(ans, x[1] + 1) else: idx = bisect.bisect_right(l, x[0] - 1) if idx >= 1: ans = max(ans, x[1] - l_idx[idx - 1]) return ans
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: nums = [] for hour in hours: if hour > 8: nums.append(1) else: nums.append(-1) prevsum, sumv = [], 0 for num in nums: sumv += num prevsum.append(sumv) stack = [] prevsum = [0] + prevsum for i, val in enumerate(prevsum): if not stack or prevsum[stack[-1]] > val: stack.append(i) i, res = len(prevsum) - 1, 0 while i >= 0: while stack and prevsum[stack[-1]] < prevsum[i]: res = max(res, i - stack[-1]) stack.pop() i -= 1 return res
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: d = {} d[0] = -1 res = 0 for i in range(len(hours)): hours[i] = (1 if hours[i] > 8 else -1) + (0 if i == 0 else hours[i-1]) if hours[i] not in d: d[hours[i]] = i if hours[i] > 0: res = i+1 target = hours[i] - 1 if target in d: res = max(res, i - d[target]) print(hours) print(d) return res
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: hours = [1 if i > 8 else -1 for i in hours] hsh = {} vals = [0 for i in hours] sm = 0 for i in range(len(hours)-1, -1, -1): sm += hours[i] if sm > 0: vals[i] = max(vals[i], len(hours) - i) hours[i] = sm smo = hours[i] - 1 if smo in hsh: v = hsh[smo] vals[i] = max(vals[i], v - i + vals[v]) if hours[i] not in hsh: hsh[hours[i]] = i return max(vals)
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: d = {} P = [0] #prefix sum F = [0 for i in range(len(hours))] res = 0 for i in range(len(hours)): P.append(P[-1] + (1 if hours[i] > 8 else -1)) if P[-1] > 0: F[i] = i + 1 else: s = d.get(P[-1] - 1, -1) if s >= 0: F[i] = max(F[i], i - s + F[s]) if P[-1] not in d: d[P[-1]] = i res = max(res, F[i]) return res
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: val = 0 result = 0 table = dict() for i in range(len(hours)): hour = hours[i] if hour > 8: val += 1 else: val -= 1 if val > 0: result = i + 1 else: if val not in table: table[val] = i if val - 1 in table: dist = i - table[val - 1] if dist > result: result = dist return result
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: sum=0 ans=0 d={} for i in range(len(hours)): sum=sum+1 if hours[i] >8 else sum-1 if sum>0: ans=i+1 else: if sum not in d: d[sum]=i if sum-1 in d: ans=max(ans,i-d[sum-1]) return ans
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: seen = {} result,score = 0,0 for i,num in enumerate(hours): score += 1 if num > 8 else -1 if score > 0: result = i+1 elif score -1 in seen: result = max(result, i - seen[score -1]) seen.setdefault(score, i) return result
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: n = len(hours) hashmap = dict() total=0 res=0 for i in range(n): if hours[i]<=8: total-=1 else: total+=1 if total>0: res=i+1 continue else: if total not in hashmap: hashmap[total] = i if (total-1) in hashmap: res = max(res, i-hashmap[total-1]) return res
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: # Convert array to 0 and 1s bits = [1 if x > 8 else -1 for x in hours] # Problem becomes: find the longest interval bits[i:j] where sum(bits[i:j]) > 0 # Get prefix sum of bits pref = [0] * (len(bits)+1) for i in range(1, len(bits)+1): pref[i] = pref[i-1] + bits[i-1] # Problem becomes: find the longest interval pref[i:j] where pref[j] > pref[i] # Sort index of pref by value. Problem becomes: find the largest sorted_pref[j]-sorted_pref[i] where j > i # Greedy to solve the new problem ans = 0 min_val = len(bits) + 1 for y in sorted(enumerate(pref), key=lambda x: (x[1], -x[0])): min_val = min(min_val, y[0]) ans = max(ans, y[0] - min_val) return ans
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: res = score = 0 seen = {} for i, h in enumerate(hours): score = score + 1 if h > 8 else score - 1 if score > 0: res = i + 1 seen.setdefault(score, i) if score - 1 in seen: res = max(res, i - seen[score - 1]) return res
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: s = 0 seen = {} ans = 0 for i,h in enumerate(hours): if h > 8: s += 1 else: s -= 1 if s > 0: ans = i+1 seen.setdefault(s, i) if s-1 in seen: ans = max(ans, i-seen[s-1]) return ans
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours): res = score = 0 seen = {} for i, h in enumerate(hours): print((i,h)) if h > 8: score += 1 else: score -= 1 if score > 0: res = i + 1 seen.setdefault(score, i) if score - 1 in seen: res = max(res, i - seen[score - 1]) return res #tiring_count = 1, not_tiring_count = 0, max_length = 1 #tiring_count = 2, not_tiring_count = 0, max_length = 2 #tiring_count = 2, not_tiring_count = 1, max_length = 3 #tiring_count = 2, not_tiring_count = 2, tiring_count = 0 #tiring_count = 0, nt = 3 #nt = 4 #tiring count += 1
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: hours = [1 if i > 8 else -1 for i in hours] hsh = {hours[0]:0} for i in range(1, len(hours)): hours[i] += hours[i-1] hsh[hours[i]] = i vals = [i+1 if hours[i] > 0 else 0 for i in range(len(hours))] for i in range(len(hours)): sm = hours[i] + 1 if sm in hsh: v = hsh[sm] vals[v] = max(vals[v], v-i+vals[i]) return max(vals)
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: final = 0 score = 0 stack = {} for i in range(len(hours)): if hours[i] > 8 : score += 1 else: score -= 1 stack.setdefault(score,i) if score > 0: final = i + 1 if score - 1 in stack: final = max(final, i - stack[score-1]) return final
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: L = [1 if i > 8 else -1 for i in hours] K = [1 if i > 8 else -1 for i in hours] mp = {} for i in range(0, len(hours)): if i > 0: s = L[i] = L[i] + L[i - 1] else: s = L[i] if s not in mp: mp[s] = {i: True} else: mp[s][i] = True #print(K) #print(L) result = 0 for i, c in enumerate(L): if L[-1] > L[i]: result = max(result, len(K) - i - 1) #print(\"ss\", i, result) if 0 < L[i]: result = max(result, i + 1) #print(\"ddd\", i) if i > 0 and L[i - 1] + 1 in mp: s = mp[L[i - 1] + 1] #print(result, s, i) result = max(result, max(s) - i + 1) #result = max(result, i + 1 - min(s)) if L[i] - 1 in mp: s = mp[L[i] - 1] #print(result, s, i) # result = max(result, max(s) - i + 1) result = max(result, i - min(s)) #print(result, i) return result def longestWPI(self, hours: List[int]) -> int: mp = {} accu = 0 result = 0 for i, v in enumerate(hours): accu += 1 if v > 8 else -1 # print(accu) if accu > 0: result = max(result, i + 1) print((result, i)) if accu - 1 in mp: result = max(result, i - mp[accu-1]) print((result, i)) if accu not in mp: mp[accu] = i return result
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: ret = 0 overall = 0 totals = {} for i, h in enumerate(hours): if h > 8: overall += 1 else: overall -= 1 if overall > 0: ret = i + 1 else: if overall-1 in totals: length = i - totals[overall-1] ret = max(ret, length) if overall not in totals: totals[overall] = i return ret
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: pre_sum = [0] for i, v in enumerate(hours): hours[i] = 1 if v > 8 else -1 pre_sum.append(pre_sum[-1] + hours[i]) dec_stack = [] # idx to pre_sum for i, v in enumerate(pre_sum): if len(dec_stack) == 0 or (len(dec_stack) > 0 and v < pre_sum[dec_stack[-1]]): dec_stack.append(i) max_r = -math.inf max_width = 0 for r in range(len(pre_sum)-1, -1, -1): n = pre_sum[r] if n < max_r: continue max_r = n while len(dec_stack) > 0: # pop top < n left_idx = dec_stack[-1] if pre_sum[left_idx] < n: dec_stack = dec_stack[0:-1] max_width = max(max_width, r - left_idx) else: break if len(dec_stack) == 0: break return max_width
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: hours = [1 if h > 8 else -1 for h in hours] down = {1: 0} s = 0 ans = 0 for i,h in enumerate(hours): if h < 0: if s not in down: down[s] = i s += h if s in down: ans = max(ans, i - down[s]) if s > 0: ans = max(ans, i+1) return ans
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: days = [1 if hour > 8 else -1 for hour in hours] #find longest strictly positive subarr prefSums = days[:] for i in range(1, len(hours)): prefSums[i] = days[i] + prefSums[i - 1] result = 0 prefixLookup = {} for i in range(len(hours)): sumToHere = prefSums[i] #longest answer ending here if sumToHere >= 1: result = max(result, i + 1) else: earlierPrefSumNeeded = sumToHere - 1 if earlierPrefSumNeeded in prefixLookup: result = max(result, i - prefixLookup[earlierPrefSumNeeded]) if sumToHere not in prefixLookup: prefixLookup[sumToHere] = i return result
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: lt = [] for hour in hours: if hour > 8: lt.append(1) else: lt.append(-1) seen = {} s = 0 mx = 0 for idx, ele in enumerate(lt): s += ele print((s, idx, mx)) if s > 0 : #atleast more 9 mx = max(mx, (idx+1)) else: if (s-1) in seen: i = seen[s-1] mx = max(mx, idx-i) if (s) not in seen: seen[s] = idx return mx
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
# [9,9,6,0,6,6,9] # [1,1,-1,-1,-1,1] # 1 2 1 0 -1 0 # [1,1,-1,-1,-1,1, 1] class Solution: def longestWPI(self, hours: List[int]) -> int: prefix = [] for hour in hours: if hour > 8: prefix.append(1) else: prefix.append(-1) curr_sum = 0 seen = {0:-1} res = 0 for i, num in enumerate(prefix): curr_sum += num if curr_sum > 0: res = i + 1 if curr_sum - 1 in seen: res = max(res, i - seen[curr_sum - 1]) if curr_sum not in seen: seen[curr_sum] = i return res
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: cur = res = 0 seen = {} for i, h in enumerate(hours): cur += 1 if h > 8 else -1 if cur > 0: res = max(res, i + 1) else: if cur - 1 in seen: res = max(res, i - seen[cur - 1]) if cur not in seen: seen[cur] = i return res
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: n = len(hours) hours = [1 if x > 8 else -1 for x in hours] s = 0 idx = {} ans = 0 for i in range(len(hours)): s += hours[i] if s > 0: ans = i + 1 if s not in idx: idx[s] = i if (s - 1) in idx: ans = max(ans, i - idx[s - 1]) return ans
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
#https://leetcode.com/problems/longest-well-performing-interval/discuss/334565/JavaC%2B%2BPython-O(N)-Solution-Life-needs-996-and-669 class Solution: def longestWPI(self, hours: List[int]) -> int: seen = dict() cnt = 0 res = 0 for idx, h in enumerate(hours): if h>8: cnt += 1 else: cnt -= 1 if cnt>0: res = idx+1 else: if cnt-1 in seen: res = max(res, idx-seen[cnt-1]) if cnt not in seen: seen[cnt] = idx return res
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: Sum, ans = 0, 0 cache = {} for i, n in enumerate(hours): Sum = Sum + 1 if n > 8 else Sum - 1 if Sum > 0: ans = i + 1 if Sum-1 in cache: ans = max(ans, i - cache[Sum-1]) cache.setdefault(Sum, i) return ans
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: hours = [1 if h > 8 else -1 for h in hours] seen = {} ans = cur = 0 for i, h in enumerate(hours): cur += h seen.setdefault(cur, i) if cur > 0: ans = i + 1 elif cur - 1 in seen: ans = max(ans, i - seen[cur - 1]) return ans # hours = [1 if h > 8 else 0 for h in hours] # acc = [0] + list(accumulate(hours)) # i, j = 0, len(hours) - 1 # cur = sum(hours) # ans = 0 # while i <= j: # if cur > (j - i + 1) // 2: # return j - i + 1
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, hours: List[int]) -> int: res = score = 0 seen = {} for i, h in enumerate(hours): score = score + 1 if h > 8 else score - 1 if score > 0: res = i + 1 seen.setdefault(score, i) if score - 1 in seen: res = max(res, i - seen[score - 1]) return res
We are given hours, a list of the number of hours worked per day for a given employee. A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval.   Example 1: Input: hours = [9,9,6,0,6,6,9] Output: 3 Explanation: The longest well-performing interval is [9,9,6].   Constraints: 1 <= hours.length <= 10000 0 <= hours[i] <= 16
class Solution: def longestWPI(self, h: List[int]) -> int: n=len(h) for i in range(n): if h[i]>8: h[i]=1 else: h[i]=-1 #longest subarray with sum >0 pre=[0]*n pre[0]=h[0] maxi=0 for i in range(1,n): pre[i]=pre[i-1]+h[i] d={} print(pre) for x in range(n): if pre[x]>0: maxi=x+1 if d.get(pre[x]-1,-1)!=-1: #print(i,d[pre[x]]) maxi=max(maxi,x-d[pre[x]-1]) if d.get(pre[x],-1)==-1: d[pre[x]]=x return maxi