Skip to content

Commit c4dba6c

Browse files
committed
Time: 116 ms (25.36%), Space: 56.7 MB (44.98%) - LeetHub
1 parent a798478 commit c4dba6c

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
class Solution {
22
public int minimizedMaximum(int n, int[] quantities) {
3-
int maxi = Integer.MIN_VALUE;
3+
4+
int maxi = Integer.MIN_VALUE;
45
for(int quantity : quantities){
5-
maxi = Math.max(quantity , maxi) ;
6+
maxi = Math.max(quantity,maxi);
67
}
78

8-
int ans=0;
9-
int l=1 , h = maxi;
10-
while(l<=h){
11-
int mid = l+(h-l)/2;
12-
if(isPossible(n,mid,quantities)){
13-
ans = mid;
14-
h= mid-1;
9+
//Edge Case ::
10+
if(n == quantities.length){
11+
return maxi ;
12+
}
13+
14+
int start = 1 ;
15+
int end = maxi ;
16+
int ans = Integer.MAX_VALUE;
17+
while(start <= end ){
18+
int mid = start +(end - start )/2;
19+
if(isPossible(mid,quantities,n)){
20+
ans = Math.min(mid , ans);
21+
end = mid-1;
1522
}else{
16-
l= mid+1;
23+
start = mid+1;
1724
}
1825
}
1926
return ans ;
2027
}
2128

22-
public boolean isPossible(int n , int target , int[] quantities){
23-
int cnt = 0;
24-
for(int ele : quantities){
25-
if(ele%target == 0){
26-
cnt += ele/target;
27-
}else{
28-
cnt += ele/target +1;
29-
}
29+
public static boolean isPossible(int mid , int[] quantities,int n){
30+
int cnt=0;
31+
for(int quantity : quantities){
32+
cnt += Math.ceil(quantity*1.0/mid);
33+
3034
}
31-
return cnt <= n ;
35+
return cnt<=n;
3236
}
3337
}

0 commit comments

Comments
 (0)