881. Boats to Save People
881. Boats to Save People
Description
Solution
Greedy, because every boat is required to save most 2 people, so we could greedily let every boat loads 2 people. But a tricky point is, intuitively we should let each boat takes as heavier as possible, but actually, if we could putpeople[-1] with people[1]
instead of people[-1] with people[0]
, we still could let people[-2] with people[1]
, so just let first and last together and we will get the correct answer.
Code
1 | class Solution { |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.