空调r 410 r语言strsplit函数 什么意思

LeetCode No.410 Split Array Largest Sum - 黄健伟的博客 - CSDN博客
LeetCode No.410 Split Array Largest Sum
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.
Given&m&satisfies the following constraint: 1 ≤ m ≤ length(nums) ≤ 14,000.
nums = [7,2,5,10,8]
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.
====================================================================================================================================
此题目的大意是:将给定数组切割成m份,求这m份中最大和的最小值(即求极大的极小问题)。
因为我们知道切割的方法,用二分+搜索求解就可以了。
所谓二分,就是确定结果的最大值maxerf和最小值miner,取两者的中间值mid。而搜索便是:判断mid是否符合条件,如果符合,则令miner=mid+1,同时记录下ans,否则,令maxer=mid-1,以此类推,当miner&maxer时结束。
方法不难,但是我们还要注意一些细节上的问题:
(1)nums = [1,] ,m = 1 时,计算maxer的时候会越界,而且计算繁杂,应单独处理
(2)nums = [1,] ,m = 2 时,计算maxer的时候会越界,得到的maxer为负数,这时候,要令maxer =&。
(3)在搜索的时候,当当前和temp==mid时,要判断此时是否为数组的结尾,再确定sum是否加1。
class Solution {
int splitArray(vector&int&& nums, int m) {
int n = nums.size() ;
if ( n == 0 )
return 0 ;
int miner = 0 , maxer = 0 ;
for ( int i = 0 ; i & i ++ )
maxer += nums[i] ;
miner = max ( miner , nums[i] ) ;
if ( m == 1 )
if ( maxer & 0 )
miner = max ( miner , maxer / m ) ;
while ( miner &= maxer )
int mid = ( (unsigned int) miner + maxer ) / 2 , temp = 0 , sum = 1 ;
for ( int i = 0 ; i & i ++ )
temp += nums[i] ;
if ( temp & mid )
temp = nums[i] ;
else if ( temp == mid )
if ( i != n - 1 )
temp = 0 ;
if ( sum &= m )
maxer = mid - 1 ;
miner = mid + 1 ;
我的热门文章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.
Given&m&satisfies the following constraint: 1 ≤ m ≤ length(nums) ≤ 14,000.
nums = [7,2,5,10,8]
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.
dp[i][j]表示nums数组从index=0到index=i,切j刀最小的和,dp[i][j]=min(max(dp[i][k],sums[i]-sums[k])),其中j-1&=k&=i-1,
然而二维数组开不下。
public class Solution {
public static int splitArray(int[] nums, int m)
int len=nums.
int[] sums=new int[len];
int sum=0;
for(int i=0;i&i++)
sum+=nums[i];
int[][] dp=new int[len][len];
for(int i=0;i&i++)
dp[i][0]=sums[i];
for(int i=0;i&i++)
for(int j=1;j&j++)
int min=Integer.MAX_VALUE;
for(int k=i-1;k&=j-1;k--)
min=Math.min(min, Math.max(dp[k][j-1], sums[i]-sums[k]));
return dp[len-1][m-1];
----------------------------------------------------------------------
二分搜索法,摘自
/topic/61324/clear-explanation-8ms-binary-search-java/9
The answer is between maximum value of input array numbers and sum of those numbers.Use binary search to approach the correct answer. We have&l = r
= sum of all Every time we do&mid = (l + r) / 2;Use greedy to narrow down left and right boundaries in binary search.
3.1 Cut the array from left.
3.2 Try our best to make sure that the sum of numbers between each two cuts (inclusive) is large enough but still less thanmid.
3.3 We'll end up with two results: either we can divide the array into more than m subarrays or we cannot.
If we can, it means that the&mid&value we pick is too small because we've already tried our best to make sure each
part holds as many non-negative numbers as we can but we still have numbers left. So, it is impossible to cut the array into m parts and make sure each parts is no larger than&mid.
We should increase m. This leads to&l = mid + 1;
If we can't, it is either we successfully divide the array into m parts and the sum of each part is less than&mid,
or we used up all numbers before we reach m. Both of them mean that we should lower&mid&because we need to find the minimum one. This
leads to&r = mid - 1;
public class Solution {
public int splitArray(int[] nums, int m) {
int max = 0; long sum = 0;
for (int num : nums) {
max = Math.max(num, max);
sum +=
if (m == 1) return (int)
long l = long r =
while (l &= r) {
long mid = (l + r)/ 2;
if (valid(mid, nums, m)) {
r = mid - 1;
l = mid + 1;
return (int)l;
public boolean valid(long target, int[] nums, int m) {
int count = 1;
long total = 0;
for(int num : nums) {
total +=
if (total & target) {
count++;
if (count & m) {
return false;
return true;
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:30659次
积分:2711
积分:2711
排名:第13638名
原创:246篇
转载:71篇
(2)(1)(50)(1)(1)(125)(122)(10)(5)厂家直供出口27000B...商品大图
请注意:本图片来自正浩商贸有限公司提供的厂家直供出口27000BTU挂壁式空调splittype3HPKF-72GW产品,图片仅供参考,厂家直供出口27000BTU挂壁式空调splittype3HPKF-72GW产品会因为批次的不同可能与本图片不一致,请以收到的实物为准。
相关商品推荐
& 500.00/台
& 130.00/袋
壁挂式空调相关推荐
您在慧聪网上采购商品属于商业贸易行为。以上所展示的信息由卖家自行提供,内容的真实性、准确性和合法性由发布卖家负责,请意识到互联网交易中的风险是客观存在的。推荐使用,保障您的交易安全!
所在地区:辽宁省&沈阳市
咨询商家:张豪杰
查看联系方式
让慧聪网撮合专家为您解决采购难题
您采购的产品:
请输入采购产品
您的手机号码:
请输入手机号码
*采购产品:
请输入采购产品
*采购数量:
请输入采购数量
*采购截止日期:
请输入正确的手机号码
请输入验证码
*短信验证码:
<input id="valid_Code1" maxlength="6" placeholder="请输入验证码" name="VALIDCODE" class="codeInput" onkeyup="this.value=this.value.replace(/\D/g,'')" onkeypress="if(event.keyCode
57) event.returnValue =" type="text">
免费获取验证码
为了安全,请输入验证码,我们将优先处理您的需求!
请输入验证码
发送成功!
慧聪已收到您的需求,我们会尽快通知卖家联系您,同时会派出采购专员1对1为您提供服务,请您耐心等待!
联系人:张豪杰&
公司名称:正浩商贸有限公司
请输入正确的手机号码
请输入验证码
*短信验证码:
免费获取验证码
为了安全,请输入验证码,我们将优先处理您的需求!
请输入验证码
优质商品推荐

我要回帖

更多关于 r split函数 的文章

 

随机推荐