Skip to content

Commit 26ba875

Browse files
committed
LIVE_YOUTUBE - Developer Bhaiya - 2 problems, 1 script typing test
1 parent dcadb83 commit 26ba875

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

LeetCode/45.jump-game2.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
ass Solution {
2+
public:
3+
int jump(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<int> dp(n, INT_MAX - 1);
6+
// dp[i] = min steps to reach the end of array if we are at ith index
7+
dp[n-1] = 0;
8+
for(int i = n-2; i >= 0; i--) {
9+
for(int j = i+1; j <= i+nums[i]; j++) {
10+
if(j >= n) break;
11+
dp[i] = min(dp[i], 1 + dp[j]);
12+
}
13+
}
14+
15+
return dp[0];
16+
}
17+
};
18+
19+
/*
20+
Find: min steps to reach the end of array
21+
22+
Given: i -> we can jump a length of nums[i]
23+
0 index
24+
25+
_ _ _ _ 7 _ _ _ _ _ _ _ _ _
26+
i x x x x x x x
27+
28+
dp[i] = min steps to reach the end of array if we are at ith index
29+
30+
for(j=i+1; j<=i+7; j++) {
31+
dp[i] = min(dp[i], 1 + dp[j]);
32+
}
33+
34+
return dp[0]
35+
*/

LeetCode/841.keys-and-room.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
bool canVisitAllRooms(vector<vector<int>>& rooms) {
4+
int n = rooms.size();
5+
vector<bool> vis(n, 0);
6+
7+
queue<int> q;
8+
q.push(0);
9+
vis[0] = 1;
10+
while(!q.empty()) {
11+
int cur = q.front();
12+
q.pop();
13+
for(int child: rooms[cur]) {
14+
if (!vis[child]) {
15+
vis[child] = 1;
16+
q.push(child);
17+
}
18+
}
19+
}
20+
for(int i = 0; i < n; i++) {
21+
if(!vis[i]) return 0;
22+
}
23+
return 1;
24+
}
25+
};
26+
27+
/*
28+
Input: [[1,3],[3,0,1],[2],[0]]
29+
0 --> 1
30+
|
31+
|
32+
3
33+
*/

0 commit comments

Comments
 (0)