| 22 | } |
| 23 | |
| 24 | public int splitArray(int[] nums, int m) { |
| 25 | int start=Integer.MIN_VALUE,end=0; |
| 26 | int n=nums.length; |
| 27 | // calculate min and max of the range |
| 28 | for(int i=0;i<n;i++) |
| 29 | { |
| 30 | start=Math.max(start,nums[i]); |
| 31 | end+=nums[i]; |
| 32 | } |
| 33 | int result=0; |
| 34 | // apply binary search |
| 35 | int mid=0; |
| 36 | while(start<=end) |
| 37 | { |
| 38 | mid=(start+end)/2; |
| 39 | if(isCorrect(mid,nums,m,n)) |
| 40 | { |
| 41 | // store as result and check if we can further minimize it |
| 42 | result=mid; |
| 43 | end=mid-1; |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | start=mid+1; |
| 48 | } |
| 49 | } |
| 50 | return result; |
| 51 | } |
| 52 | } |