2022-09-08 17:29
门头沟学院 Java 0 点赞 评论 收藏
分享
2021-10-11 18:08
门头沟学院 Java 0 点赞 评论 收藏
分享
2021-09-22 16:34
门头沟学院 Java 花椒茴香:背包容量第一题二分做出来了
public long Solve(int n, int m, int[] weights) {
// write code here
int max = -1;
long sum = 0;
for (int i = 0; i < weights.length; i++) {
if(weights[i]>max){
max = weights[i];
}
sum += weights[i];
}
long start = Math.max(max,sum/m);
long end = sum;
long mid = (start+end)/2;
while(start<end){
mid = (start+end)/2;
if(canHave(m,n,weights,mid)){
end = mid;
}else{
start = mid + 1;mid = mid + 1;
}}
return mid;
}
private boolean canHave(int m,int n,int[] weights,long k){
int leftNum = m-1;long leftWeight = k;
for (int j = 0; j < weights.length; j++) {
if(leftWeight>= weights[j]){
leftWeight -= weights[j];
}else{
leftNum--;leftWeight = k;leftWeight -= weights[j];
}
}
if(leftNum>=0){return true;}
return false;
}
投递虾皮信息等公司10个岗位 >
0 点赞 评论 收藏
分享
2021-09-13 22:15
门头沟学院 Java 0 点赞 评论 收藏
分享
投递网易等公司10个岗位 >
0 点赞 评论 收藏
分享
2021-09-06 16:37
门头沟学院 Java 0 点赞 评论 收藏
分享
关注他的用户也关注了: