背包容量第一题二分做出来了 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; }