Problem Statement |
|||||||||||||
Fox Ciel has some items. The weight of the i-th (0-based) item is item[i].
She wants to put all items into bins. The capacity of each bin is 300. She can put an arbitrary number of items into a single bin, but the total weight of items in a bin must be less than or equal to 300. You are given the int[] item. It is known that the weight of each item is between 101 and 300, inclusive. Return the minimal number of bins required to store all items. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- | item will contain between 1 and 50 elements, inclusive. | ||||||||||||
- | Each element of item will be between 101 and 300, inclusive. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
and My Code is blow
public static int minBins2(int[] items){
Arrays.sort(items);
int sp = 0;
int ep = Math.max(items.length -1, 0);
int totalNum =0;
while(true){
if(items[ep] > 199){
totalNum ++;
ep--;
}else{
break;
}
}
while(ep >= sp){
if(items[ep] + items[sp] <= 300){
sp++;
}
ep--;
totalNum++;
}
return totalNum;
}
## Access Step ##
1. Sort Items
2. if items are bigger than 199 add totalNum
3. sum of first item and last item are bigger than 300
last index -- and totalnum ++
.......
i think this is very easy