File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 2020int: the minimum number of boxes that need to be unloaded
2121*/
2222
23+ //brute force
24+ public int getMinimumBoxes (int [] boxes , int capacity )
25+ {
26+ //Base Check
27+ if (boxes == null )
28+ {
29+ return 0 ;
30+ }
31+
32+ //Init
33+ int value ;
34+
35+ //Compute
36+ /** sort the given array */
37+ Arrays .sort (boxes );
38+
39+ /** find the maximum number of boxes to be loaded */
40+ int longest = Integer .MIN_VALUE ;
41+ for (int i = 0 ; i < boxes .length - 1 ; i ++)
42+ {
43+ for (int j = i ; j < boxes .length ; j ++)
44+ {
45+ //current pair (min, max)
46+ int localLen = Integer .MIN_VALUE ;
47+ if (boxes [j ] <= capacity * boxes [i ])
48+ {
49+ localLen = j - i + 1 ;
50+ }
51+
52+ //Update longest
53+ longest = Math .max (longest , localLen );
54+
55+ }// for
56+
57+ }// for
58+
59+ /** update value */
60+ value = boxes .length - longest ;
61+
62+ //Return
63+ return value ;
64+ }
You can’t perform that action at this time.
0 commit comments