Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3a66f08

Browse files
authored
Sri Hari: Batch-5/Neetcode-ALL/Added-articles (neetcode-gh#3854)
* Batch-5/Neetcode-ALL/Added-articles* Batch-5/Neetcode-ALL/Added-articles* Batch-5/Neetcode-ALL/Added-articles
1 parent9e68fa1 commit3a66f08

14 files changed

+6773
-0
lines changed

‎articles/constrained-subsequence-sum.md

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

‎articles/find-the-kth-largest-integer-in-the-array.md

Lines changed: 543 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 387 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,387 @@
1+
##1. Dynamic Programming (Top-Down)
2+
3+
::tabs-start
4+
5+
```python
6+
classSolution:
7+
deflongestObstacleCourseAtEachPosition(self,obstacles: List[int]) -> List[int]:
8+
n=len(obstacles)
9+
dp= [[-1]* (n+1)for _inrange(n)]
10+
11+
defdfs(i,prev):
12+
if i<0:
13+
return0
14+
if dp[i][prev]!=-1:
15+
return dp[i][prev]
16+
17+
res= dfs(i-1, prev)
18+
if prev== nor obstacles[prev]>= obstacles[i]:
19+
res=max(res,1+ dfs(i-1, i))
20+
dp[i][prev]= res
21+
return res
22+
23+
dfs(n-1, n)
24+
return [1]+ [1+ dp[i-1][i]for iinrange(1, n)]
25+
```
26+
27+
```java
28+
publicclassSolution {
29+
privateint[][] dp;
30+
31+
publicint[]longestObstacleCourseAtEachPosition(int[]obstacles) {
32+
int n= obstacles.length;
33+
this.dp=newint[n][n+1];
34+
for (int[] row: dp) {
35+
Arrays.fill(row,-1);
36+
}
37+
38+
dfs(n-1, n, obstacles);
39+
40+
int[] res=newint[n];
41+
res[0]=1;
42+
for (int i=1; i< n; i++) {
43+
res[i]=1+ dp[i-1][i];
44+
}
45+
return res;
46+
}
47+
48+
privateintdfs(inti,intprev,int[]obstacles) {
49+
if (i<0) {
50+
return0;
51+
}
52+
if (dp[i][prev]!=-1) {
53+
return dp[i][prev];
54+
}
55+
56+
int res= dfs(i-1, prev, obstacles);
57+
if (prev== obstacles.length|| obstacles[prev]>= obstacles[i]) {
58+
res=Math.max(res,1+ dfs(i-1, i, obstacles));
59+
}
60+
return dp[i][prev]= res;
61+
}
62+
}
63+
```
64+
65+
```cpp
66+
classSolution {
67+
public:
68+
vector<vector<int>> dp;
69+
70+
vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {
71+
int n = obstacles.size();
72+
this->dp = vector<vector<int>>(n, vector<int>(n + 1, -1));
73+
74+
dfs(n - 1, n, obstacles);
75+
76+
vector<int> res(n, 1);
77+
for (int i = 1; i < n; i++) {
78+
res[i] = 1 + dp[i - 1][i];
79+
}
80+
return res;
81+
}
82+
83+
private:
84+
intdfs(int i, int prev, vector<int>& obstacles) {
85+
if (i < 0) {
86+
return 0;
87+
}
88+
if (dp[i][prev] != -1) {
89+
return dp[i][prev];
90+
}
91+
92+
int res = dfs(i - 1, prev, obstacles);
93+
if (prev == obstacles.size() || obstacles[prev] >= obstacles[i]) {
94+
res = max(res, 1 + dfs(i - 1, i, obstacles));
95+
}
96+
return dp[i][prev] = res;
97+
}
98+
};
99+
```
100+
101+
```javascript
102+
class Solution {
103+
/**
104+
* @param {number[]} obstacles
105+
* @return {number[]}
106+
*/
107+
longestObstacleCourseAtEachPosition(obstacles) {
108+
const n = obstacles.length;
109+
const dp = Array.from({ length: n }, () => new Array(n + 1).fill(-1));
110+
111+
const dfs = (i, prev) => {
112+
if (i < 0) {
113+
return 0;
114+
}
115+
if (dp[i][prev] !== -1) {
116+
return dp[i][prev];
117+
}
118+
119+
let res = dfs(i - 1, prev);
120+
if (prev === n || obstacles[prev] >= obstacles[i]) {
121+
res = Math.max(res, 1 + dfs(i - 1, i));
122+
}
123+
dp[i][prev] = res;
124+
return res;
125+
};
126+
127+
dfs(n - 1, n);
128+
129+
const res = new Array(n).fill(1);
130+
for (let i = 1; i < n; i++) {
131+
res[i] = 1 + dp[i - 1][i];
132+
}
133+
return res;
134+
}
135+
}
136+
```
137+
138+
::tabs-end
139+
140+
###Time & Space Complexity
141+
142+
* Time complexity: $O(n ^ 2)$
143+
* Space complexity: $O(n ^ 2)$
144+
145+
---
146+
147+
##2. Dynamic Programming (Binary Search) - I
148+
149+
::tabs-start
150+
151+
```python
152+
classSolution:
153+
deflongestObstacleCourseAtEachPosition(self,obstacles: List[int]) -> List[int]:
154+
res= []
155+
dp= [10**8]* (len(obstacles)+1)
156+
157+
for numin obstacles:
158+
index= bisect.bisect(dp, num)
159+
res.append(index+1)
160+
dp[index]= num
161+
162+
return res
163+
```
164+
165+
```java
166+
publicclassSolution {
167+
publicint[]longestObstacleCourseAtEachPosition(int[]obstacles) {
168+
int n= obstacles.length;
169+
int[] res=newint[n];
170+
int[] dp=newint[n+1];
171+
Arrays.fill(dp, (int)1e8);
172+
173+
for (int i=0; i< n; i++) {
174+
int index= upperBound(dp, obstacles[i]);
175+
res[i]= index+1;
176+
dp[index]= obstacles[i];
177+
}
178+
179+
return res;
180+
}
181+
182+
privateintupperBound(int[]dp,inttarget) {
183+
int left=0, right= dp.length;
184+
while (left< right) {
185+
int mid= left+ (right- left)/2;
186+
if (dp[mid]> target) {
187+
right= mid;
188+
}else {
189+
left= mid+1;
190+
}
191+
}
192+
return left;
193+
}
194+
}
195+
```
196+
197+
```cpp
198+
classSolution {
199+
public:
200+
vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {
201+
int n = obstacles.size();
202+
vector<int> res(n);
203+
vector<int> dp(n + 1, 1e8);
204+
205+
for (int i = 0; i < n; i++) {
206+
int index = upper_bound(dp.begin(), dp.end(), obstacles[i]) - dp.begin();
207+
res[i] = index + 1;
208+
dp[index] = obstacles[i];
209+
}
210+
211+
return res;
212+
}
213+
};
214+
```
215+
216+
```javascript
217+
classSolution {
218+
/**
219+
*@param{number[]}obstacles
220+
*@return{number[]}
221+
*/
222+
longestObstacleCourseAtEachPosition(obstacles) {
223+
let n=obstacles.length;
224+
let res=newArray(n).fill(0);
225+
let dp=newArray(n+1).fill(1e8);
226+
227+
constupperBound= (dp,target)=> {
228+
let left=0, right=dp.length;
229+
while (left< right) {
230+
let mid=Math.floor((left+ right)/2);
231+
if (dp[mid]> target) {
232+
right= mid;
233+
}else {
234+
left= mid+1;
235+
}
236+
}
237+
return left;
238+
};
239+
240+
for (let i=0; i< n; i++) {
241+
let index=upperBound(dp, obstacles[i]);
242+
res[i]= index+1;
243+
dp[index]= obstacles[i];
244+
}
245+
246+
return res;
247+
}
248+
}
249+
```
250+
251+
::tabs-end
252+
253+
###Time & Space Complexity
254+
255+
* Time complexity: $O(n \log n)$
256+
* Space complexity: $O(n)$
257+
258+
---
259+
260+
##3. Dynamic Programming (Binary Search) - II
261+
262+
::tabs-start
263+
264+
```python
265+
classSolution:
266+
deflongestObstacleCourseAtEachPosition(self,obstacles: List[int]) -> List[int]:
267+
res= []
268+
dp= []
269+
270+
for numin obstacles:
271+
index= bisect.bisect_right(dp, num)
272+
res.append(index+1)
273+
274+
if index==len(dp):
275+
dp.append(num)
276+
else:
277+
dp[index]= num
278+
279+
return res
280+
```
281+
282+
```java
283+
publicclassSolution {
284+
publicint[]longestObstacleCourseAtEachPosition(int[]obstacles) {
285+
int n= obstacles.length;
286+
int[] res=newint[n];
287+
List<Integer> dp=newArrayList<>();
288+
289+
for (int i=0; i< n; i++) {
290+
int index= upperBound(dp, obstacles[i]);
291+
res[i]= index+1;
292+
293+
if (index== dp.size()) {
294+
dp.add(obstacles[i]);
295+
}else {
296+
dp.set(index, obstacles[i]);
297+
}
298+
}
299+
300+
return res;
301+
}
302+
303+
privateintupperBound(List<Integer>dp,inttarget) {
304+
int left=0, right= dp.size();
305+
while (left< right) {
306+
int mid= left+ (right- left)/2;
307+
if (dp.get(mid)> target) {
308+
right= mid;
309+
}else {
310+
left= mid+1;
311+
}
312+
}
313+
return left;
314+
}
315+
}
316+
```
317+
318+
```cpp
319+
classSolution {
320+
public:
321+
vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {
322+
int n = obstacles.size();
323+
vector<int> res(n);
324+
vector<int> dp;
325+
326+
for (int i = 0; i < n; i++) {
327+
int index = upper_bound(dp.begin(), dp.end(), obstacles[i]) - dp.begin();
328+
res[i] = index + 1;
329+
330+
if (index == dp.size()) {
331+
dp.push_back(obstacles[i]);
332+
} else {
333+
dp[index] = obstacles[i];
334+
}
335+
}
336+
337+
return res;
338+
}
339+
};
340+
```
341+
342+
```javascript
343+
classSolution {
344+
/**
345+
*@param{number[]}obstacles
346+
*@return{number[]}
347+
*/
348+
longestObstacleCourseAtEachPosition(obstacles) {
349+
let n=obstacles.length;
350+
let res=newArray(n).fill(0);
351+
let dp= [];
352+
353+
constupperBound= (dp,target)=> {
354+
let left=0, right=dp.length;
355+
while (left< right) {
356+
let mid=Math.floor((left+ right)/2);
357+
if (dp[mid]> target) {
358+
right= mid;
359+
}else {
360+
left= mid+1;
361+
}
362+
}
363+
return left;
364+
};
365+
366+
for (let i=0; i< n; i++) {
367+
let index=upperBound(dp, obstacles[i]);
368+
res[i]= index+1;
369+
370+
if (index===dp.length) {
371+
dp.push(obstacles[i]);
372+
}else {
373+
dp[index]= obstacles[i];
374+
}
375+
}
376+
377+
return res;
378+
}
379+
}
380+
```
381+
382+
::tabs-end
383+
384+
###Time & Space Complexity
385+
386+
* Time complexity: $O(n \log n)$
387+
* Space complexity: $O(n)$

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp