Skip to content

Commit ce73260

Browse files
authored
Merge pull request #1542 from PDKhan/main
2 parents 7f318e0 + 4c9c1c0 commit ce73260

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

course-schedule/PDKhan.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//DFS
2+
class Solution {
3+
public:
4+
bool dfs(int curr, vector<vector<int>>& graph, vector<int>& visited){
5+
if(visited[curr] == 1)
6+
return false;
7+
8+
if(visited[curr] == 2)
9+
return true;
10+
11+
visited[curr] = 1;
12+
13+
for(int i = 0; i < graph[curr].size(); i++){
14+
if(dfs(graph[curr][i], graph, visited) == false)
15+
return false;
16+
}
17+
18+
visited[curr] = 2;
19+
20+
return true;
21+
}
22+
23+
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
24+
vector<vector<int>> graph (numCourses);
25+
vector<int> visited (numCourses, 0);
26+
27+
for(int i = 0; i < prerequisites.size(); i++){
28+
int course = prerequisites[i][0];
29+
int pre = prerequisites[i][1];
30+
31+
graph[pre].push_back(course);
32+
}
33+
34+
for(int i = 0; i < numCourses; i++){
35+
if(dfs(i, graph, visited) == false)
36+
return false;
37+
}
38+
39+
return true;
40+
}
41+
};
42+
43+
// BFS
44+
class Solution {
45+
public:
46+
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
47+
vector<vector<int>> graph (numCourses);
48+
vector<int> inDegree (numCourses, 0);
49+
50+
for(int i = 0; i < prerequisites.size(); i++){
51+
int course = prerequisites[i][0];
52+
int pre = prerequisites[i][1];
53+
54+
graph[pre].push_back(course);
55+
inDegree[course]++;
56+
}
57+
58+
queue<int> q;
59+
60+
for(int i = 0; i < numCourses; i++){
61+
if(inDegree[i] == 0)
62+
q.push(i);
63+
}
64+
65+
int count = 0;
66+
67+
while(!q.empty()){
68+
int curr = q.front();
69+
q.pop();
70+
count++;
71+
72+
for(int i = 0; i < graph[curr].size(); i++){
73+
if(--inDegree[graph[curr][i]] == 0)
74+
q.push(graph[curr][i]);
75+
}
76+
}
77+
78+
return count == numCourses;;
79+
}
80+
};

invert-binary-tree/PDKhan.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
TreeNode* invertTree(TreeNode* root) {
4+
if(root == NULL)
5+
return NULL;
6+
7+
TreeNode* tmp = root->left;
8+
9+
root->left = invertTree(root->right);
10+
root->right = invertTree(tmp);
11+
12+
return root;
13+
}
14+
};

jump-game/PDKhan.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool canJump(vector<int>& nums) {
4+
int goal = nums.size() - 1;
5+
6+
for(int i = nums.size() - 2; i >= 0; i--){
7+
if(nums[i] + i >= goal)
8+
goal = i;
9+
}
10+
11+
return goal == 0;
12+
}
13+
};

merge-k-sorted-lists/PDKhan.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
struct Compare{
2+
bool operator()(ListNode* a, ListNode* b){
3+
return a->val > b->val;
4+
}
5+
};
6+
7+
class Solution {
8+
public:
9+
ListNode* mergeKLists(vector<ListNode*>& lists) {
10+
priority_queue<ListNode*, vector<ListNode*>, Compare> pq;
11+
ListNode* head = nullptr;
12+
ListNode* tail;
13+
14+
for(ListNode* node : lists){
15+
if(node) pq.push(node);
16+
}
17+
18+
while(!pq.empty()){
19+
ListNode* node = pq.top();
20+
21+
pq.pop();
22+
if(head == nullptr){
23+
head = node;
24+
tail = head;
25+
}else{
26+
tail->next = node;
27+
tail = tail->next;
28+
}
29+
30+
if(node->next)
31+
pq.push(node->next);
32+
}
33+
34+
return head;
35+
}
36+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int search(vector<int>& nums, int target) {
4+
int start = 0;
5+
int end = nums.size() - 1;
6+
7+
while(start <= end){
8+
int mid = start + (end - start) / 2;
9+
10+
if(nums[mid] == target)
11+
return mid;
12+
13+
if(nums[start] <= nums[mid]){
14+
if(nums[start] <= target && nums[mid] > target)
15+
end = mid - 1;
16+
else
17+
start = mid + 1;
18+
}else{
19+
if(nums[mid] < target && nums[end] >= target)
20+
start = mid + 1;
21+
else
22+
end = mid - 1;
23+
}
24+
}
25+
26+
return -1;
27+
}
28+
};

0 commit comments

Comments
 (0)