Skip to content

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

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 1 commit into from
Dec 14, 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
401 changes: 401 additions & 0 deletions articles/arranging-coins.md

Large diffs are not rendered by default.

300 changes: 300 additions & 0 deletions articles/assign-cookies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
## 1. Brute Force

::tabs-start

```python
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
s.sort()
res = 0

for i in g:
minIdx = -1
for j in range(len(s)):
if s[j] < i:
continue

if minIdx == -1 or s[minIdx] > s[j]:
minIdx = j

if minIdx != -1:
s[minIdx] = -1
res += 1

return res
```

```java
public class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(s);
int res = 0;

for (int i : g) {
int minIdx = -1;
for (int j = 0; j < s.length; j++) {
if (s[j] < i) continue;

if (minIdx == -1 || s[minIdx] > s[j]) {
minIdx = j;
}
}

if (minIdx != -1) {
s[minIdx] = -1;
res++;
}
}

return res;
}
}
```

```cpp
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(s.begin(), s.end());
int res = 0;

for (int i : g) {
int minIdx = -1;
for (int j = 0; j < s.size(); j++) {
if (s[j] < i) continue;

if (minIdx == -1 || s[minIdx] > s[j]) {
minIdx = j;
}
}

if (minIdx != -1) {
s[minIdx] = -1;
res++;
}
}

return res;
}
};
```

```javascript
class Solution {
/**
* @param {number[]} g
* @param {number[]} s
* @return {number}
*/
findContentChildren(g, s) {
s.sort((a, b) => a - b);
let res = 0;

for (let i of g) {
let minIdx = -1;
for (let j = 0; j < s.length; j++) {
if (s[j] < i) continue;

if (minIdx === -1 || s[minIdx] > s[j]) {
minIdx = j;
}
}

if (minIdx !== -1) {
s[minIdx] = -1;
res++;
}
}

return res;
}
}
```

::tabs-end

### Time & Space Complexity

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

> Where $n$ is the size of the array $g$ and $m$ is the size of the array $s$.

---

## 2. Two Pointers - I

::tabs-start

```python
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()

i = j = 0
while i < len(g):
while j < len(s) and g[i] > s[j]:
j += 1
if j == len(s):
break
i += 1
j += 1
return i
```

```java
public class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);

int i = 0, j = 0;
while (i < g.length) {
while (j < s.length && g[i] > s[j]) {
j++;
}
if (j == s.length) break;
i++;
j++;
}
return i;
}
}
```

```cpp
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());

int i = 0, j = 0;
while (i < g.size()) {
while (j < s.size() && g[i] > s[j]) {
j++;
}
if (j == s.size()) break;
i++;
j++;
}
return i;
}
};
```

```javascript
class Solution {
/**
* @param {number[]} g
* @param {number[]} s
* @return {number}
*/
findContentChildren(g, s) {
g.sort((a, b) => a - b);
s.sort((a, b) => a - b);

let i = 0, j = 0;
while (i < g.length) {
while (j < s.length && g[i] > s[j]) {
j++;
}
if (j === s.length) break;
i++;
j++;
}
return i;
}
}
```

::tabs-end

### Time & Space Complexity

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

> Where $n$ is the size of the array $g$ and $m$ is the size of the array $s$.

---

## 3. Two Pointers - II

::tabs-start

```python
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()

i = j = 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
i += 1
j += 1

return i
```

```java
public class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);

int i = 0;
for (int j = 0; i < g.length && j < s.length; j++) {
if (g[i] <= s[j]) i++;
}
return i;
}
}
```

```cpp
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());

int i = 0;
for (int j = 0; i < g.size() && j < s.size(); j++) {
if (g[i] <= s[j]) i++;
}
return i;
}
};
```

```javascript
class Solution {
/**
* @param {number[]} g
* @param {number[]} s
* @return {number}
*/
findContentChildren(g, s) {
g.sort((a, b) => a - b);
s.sort((a, b) => a - b);

let i = 0;
for (let j = 0; i < g.length && j < s.length; j++) {
if (g[i] <= s[j]) i++;
}
return i;
}
}
```

::tabs-end

### Time & Space Complexity

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

> Where $n$ is the size of the array $g$ and $m$ is the size of the array $s$.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy