|
| 1 | +## 1. Sorting + Two Pointers |
| 2 | + |
| 3 | +::tabs-start |
| 4 | + |
| 5 | +```python |
| 6 | +class Solution: |
| 7 | + def numRescueBoats(self, people: List[int], limit: int) -> int: |
| 8 | + people.sort() |
| 9 | + res, l, r = 0, 0, len(people) - 1 |
| 10 | + while l <= r: |
| 11 | + remain = limit - people[r] |
| 12 | + r -= 1 |
| 13 | + res += 1 |
| 14 | + if l <= r and remain >= people[l]: |
| 15 | + l += 1 |
| 16 | + return res |
| 17 | +``` |
| 18 | + |
| 19 | +```java |
| 20 | +public class Solution { |
| 21 | + public int numRescueBoats(int[] people, int limit) { |
| 22 | + Arrays.sort(people); |
| 23 | + int res = 0, l = 0, r = people.length - 1; |
| 24 | + while (l <= r) { |
| 25 | + int remain = limit - people[r--]; |
| 26 | + res++; |
| 27 | + if (l <= r && remain >= people[l]) { |
| 28 | + l++; |
| 29 | + } |
| 30 | + } |
| 31 | + return res; |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +```cpp |
| 37 | +class Solution { |
| 38 | +public: |
| 39 | + int numRescueBoats(vector<int>& people, int limit) { |
| 40 | + sort(people.begin(), people.end()); |
| 41 | + int res = 0, l = 0, r = people.size() - 1; |
| 42 | + while (l <= r) { |
| 43 | + int remain = limit - people[r--]; |
| 44 | + res++; |
| 45 | + if (l <= r && remain >= people[l]) { |
| 46 | + l++; |
| 47 | + } |
| 48 | + } |
| 49 | + return res; |
| 50 | + } |
| 51 | +}; |
| 52 | +``` |
| 53 | +
|
| 54 | +```javascript |
| 55 | +class Solution { |
| 56 | + /** |
| 57 | + * @param {number[]} people |
| 58 | + * @param {number} limit |
| 59 | + * @return {number} |
| 60 | + */ |
| 61 | + numRescueBoats(people, limit) { |
| 62 | + people.sort((a, b) => a - b); |
| 63 | + let res = 0, l = 0, r = people.length - 1; |
| 64 | + while (l <= r) { |
| 65 | + let remain = limit - people[r--]; |
| 66 | + res++; |
| 67 | + if (l <= r && remain >= people[l]) { |
| 68 | + l++; |
| 69 | + } |
| 70 | + } |
| 71 | + return res; |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +::tabs-end |
| 77 | + |
| 78 | +### Time & Space Complexity |
| 79 | + |
| 80 | +* Time complexity: $O(n \log n)$ |
| 81 | +* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm. |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +## 2. Counting Sort |
| 86 | + |
| 87 | +::tabs-start |
| 88 | + |
| 89 | +```python |
| 90 | +class Solution: |
| 91 | + def numRescueBoats(self, people: List[int], limit: int) -> int: |
| 92 | + m = max(people) |
| 93 | + count = [0] * (m + 1) |
| 94 | + for p in people: |
| 95 | + count[p] += 1 |
| 96 | + |
| 97 | + idx, i = 0, 1 |
| 98 | + while idx < len(people): |
| 99 | + while count[i] == 0: |
| 100 | + i += 1 |
| 101 | + people[idx] = i |
| 102 | + count[i] -= 1 |
| 103 | + idx += 1 |
| 104 | + |
| 105 | + res, l, r = 0, 0, len(people) - 1 |
| 106 | + while l <= r: |
| 107 | + remain = limit - people[r] |
| 108 | + r -= 1 |
| 109 | + res += 1 |
| 110 | + if l <= r and remain >= people[l]: |
| 111 | + l += 1 |
| 112 | + return res |
| 113 | +``` |
| 114 | + |
| 115 | +```java |
| 116 | +public class Solution { |
| 117 | + public int numRescueBoats(int[] people, int limit) { |
| 118 | + int m = Arrays.stream(people).max().getAsInt(); |
| 119 | + int[] count = new int[m + 1]; |
| 120 | + for (int p : people) { |
| 121 | + count[p]++; |
| 122 | + } |
| 123 | + |
| 124 | + int idx = 0, i = 1; |
| 125 | + while (idx < people.length) { |
| 126 | + while (count[i] == 0) { |
| 127 | + i++; |
| 128 | + } |
| 129 | + people[idx++] = i; |
| 130 | + count[i]--; |
| 131 | + } |
| 132 | + |
| 133 | + int res = 0, l = 0, r = people.length - 1; |
| 134 | + while (l <= r) { |
| 135 | + int remain = limit - people[r--]; |
| 136 | + res++; |
| 137 | + if (l <= r && remain >= people[l]) { |
| 138 | + l++; |
| 139 | + } |
| 140 | + } |
| 141 | + return res; |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +```cpp |
| 147 | +class Solution { |
| 148 | +public: |
| 149 | + int numRescueBoats(vector<int>& people, int limit) { |
| 150 | + int m = *max_element(people.begin(), people.end()); |
| 151 | + vector<int> count(m + 1, 0); |
| 152 | + for (int p : people) { |
| 153 | + count[p]++; |
| 154 | + } |
| 155 | + |
| 156 | + int idx = 0, i = 1; |
| 157 | + while (idx < people.size()) { |
| 158 | + while (count[i] == 0) { |
| 159 | + i++; |
| 160 | + } |
| 161 | + people[idx++] = i; |
| 162 | + count[i]--; |
| 163 | + } |
| 164 | + |
| 165 | + int res = 0, l = 0, r = people.size() - 1; |
| 166 | + while (l <= r) { |
| 167 | + int remain = limit - people[r--]; |
| 168 | + res++; |
| 169 | + if (l <= r && remain >= people[l]) { |
| 170 | + l++; |
| 171 | + } |
| 172 | + } |
| 173 | + return res; |
| 174 | + } |
| 175 | +}; |
| 176 | +``` |
| 177 | +
|
| 178 | +```javascript |
| 179 | +class Solution { |
| 180 | + /** |
| 181 | + * @param {number[]} people |
| 182 | + * @param {number} limit |
| 183 | + * @return {number} |
| 184 | + */ |
| 185 | + numRescueBoats(people, limit) { |
| 186 | + const m = Math.max(...people); |
| 187 | + const count = new Array(m + 1).fill(0); |
| 188 | + for (const p of people) { |
| 189 | + count[p]++; |
| 190 | + } |
| 191 | + |
| 192 | + let idx = 0, i = 1; |
| 193 | + while (idx < people.length) { |
| 194 | + while (count[i] === 0) { |
| 195 | + i++; |
| 196 | + } |
| 197 | + people[idx++] = i; |
| 198 | + count[i]--; |
| 199 | + } |
| 200 | + |
| 201 | + let res = 0, l = 0, r = people.length - 1; |
| 202 | + while (l <= r) { |
| 203 | + const remain = limit - people[r--]; |
| 204 | + res++; |
| 205 | + if (l <= r && remain >= people[l]) { |
| 206 | + l++; |
| 207 | + } |
| 208 | + } |
| 209 | + return res; |
| 210 | + } |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +::tabs-end |
| 215 | + |
| 216 | +### Time & Space Complexity |
| 217 | + |
| 218 | +* Time complexity: $O(n)$ |
| 219 | +* Space complexity: $O(m)$ |
| 220 | + |
| 221 | +> Where $n$ is the size of the input array and $m$ is the maximum value in the array. |
0 commit comments