2871. Split Array Into Maximum Number of Subarrays Medium

@problem@discussion
#Array#Greedy#Bit Manipulation



1/**
2 * [2871] Split Array Into Maximum Number of Subarrays
3 *
4 * You are given an array nums consisting of non-negative integers.
5 * 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.
6 * Consider splitting the array into one or more subarrays such that the following conditions are satisfied:
7 * 
8 * 	Each element of the array belongs to exactly one subarray.
9 * 	The sum of scores of the subarrays is the minimum possible.
10 * 
11 * Return the maximum number of subarrays in a split that satisfies the conditions above.
12 * A subarray is a contiguous part of an array.
13 *  
14 * <strong class="example">Example 1:
15 * 
16 * Input: nums = [1,0,2,0,1,2]
17 * Output: 3
18 * Explanation: We can split the array into the following subarrays:
19 * - [1,0]. The score of this subarray is 1 AND 0 = 0.
20 * - [2,0]. The score of this subarray is 2 AND 0 = 0.
21 * - [1,2]. The score of this subarray is 1 AND 2 = 0.
22 * The sum of scores is 0 + 0 + 0 = 0, which is the minimum possible score that we can obtain.
23 * 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.
24 * 
25 * <strong class="example">Example 2:
26 * 
27 * Input: nums = [5,7,1,3]
28 * Output: 1
29 * 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.
30 * 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.
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	1 <= nums.length <= 10^5
36 * 	0 <= nums[i] <= 10^6
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/split-array-into-maximum-number-of-subarrays/
42// discuss: https://leetcode.com/problems/split-array-into-maximum-number-of-subarrays/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn max_subarrays(nums: Vec<i32>) -> i32 {
48        0
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_2871() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.