|
| 1 | +/** |
| 2 | + * [1567] Maximum Length of Subarray With Positive Product |
| 3 | + * |
| 4 | + * Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. |
| 5 | + * A subarray of an array is a consecutive sequence of zero or more values taken out of that array. |
| 6 | + * Return the maximum length of a subarray with positive product. |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * |
| 10 | + * Input: nums = [1,-2,-3,4] |
| 11 | + * Output: 4 |
| 12 | + * Explanation: The array nums already has a positive product of 24. |
| 13 | + * |
| 14 | + * Example 2: |
| 15 | + * |
| 16 | + * Input: nums = [0,1,-2,-3,-4] |
| 17 | + * Output: 3 |
| 18 | + * Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. |
| 19 | + * Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. |
| 20 | + * Example 3: |
| 21 | + * |
| 22 | + * Input: nums = [-1,-2,-3,0,1] |
| 23 | + * Output: 2 |
| 24 | + * Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. |
| 25 | + * |
| 26 | + * |
| 27 | + * Constraints: |
| 28 | + * |
| 29 | + * 1 <= nums.length <= 10^5 |
| 30 | + * -10^9 <= nums[i] <= 10^9 |
| 31 | + * |
| 32 | + */ |
| 33 | +pub struct Solution {} |
| 34 | + |
| 35 | +// problem: https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/ |
| 36 | +// discuss: https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/?currentPage=1&orderBy=most_votes&query= |
| 37 | + |
| 38 | +// submission codes start here |
| 39 | + |
| 40 | +impl Solution { |
| 41 | + pub fn get_max_len(nums: Vec<i32>) -> i32 { |
| 42 | + let n = nums.len(); |
| 43 | + if n == 0 { |
| 44 | + return 0; |
| 45 | + } |
| 46 | + |
| 47 | + let mut pos = if nums[0] > 0 { 1 } else { 0 }; |
| 48 | + let mut result = pos; |
| 49 | + |
| 50 | + let mut neg = if nums[0] < 0 { 1 } else { 0 }; |
| 51 | + |
| 52 | + for i in 1..n { |
| 53 | + let curr = nums[i]; |
| 54 | + if curr == 0 { |
| 55 | + pos = 0; |
| 56 | + neg = 0; |
| 57 | + } else { |
| 58 | + let next_neg = if neg > 0 { neg + 1 } else { 0 }; |
| 59 | + let next_pos = pos + 1; |
| 60 | + pos = if curr < 0 { next_neg } else { next_pos }; |
| 61 | + neg = if curr < 0 { next_pos } else { next_neg }; |
| 62 | + } |
| 63 | + result = std::cmp::max(result, pos); |
| 64 | + } |
| 65 | + |
| 66 | + result |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// submission codes end |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use super::*; |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_1567_example_1() { |
| 78 | + let nums = vec![1, -2, -3, 4]; |
| 79 | + |
| 80 | + let result = 4; |
| 81 | + |
| 82 | + assert_eq!(Solution::get_max_len(nums), result); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_1567_example_2() { |
| 87 | + let nums = vec![0, 1, -2, -3, -4]; |
| 88 | + |
| 89 | + let result = 3; |
| 90 | + |
| 91 | + assert_eq!(Solution::get_max_len(nums), result); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_1567_example_3() { |
| 96 | + let nums = vec![-1, -2, -3, 0, 1]; |
| 97 | + |
| 98 | + let result = 2; |
| 99 | + |
| 100 | + assert_eq!(Solution::get_max_len(nums), result); |
| 101 | + } |
| 102 | +} |
0 commit comments