diff options
Diffstat (limited to 'bitwise_or_subarray.py')
-rw-r--r-- | bitwise_or_subarray.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/bitwise_or_subarray.py b/bitwise_or_subarray.py new file mode 100644 index 0000000..9c15024 --- /dev/null +++ b/bitwise_or_subarray.py @@ -0,0 +1,29 @@ +# 898. Bitwise ORs of Subarrays + +""" +given an integer array 'arr', return the number of distinct bitwise or's of +all the non-empty subarrays of 'arr'. the bitwise or of a subarray is the +bitwise or of each interger in the subarray. the bitwise or of a subarray of +one integer is that integer. a subarray is a contiguous non-empty sequence of +elements within an array. +""" + + +class Solution(object): + def subarrayBitwiseORs(self, arr): + """ + :type arr: List[int] + :rtype: int + """ + ans, curr = set(), set() + for i in arr: + curr = {i | j for j in curr} | {i} + ans |= curr + return len(ans) + + +if __name__ == "__main__": + obj = Solution() + print(obj.subarrayBitwiseORs(arr=[0])) + print(obj.subarrayBitwiseORs(arr=[1, 1, 2])) + print(obj.subarrayBitwiseORs(arr=[1, 2, 4])) |