1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public int numRescueBoats(int[] people, int limit) { int boats = 0; Arrays.sort(people);
int light = 0, heavy = people.length - 1; while (light <= heavy) { if (people[light] + people[heavy] <= limit) { light++; heavy--; boats++; } else { heavy--; boats++; } }
return boats; } }
|
先让瘦子上船能够便于解题。如果先让胖子上船,那么需要找第二胖的胖子,并不便于解题。
References
881. Boats to Save People