diff --git a/README.md b/README.md index 4669bf6..284237e 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8 ### 数组/队列/集合/映射 +- [分割数组的最大值](src/array/split_array_largest_sum.rs) [贪心, 数组, 二分查找, 动态规划, 前缀和] + + - LeetCode 410. 分割数组的最大值 + - [按分隔符拆分字符串](src/array/split_strings_by_separator.rs) [数组, 字符串] - LeetCode 2788. 按分隔符拆分字符串 diff --git a/images/array/split_array_largest_sum.jpeg b/images/array/split_array_largest_sum.jpeg new file mode 100644 index 0000000..3d5588d Binary files /dev/null and b/images/array/split_array_largest_sum.jpeg differ diff --git a/src/array/mod.rs b/src/array/mod.rs index e74e1cc..6267f81 100644 --- a/src/array/mod.rs +++ b/src/array/mod.rs @@ -57,3 +57,4 @@ pub mod number_of_boomerangs; pub mod count_common_words_with_one_occurrence; pub mod find_maximum_number_of_string_pairs; pub mod split_strings_by_separator; +pub mod split_array_largest_sum; diff --git a/src/array/split_array_largest_sum.rs b/src/array/split_array_largest_sum.rs new file mode 100644 index 0000000..9542de3 --- /dev/null +++ b/src/array/split_array_largest_sum.rs @@ -0,0 +1,38 @@ +// 分割数组的最大值 +// https://leetcode.cn/problems/split-array-largest-sum +// INLINE ../../images/array/split_array_largest_sum.jpeg + +pub struct Solution; + +impl Solution { + fn check(nums: &Vec, k: i32, x: i32) -> bool { + let mut sum = 0; + let mut cnt = 1; + for num in nums { + if sum + num > x { + cnt += 1; + sum = *num; + } else { + sum += num; + } + } + cnt <= k + } + pub fn split_array(nums: Vec, k: i32) -> i32 { + let mut left = 0; + let mut right = 0; + for num in &nums { + left = left.max(*num); + right += num; + } + while left < right { + let mid = left + (right - left) / 2; + if Solution::check(&nums, k, mid) { + right = mid; + } else { + left = mid + 1; + } + } + left + } +} diff --git a/tests/array/mod.rs b/tests/array/mod.rs index dd2df7c..353ca76 100644 --- a/tests/array/mod.rs +++ b/tests/array/mod.rs @@ -57,3 +57,4 @@ pub mod two_sum_test; pub mod zero_matrix_lcci_test; pub mod find_maximum_number_of_string_pairs_test; pub mod split_strings_by_separator_test; +pub mod split_array_largest_sum_test; diff --git a/tests/array/split_array_largest_sum_test.rs b/tests/array/split_array_largest_sum_test.rs new file mode 100644 index 0000000..9abcef9 --- /dev/null +++ b/tests/array/split_array_largest_sum_test.rs @@ -0,0 +1,29 @@ +use rust_practice::array::split_array_largest_sum::Solution; + +#[test] +fn split_array_test() { + // 示例 1: + // 输入:nums = [7,2,5,10,8], k = 2 + // 输出:18 + // 解释: + // 一共有四种方法将 nums 分割为 2 个子数组。 + // 其中最好的方式是将其分为 [7,2,5] 和 [10,8] 。 + // 因为此时这两个子数组各自的和的最大值为18,在所有情况中最小。 + let nums = vec![7, 2, 5, 10, 8]; + let k = 2; + assert_eq!(Solution::split_array(nums, k), 18); + + // 示例 2: + // 输入:nums = [1,2,3,4,5], k = 2 + // 输出:9 + let nums = vec![1, 2, 3, 4, 5]; + let k = 2; + assert_eq!(Solution::split_array(nums, k), 9); + + // 示例 3: + // 输入:nums = [1,4,4], k = 3 + // 输出:4 + let nums = vec![1, 4, 4]; + let k = 3; + assert_eq!(Solution::split_array(nums, k), 4); +}