|
| 1 | +/** |
| 2 | + * [2270] Number of Ways to Split Array |
| 3 | + * |
| 4 | + * You are given a 0-indexed integer array nums of length n. |
| 5 | + * nums contains a valid split at index i if the following are true: |
| 6 | + * |
| 7 | + * The sum of the first i + 1 elements is greater than or equal to the sum of the last n - i - 1 elements. |
| 8 | + * There is at least one element to the right of i. That is, 0 <= i < n - 1. |
| 9 | + * |
| 10 | + * Return the number of valid splits in nums. |
| 11 | + * |
| 12 | + * Example 1: |
| 13 | + * |
| 14 | + * Input: nums = [10,4,-8,7] |
| 15 | + * Output: 2 |
| 16 | + * Explanation: |
| 17 | + * There are three ways of splitting nums into two non-empty parts: |
| 18 | + * - Split nums at index 0. Then, the first part is [10], and its sum is 10. The second part is [4,-8,7], and its sum is 3. Since 10 >= 3, i = 0 is a valid split. |
| 19 | + * - Split nums at index 1. Then, the first part is [10,4], and its sum is 14. The second part is [-8,7], and its sum is -1. Since 14 >= -1, i = 1 is a valid split. |
| 20 | + * - Split nums at index 2. Then, the first part is [10,4,-8], and its sum is 6. The second part is [7], and its sum is 7. Since 6 < 7, i = 2 is not a valid split. |
| 21 | + * Thus, the number of valid splits in nums is 2. |
| 22 | + * |
| 23 | + * Example 2: |
| 24 | + * |
| 25 | + * Input: nums = [2,3,1,0] |
| 26 | + * Output: 2 |
| 27 | + * Explanation: |
| 28 | + * There are two valid splits in nums: |
| 29 | + * - Split nums at index 1. Then, the first part is [2,3], and its sum is 5. The second part is [1,0], and its sum is 1. Since 5 >= 1, i = 1 is a valid split. |
| 30 | + * - Split nums at index 2. Then, the first part is [2,3,1], and its sum is 6. The second part is [0], and its sum is 0. Since 6 >= 0, i = 2 is a valid split. |
| 31 | + * |
| 32 | + * |
| 33 | + * Constraints: |
| 34 | + * |
| 35 | + * 2 <= nums.length <= 10^5 |
| 36 | + * -10^5 <= nums[i] <= 10^5 |
| 37 | + * |
| 38 | + */ |
| 39 | +pub struct Solution {} |
| 40 | + |
| 41 | +// problem: https://leetcode.com/problems/number-of-ways-to-split-array/ |
| 42 | +// discuss: https://leetcode.com/problems/number-of-ways-to-split-array/discuss/?currentPage=1&orderBy=most_votes&query= |
| 43 | + |
| 44 | +// submission codes start here |
| 45 | + |
| 46 | +impl Solution { |
| 47 | + pub fn ways_to_split_array(nums: Vec<i32>) -> i32 { |
| 48 | + 0 |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// submission codes end |
| 53 | + |
| 54 | +#[cfg(test)] |
| 55 | +mod tests { |
| 56 | + use super::*; |
| 57 | + |
| 58 | + #[test] |
| 59 | + #[ignore] |
| 60 | + fn test_2270_example_1() { |
| 61 | + let nums = vec![10, 4, -8, 7]; |
| 62 | + |
| 63 | + let result = 2; |
| 64 | + |
| 65 | + assert_eq!(Solution::ways_to_split_array(nums), result); |
| 66 | + } |
| 67 | + |
| 68 | + #[test] |
| 69 | + #[ignore] |
| 70 | + fn test_2270_example_2() { |
| 71 | + let nums = vec![2, 3, 1, 0]; |
| 72 | + |
| 73 | + let result = 2; |
| 74 | + |
| 75 | + assert_eq!(Solution::ways_to_split_array(nums), result); |
| 76 | + } |
| 77 | +} |
0 commit comments