-
Notifications
You must be signed in to change notification settings - Fork 2
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
5 changed files
with
73 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 分割数组的最大值 | ||
// https://leetcode.cn/problems/split-array-largest-sum | ||
// INLINE ../../images/array/split_array_largest_sum.jpeg | ||
|
||
#include <headers.hpp> | ||
|
||
class Solution { | ||
public: | ||
int split(vector<int> &nums, int max) { | ||
int count = 1; | ||
int sum = 0; | ||
for (int num : nums) { | ||
if (sum + num > max) { | ||
count++; | ||
sum = num; | ||
} else { | ||
sum += num; | ||
} | ||
} | ||
return count; | ||
} | ||
int splitArray(vector<int> &nums, int k) { | ||
int left = 0, right = 0; | ||
for (int num : nums) { | ||
left = max(left, num); | ||
right += num; | ||
} | ||
while (left < right) { | ||
int mid = left + (right - left) / 2; | ||
int count = split(nums, mid); | ||
if (count <= k) { | ||
right = mid; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <array/split_array_largest_sum.cpp> | ||
|
||
TEST(分割数组的最大值, splitArray) { | ||
Solution solution; | ||
// 示例 1: | ||
// 输入:nums = [7,2,5,10,8], k = 2 | ||
// 输出:18 | ||
// 解释: | ||
// 一共有四种方法将 nums 分割为 2 个子数组。 | ||
// 其中最好的方式是将其分为 [7,2,5] 和 [10,8] 。 | ||
// 因为此时这两个子数组各自的和的最大值为18,在所有情况中最小。 | ||
vector<int> nums = {7, 2, 5, 10, 8}; | ||
int k = 2; | ||
EXPECT_EQ(solution.splitArray(nums, k), 18); | ||
|
||
// 示例 2: | ||
// 输入:nums = [1,2,3,4,5], k = 2 | ||
// 输出:9 | ||
vector<int> nums2 = {1, 2, 3, 4, 5}; | ||
int k2 = 2; | ||
EXPECT_EQ(solution.splitArray(nums2, k2), 9); | ||
|
||
// 示例 3: | ||
// 输入:nums = [1,4,4], k = 3 | ||
// 输出:4 | ||
vector<int> nums3 = {1, 4, 4}; | ||
int k3 = 3; | ||
EXPECT_EQ(solution.splitArray(nums3, k3), 4); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// 执行编译时间:2024-01-20 23:28:50 | ||
// 执行编译时间:2024-01-21 14:50:51 | ||
#include <gtest/gtest.h> | ||
#include <lib.hpp> | ||
|
||
|