410. Split Array Largest Sum

本文介绍了一种使用二分查找法解决数组分割问题的算法。给定一个非负整数数组及一个整数m,目标是将数组分割成m个连续的子数组,并使这些子数组的和的最大值达到最小。通过确定可能的有效值范围并逐步缩小范围,找到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays.

Note:
If n is the length of array, assume the following constraints are satisfied:

  • 1 ≤ n ≤ 1000
  • 1 ≤ m ≤ min(50, n)

Examples:

Input:
nums = [7,2,5,10,8]
m = 2

Output:
18

Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.

Subscribe to see which companies asked this question.


将给定的序列分成m个子序列,使得各个序列的总和的最大值最小,求出这个最小的最大值。看了discuss才知道怎样用二分法做,答案一定是在Max(序列的最大值)和sum(序列总和)之间,在这个范围内进行二分搜索。对于当前的值d,如果序列能分成m个和小于等于d的序列,则表示当前值是“有效的”,可以进一步减少来寻找最终答案;如果不能,即分成多于m个和小于等于d的序列,则当前值比答案小,增大之寻找最终答案。最后缩到一个值,判断这个值是否“有效”,“有效”的话答案是这个值,否则是这个值加1.


代码:

class Solution 
{
public:
	int splitArray(vector<int>& nums, int m) 
	{
		int sum = 0, Max = 0;
		for(auto num:nums)
		{
			sum += num;
			Max = max(Max, num);
		}
		int l = Max, r = sum;
		while(l < r)
		{
			int mid = l + (r - l) / 2;
			bool b = isvalid(nums, m, mid);
			if(b)
			{
				r = mid - 1;
			}
			else
			{
				l = mid + 1;
			}
		}
		return isvalid(nums, m, l) ? l : l+1;
	}
private:
	bool isvalid(vector<int>& nums, int m, int d)
	{
		int sum = 0;
		for(auto num:nums)
		{
			if(sum + num > d)
			{
				--m;
				sum = 0;
			}
			if(m == 0) return false;
			sum += num;
		}
		return true;
	}
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值