-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |