Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Sri Hari: Batch-4/Neetcode-All/Added-articles #3777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
647 changes: 647 additions & 0 deletions articles/4sum.md

Large diffs are not rendered by default.

499 changes: 499 additions & 0 deletions articles/arithmetic-slices-ii-subsequence.md

Large diffs are not rendered by default.

221 changes: 221 additions & 0 deletions articles/boats-to-save-people.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
## 1. Sorting + Two Pointers

::tabs-start

```python
class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
res, l, r = 0, 0, len(people) - 1
while l <= r:
remain = limit - people[r]
r -= 1
res += 1
if l <= r and remain >= people[l]:
l += 1
return res
```

```java
public class Solution {
public int numRescueBoats(int[] people, int limit) {
Arrays.sort(people);
int res = 0, l = 0, r = people.length - 1;
while (l <= r) {
int remain = limit - people[r--];
res++;
if (l <= r && remain >= people[l]) {
l++;
}
}
return res;
}
}
```

```cpp
class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
sort(people.begin(), people.end());
int res = 0, l = 0, r = people.size() - 1;
while (l <= r) {
int remain = limit - people[r--];
res++;
if (l <= r && remain >= people[l]) {
l++;
}
}
return res;
}
};
```

```javascript
class Solution {
/**
* @param {number[]} people
* @param {number} limit
* @return {number}
*/
numRescueBoats(people, limit) {
people.sort((a, b) => a - b);
let res = 0, l = 0, r = people.length - 1;
while (l <= r) {
let remain = limit - people[r--];
res++;
if (l <= r && remain >= people[l]) {
l++;
}
}
return res;
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(n \log n)$
* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm.

---

## 2. Counting Sort

::tabs-start

```python
class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
m = max(people)
count = [0] * (m + 1)
for p in people:
count[p] += 1

idx, i = 0, 1
while idx < len(people):
while count[i] == 0:
i += 1
people[idx] = i
count[i] -= 1
idx += 1

res, l, r = 0, 0, len(people) - 1
while l <= r:
remain = limit - people[r]
r -= 1
res += 1
if l <= r and remain >= people[l]:
l += 1
return res
```

```java
public class Solution {
public int numRescueBoats(int[] people, int limit) {
int m = Arrays.stream(people).max().getAsInt();
int[] count = new int[m + 1];
for (int p : people) {
count[p]++;
}

int idx = 0, i = 1;
while (idx < people.length) {
while (count[i] == 0) {
i++;
}
people[idx++] = i;
count[i]--;
}

int res = 0, l = 0, r = people.length - 1;
while (l <= r) {
int remain = limit - people[r--];
res++;
if (l <= r && remain >= people[l]) {
l++;
}
}
return res;
}
}
```

```cpp
class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
int m = *max_element(people.begin(), people.end());
vector<int> count(m + 1, 0);
for (int p : people) {
count[p]++;
}

int idx = 0, i = 1;
while (idx < people.size()) {
while (count[i] == 0) {
i++;
}
people[idx++] = i;
count[i]--;
}

int res = 0, l = 0, r = people.size() - 1;
while (l <= r) {
int remain = limit - people[r--];
res++;
if (l <= r && remain >= people[l]) {
l++;
}
}
return res;
}
};
```

```javascript
class Solution {
/**
* @param {number[]} people
* @param {number} limit
* @return {number}
*/
numRescueBoats(people, limit) {
const m = Math.max(...people);
const count = new Array(m + 1).fill(0);
for (const p of people) {
count[p]++;
}

let idx = 0, i = 1;
while (idx < people.length) {
while (count[i] === 0) {
i++;
}
people[idx++] = i;
count[i]--;
}

let res = 0, l = 0, r = people.length - 1;
while (l <= r) {
const remain = limit - people[r--];
res++;
if (l <= r && remain >= people[l]) {
l++;
}
}
return res;
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(n)$
* Space complexity: $O(m)$

> Where $n$ is the size of the input array and $m$ is the maximum value in the array.
Loading