返回介绍

solution / 2400-2499 / 2411.Smallest Subarrays With Maximum Bitwise OR / README_EN

发布于 2024-06-17 01:03:06 字数 5648 浏览 0 评论 0 收藏 0

2411. Smallest Subarrays With Maximum Bitwise OR

中文文档

Description

You are given a 0-indexed array nums of length n, consisting of non-negative integers. For each index i from 0 to n - 1, you must determine the size of the minimum sized non-empty subarray of nums starting at i (inclusive) that has the maximum possible bitwise OR.

  • In other words, let Bij be the bitwise OR of the subarray nums[i...j]. You need to find the smallest subarray starting at i, such that bitwise OR of this subarray is equal to max(Bik) where i <= k <= n - 1.

The bitwise OR of an array is the bitwise OR of all the numbers in it.

Return _an integer array _answer_ of size _n_ where _answer[i]_ is the length of the minimum sized subarray starting at _i_ with maximum bitwise OR._

A subarray is a contiguous non-empty sequence of elements within an array.

 

Example 1:

Input: nums = [1,0,2,1,3]
Output: [3,3,2,2,1]
Explanation:
The maximum possible bitwise OR starting at any index is 3. 
- Starting at index 0, the shortest subarray that yields it is [1,0,2].
- Starting at index 1, the shortest subarray that yields the maximum bitwise OR is [0,2,1].
- Starting at index 2, the shortest subarray that yields the maximum bitwise OR is [2,1].
- Starting at index 3, the shortest subarray that yields the maximum bitwise OR is [1,3].
- Starting at index 4, the shortest subarray that yields the maximum bitwise OR is [3].
Therefore, we return [3,3,2,2,1]. 

Example 2:

Input: nums = [1,2]
Output: [2,1]
Explanation:
Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2.
Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1.
Therefore, we return [2,1].

 

Constraints:

  • n == nums.length
  • 1 <= n <= 105
  • 0 <= nums[i] <= 109

Solutions

Solution 1: Reverse Traversal

To find the shortest subarray starting at position $i$ that maximizes the bitwise OR operation, we need to maximize the number of $1$s in the result.

We use an array $f$ of size $32$ to record the earliest position of each bit $1$.

We traverse the array $nums[i]$ in reverse order. For the $j$-th bit of $nums[i]$, if it is $1$, then $f[j]$ is $i$. Otherwise, if $f[j]$ is not $-1$, it means that a number satisfying the $j$-th bit as $1$ is found on the right, so we update the length.

The time complexity is $O(n \times \log m)$, where $n$ is the length of the array $nums$, and $m$ is the maximum value in the array $nums$.

class Solution:
  def smallestSubarrays(self, nums: List[int]) -> List[int]:
    n = len(nums)
    ans = [1] * n
    f = [-1] * 32
    for i in range(n - 1, -1, -1):
      t = 1
      for j in range(32):
        if (nums[i] >> j) & 1:
          f[j] = i
        elif f[j] != -1:
          t = max(t, f[j] - i + 1)
      ans[i] = t
    return ans
class Solution {
  public int[] smallestSubarrays(int[] nums) {
    int n = nums.length;
    int[] ans = new int[n];
    int[] f = new int[32];
    Arrays.fill(f, -1);
    for (int i = n - 1; i >= 0; --i) {
      int t = 1;
      for (int j = 0; j < 32; ++j) {
        if (((nums[i] >> j) & 1) == 1) {
          f[j] = i;
        } else if (f[j] != -1) {
          t = Math.max(t, f[j] - i + 1);
        }
      }
      ans[i] = t;
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> smallestSubarrays(vector<int>& nums) {
    int n = nums.size();
    vector<int> f(32, -1);
    vector<int> ans(n);
    for (int i = n - 1; ~i; --i) {
      int t = 1;
      for (int j = 0; j < 32; ++j) {
        if ((nums[i] >> j) & 1) {
          f[j] = i;
        } else if (f[j] != -1) {
          t = max(t, f[j] - i + 1);
        }
      }
      ans[i] = t;
    }
    return ans;
  }
};
func smallestSubarrays(nums []int) []int {
  n := len(nums)
  f := make([]int, 32)
  for i := range f {
    f[i] = -1
  }
  ans := make([]int, n)
  for i := n - 1; i >= 0; i-- {
    t := 1
    for j := 0; j < 32; j++ {
      if ((nums[i] >> j) & 1) == 1 {
        f[j] = i
      } else if f[j] != -1 {
        t = max(t, f[j]-i+1)
      }
    }
    ans[i] = t
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文