返回介绍

solution / 2800-2899 / 2871.Split Array Into Maximum Number of Subarrays / README_EN

发布于 2024-06-17 01:02:59 字数 4611 浏览 0 评论 0 收藏 0

2871. Split Array Into Maximum Number of Subarrays

中文文档

Description

You are given an array nums consisting of non-negative integers.

We define the score of subarray nums[l..r] such that l <= r as nums[l] AND nums[l + 1] AND ... AND nums[r] where AND is the bitwise AND operation.

Consider splitting the array into one or more subarrays such that the following conditions are satisfied:

  • Each element of the array belongs to exactly one subarray.
  • The sum of scores of the subarrays is the minimum possible.

Return _the maximum number of subarrays in a split that satisfies the conditions above._

A subarray is a contiguous part of an array.

 

Example 1:

Input: nums = [1,0,2,0,1,2]
Output: 3
Explanation: We can split the array into the following subarrays:
- [1,0]. The score of this subarray is 1 AND 0 = 0.
- [2,0]. The score of this subarray is 2 AND 0 = 0.
- [1,2]. The score of this subarray is 1 AND 2 = 0.
The sum of scores is 0 + 0 + 0 = 0, which is the minimum possible score that we can obtain.
It can be shown that we cannot split the array into more than 3 subarrays with a total score of 0. So we return 3.

Example 2:

Input: nums = [5,7,1,3]
Output: 1
Explanation: We can split the array into one subarray: [5,7,1,3] with a score of 1, which is the minimum possible score that we can obtain.
It can be shown that we cannot split the array into more than 1 subarray with a total score of 1. So we return 1.

 

Constraints:

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

Solutions

Solution 1: Greedy + Bitwise Operation

We initialize a variable $score$ to record the score of the current subarray, and set $score=-1$ initially. Then we traverse the array, for each element $num$, we perform a bitwise AND operation between $score$ and $num$, and assign the result to $score$. If $score=0$, it means the score of the current subarray is 0, so we can split the current subarray and reset $score$ to $-1$. Finally, we return the number of split subarrays.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

class Solution:
  def maxSubarrays(self, nums: List[int]) -> int:
    score, ans = -1, 1
    for num in nums:
      score &= num
      if score == 0:
        score = -1
        ans += 1
    return 1 if ans == 1 else ans - 1
class Solution {
  public int maxSubarrays(int[] nums) {
    int score = -1;
    int ans = 1;
    for (int num : nums) {
      score &= num;
      if (score == 0) {
        ans++;
        score = -1;
      }
    }
    return ans == 1 ? 1 : ans - 1;
  }
}
class Solution {
public:
  int maxSubarrays(vector<int>& nums) {
    int score = -1, ans = 1;
    for (int num : nums) {
      score &= num;
      if (score == 0) {
        --score;
        ++ans;
      }
    }
    return ans == 1 ? 1 : ans - 1;
  }
};
func maxSubarrays(nums []int) int {
  ans, score := 1, -1
  for _, num := range nums {
    score &= num
    if score == 0 {
      score--
      ans++
    }
  }
  if ans == 1 {
    return 1
  }
  return ans - 1
}
function maxSubarrays(nums: number[]): number {
  let [ans, score] = [1, -1];
  for (const num of nums) {
    score &= num;
    if (score === 0) {
      --score;
      ++ans;
    }
  }
  return ans == 1 ? 1 : ans - 1;
}

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

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

发布评论

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