filmov
tv
Leetcode 2411. Smallest Subarrays With Maximum Bitwise OR - bit position check solution

Показать описание
See other videos @codeyao9796
Python solution to Leetcode 2411 - Smallest Subarrays With Maximum Bitwise OR. Assume the largest number in the array contain m bits (in this problem 30 is enough). To maximize bitwise OR result starting an index i toward the right, for each bit position, we could try to locate the nearest right index j for indices i, i + 1, .., n-1, such nums[j] has 1-bit in the corresponding bit position. (If no such index exists for the bit position, we set j = i, meaning that the maximal bitwise OR is 0 in this bit position.)
Then max(j) - i + 1 is the desired length of smallest subarray starting from i and maximizes bitwise OR result.
When we do the implementation, we could start from right towards left when we iterate the nums array. This is an important point to digest.
Python solution to Leetcode 2411 - Smallest Subarrays With Maximum Bitwise OR. Assume the largest number in the array contain m bits (in this problem 30 is enough). To maximize bitwise OR result starting an index i toward the right, for each bit position, we could try to locate the nearest right index j for indices i, i + 1, .., n-1, such nums[j] has 1-bit in the corresponding bit position. (If no such index exists for the bit position, we set j = i, meaning that the maximal bitwise OR is 0 in this bit position.)
Then max(j) - i + 1 is the desired length of smallest subarray starting from i and maximizes bitwise OR result.
When we do the implementation, we could start from right towards left when we iterate the nums array. This is an important point to digest.