-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Batch-4/Neetcode-All/Added-articles (#3766)
- Loading branch information
1 parent
fdaf14b
commit ff45c13
Showing
26 changed files
with
8,454 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$. |
Oops, something went wrong.