Bin Packing
InterviewTime limit1sMemory limit128 MB
Pack items into identical bins holding at most two items each so that the number of bins is minimized.
- Level
Medium5 of 10
- Topics
- Greedy, Two pointers, Sorting, Array
- Solved
- No attempts yet
Problem
You must pack one-dimensional items into identical bins. Every bin has the same length , and each item has length .
Find the minimum number of bins such that all of the following hold:
- Each bin contains at most 2 items.
- Every item is packed into exactly one bin.
- The total length of the items in any single bin does not exceed .
Given the integers , , and , compute the minimum number of bins .
Input
The first line contains the number of items ().
The second line contains the bin length ().
Each of the next lines contains one item length ().
Output
Print a single line containing the minimum number of bins needed to pack all items.
The figure below shows one optimal packing.
