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

Skip to content

Commit ff45c13

Browse files
authored
Batch-4/Neetcode-All/Added-articles (neetcode-gh#3766)
1 parent fdaf14b commit ff45c13

26 files changed

+8454
-1
lines changed

articles/arranging-coins.md

Lines changed: 401 additions & 0 deletions
Large diffs are not rendered by default.

articles/assign-cookies.md

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
## 1. Brute Force
2+
3+
::tabs-start
4+
5+
```python
6+
class Solution:
7+
def findContentChildren(self, g: List[int], s: List[int]) -> int:
8+
s.sort()
9+
res = 0
10+
11+
for i in g:
12+
minIdx = -1
13+
for j in range(len(s)):
14+
if s[j] < i:
15+
continue
16+
17+
if minIdx == -1 or s[minIdx] > s[j]:
18+
minIdx = j
19+
20+
if minIdx != -1:
21+
s[minIdx] = -1
22+
res += 1
23+
24+
return res
25+
```
26+
27+
```java
28+
public class Solution {
29+
public int findContentChildren(int[] g, int[] s) {
30+
Arrays.sort(s);
31+
int res = 0;
32+
33+
for (int i : g) {
34+
int minIdx = -1;
35+
for (int j = 0; j < s.length; j++) {
36+
if (s[j] < i) continue;
37+
38+
if (minIdx == -1 || s[minIdx] > s[j]) {
39+
minIdx = j;
40+
}
41+
}
42+
43+
if (minIdx != -1) {
44+
s[minIdx] = -1;
45+
res++;
46+
}
47+
}
48+
49+
return res;
50+
}
51+
}
52+
```
53+
54+
```cpp
55+
class Solution {
56+
public:
57+
int findContentChildren(vector<int>& g, vector<int>& s) {
58+
sort(s.begin(), s.end());
59+
int res = 0;
60+
61+
for (int i : g) {
62+
int minIdx = -1;
63+
for (int j = 0; j < s.size(); j++) {
64+
if (s[j] < i) continue;
65+
66+
if (minIdx == -1 || s[minIdx] > s[j]) {
67+
minIdx = j;
68+
}
69+
}
70+
71+
if (minIdx != -1) {
72+
s[minIdx] = -1;
73+
res++;
74+
}
75+
}
76+
77+
return res;
78+
}
79+
};
80+
```
81+
82+
```javascript
83+
class Solution {
84+
/**
85+
* @param {number[]} g
86+
* @param {number[]} s
87+
* @return {number}
88+
*/
89+
findContentChildren(g, s) {
90+
s.sort((a, b) => a - b);
91+
let res = 0;
92+
93+
for (let i of g) {
94+
let minIdx = -1;
95+
for (let j = 0; j < s.length; j++) {
96+
if (s[j] < i) continue;
97+
98+
if (minIdx === -1 || s[minIdx] > s[j]) {
99+
minIdx = j;
100+
}
101+
}
102+
103+
if (minIdx !== -1) {
104+
s[minIdx] = -1;
105+
res++;
106+
}
107+
}
108+
109+
return res;
110+
}
111+
}
112+
```
113+
114+
::tabs-end
115+
116+
### Time & Space Complexity
117+
118+
* Time complexity: $O(n * m + m \log m)$
119+
* Space complexity: $O(1)$ or $O(m)$ depending on the sorting algorithm.
120+
121+
> Where $n$ is the size of the array $g$ and $m$ is the size of the array $s$.
122+
123+
---
124+
125+
## 2. Two Pointers - I
126+
127+
::tabs-start
128+
129+
```python
130+
class Solution:
131+
def findContentChildren(self, g: List[int], s: List[int]) -> int:
132+
g.sort()
133+
s.sort()
134+
135+
i = j = 0
136+
while i < len(g):
137+
while j < len(s) and g[i] > s[j]:
138+
j += 1
139+
if j == len(s):
140+
break
141+
i += 1
142+
j += 1
143+
return i
144+
```
145+
146+
```java
147+
public class Solution {
148+
public int findContentChildren(int[] g, int[] s) {
149+
Arrays.sort(g);
150+
Arrays.sort(s);
151+
152+
int i = 0, j = 0;
153+
while (i < g.length) {
154+
while (j < s.length && g[i] > s[j]) {
155+
j++;
156+
}
157+
if (j == s.length) break;
158+
i++;
159+
j++;
160+
}
161+
return i;
162+
}
163+
}
164+
```
165+
166+
```cpp
167+
class Solution {
168+
public:
169+
int findContentChildren(vector<int>& g, vector<int>& s) {
170+
sort(g.begin(), g.end());
171+
sort(s.begin(), s.end());
172+
173+
int i = 0, j = 0;
174+
while (i < g.size()) {
175+
while (j < s.size() && g[i] > s[j]) {
176+
j++;
177+
}
178+
if (j == s.size()) break;
179+
i++;
180+
j++;
181+
}
182+
return i;
183+
}
184+
};
185+
```
186+
187+
```javascript
188+
class Solution {
189+
/**
190+
* @param {number[]} g
191+
* @param {number[]} s
192+
* @return {number}
193+
*/
194+
findContentChildren(g, s) {
195+
g.sort((a, b) => a - b);
196+
s.sort((a, b) => a - b);
197+
198+
let i = 0, j = 0;
199+
while (i < g.length) {
200+
while (j < s.length && g[i] > s[j]) {
201+
j++;
202+
}
203+
if (j === s.length) break;
204+
i++;
205+
j++;
206+
}
207+
return i;
208+
}
209+
}
210+
```
211+
212+
::tabs-end
213+
214+
### Time & Space Complexity
215+
216+
* Time complexity: $O(n \log n + m \log m)$
217+
* Space complexity: $O(1)$ or $O(n + m)$ depending on the sorting algorithm.
218+
219+
> Where $n$ is the size of the array $g$ and $m$ is the size of the array $s$.
220+
221+
---
222+
223+
## 3. Two Pointers - II
224+
225+
::tabs-start
226+
227+
```python
228+
class Solution:
229+
def findContentChildren(self, g: List[int], s: List[int]) -> int:
230+
g.sort()
231+
s.sort()
232+
233+
i = j = 0
234+
while i < len(g) and j < len(s):
235+
if g[i] <= s[j]:
236+
i += 1
237+
j += 1
238+
239+
return i
240+
```
241+
242+
```java
243+
public class Solution {
244+
public int findContentChildren(int[] g, int[] s) {
245+
Arrays.sort(g);
246+
Arrays.sort(s);
247+
248+
int i = 0;
249+
for (int j = 0; i < g.length && j < s.length; j++) {
250+
if (g[i] <= s[j]) i++;
251+
}
252+
return i;
253+
}
254+
}
255+
```
256+
257+
```cpp
258+
class Solution {
259+
public:
260+
int findContentChildren(vector<int>& g, vector<int>& s) {
261+
sort(g.begin(), g.end());
262+
sort(s.begin(), s.end());
263+
264+
int i = 0;
265+
for (int j = 0; i < g.size() && j < s.size(); j++) {
266+
if (g[i] <= s[j]) i++;
267+
}
268+
return i;
269+
}
270+
};
271+
```
272+
273+
```javascript
274+
class Solution {
275+
/**
276+
* @param {number[]} g
277+
* @param {number[]} s
278+
* @return {number}
279+
*/
280+
findContentChildren(g, s) {
281+
g.sort((a, b) => a - b);
282+
s.sort((a, b) => a - b);
283+
284+
let i = 0;
285+
for (let j = 0; i < g.length && j < s.length; j++) {
286+
if (g[i] <= s[j]) i++;
287+
}
288+
return i;
289+
}
290+
}
291+
```
292+
293+
::tabs-end
294+
295+
### Time & Space Complexity
296+
297+
* Time complexity: $O(n \log n + m \log m)$
298+
* Space complexity: $O(1)$ or $O(n + m)$ depending on the sorting algorithm.
299+
300+
> Where $n$ is the size of the array $g$ and $m$ is the size of the array $s$.

0 commit comments

Comments
 (0)