3022. Minimize OR of Remaining Elements Using Operations
Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 29% Topics: Array, Greedy, Bit Manipulation
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(nlogr)
# Space: O(1)
# bitmasks, greedy
class Solution(object):
def minOrAfterOperations(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
result = 0
l = max(nums).bit_length()
mask = (1<<l)-1
for i in reversed(xrange(l)):
result <<= 1
curr, cnt = mask, 0
for x in nums:
curr &= x>>i
if curr&~result:
cnt += 1
else:
curr = mask
if cnt > k:
result += 1
return result
Solution from kamyu104/LeetCode-Solutions · MIT