I like to read this solution as a small machine: keep the useful information, throw away the noise. For 881. Boats to Save People, the solution in this repository is mainly a two pointers solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers, sliding window, greedy.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are numRescueBoats.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(nlogn), space: O(n)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 104 ms, faster than 94.88% of C++ online submissions for Boats to Save People.
02//Memory Usage: 13.3 MB, less than 100.00% of C++ online submissions for Boats to Save People.
03
04class Solution {
05public:
06 int numRescueBoats(vector<int>& people, int limit) {
07 sort(people.begin(), people.end());
08 int N = people.size();
09 // for(int i = 0; i < N; i++){
10 // cout << people[i] << " ";
11 // }
12 // cout << endl;
13 //the boundaries, exclusive
14 int left = -1, right = N;
15 int boats = 0;
16
17 //the range to be explored is (left, right) = (left+1, right-1)
18 //, and there should be at least one empty position in the range so we need right - left > 1
19 while(left+1 < N && right-1 >= 0 && right - left > 1){
20 int cur = 0;
21 cur += people[--right];
22 if(right-1 >= 0 && right - left > 1 && cur + people[right-1] <= limit){
23 cur += people[--right];
24 }else if(left+1 < N && right - left > 1 && cur + people[left+1] <= limit){
25 cur += people[++left];
26 }
27 boats++;
28 // cout << "(" << left << ", " << right << ") " << cur << " " << boats << endl;
29 }
30
31 return boats;
32 }
33};
34
35//Approach 1: Greedy (Two Pointer)
36//Runtime: 108 ms, faster than 92.83% of C++ online submissions for Boats to Save People.
37//Memory Usage: 13 MB, less than 100.00% of C++ online submissions for Boats to Save People.
38//time: O(nlogn), space: O(n)
39class Solution {
40public:
41 int numRescueBoats(vector<int>& people, int limit) {
42 int N = people.size();
43 int i = 0, j = N-1;
44 int ans = 0;
45
46 sort(people.begin(), people.end());
47 // for(int k = 0; k < N; k++){
48 // cout << people[k] << " ";
49 // }
50 // cout << endl;
51
52 while(i <= j){
53 ans++;
54 //when i equals to j, people[i] may be added twice
55 //, but that doesn't affect the ans
56
57 if(people[i] + people[j] <= limit){
58 i++;
59 }
60 j--;
61 // cout << "[" << i << ", " << j << "]" << endl;
62 }
63
64 return ans;
65 }
66};
Cost