Skip to content

Commit

Permalink
add: 分割数组的最大值
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 21, 2024
1 parent 19604eb commit c722320
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 数组/队列/集合/映射

- [分割数组的最大值](src/array/split_array_largest_sum.rs) [贪心, 数组, 二分查找, 动态规划, 前缀和]

- LeetCode 410. 分割数组的最大值 <https://leetcode.cn/problems/split-array-largest-sum>

- [按分隔符拆分字符串](src/array/split_strings_by_separator.rs) [数组, 字符串]

- LeetCode 2788. 按分隔符拆分字符串 <https://leetcode.cn/problems/split-strings-by-separator>
Expand Down
Binary file added images/array/split_array_largest_sum.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
38 changes: 38 additions & 0 deletions src/array/split_array_largest_sum.rs
Original file line number Diff line number Diff line change
@@ -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<i32>, 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<i32>, 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
}
}
1 change: 1 addition & 0 deletions tests/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
29 changes: 29 additions & 0 deletions tests/array/split_array_largest_sum_test.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit c722320

Please sign in to comment.