Skip to content

[PDKhan] WEEK 10 solutions #1542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions course-schedule/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//DFS
class Solution {
public:
bool dfs(int curr, vector<vector<int>>& graph, vector<int>& visited){
if(visited[curr] == 1)
return false;

if(visited[curr] == 2)
return true;

visited[curr] = 1;

for(int i = 0; i < graph[curr].size(); i++){
if(dfs(graph[curr][i], graph, visited) == false)
return false;
}

visited[curr] = 2;

return true;
}

bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> graph (numCourses);
vector<int> visited (numCourses, 0);

for(int i = 0; i < prerequisites.size(); i++){
int course = prerequisites[i][0];
int pre = prerequisites[i][1];

graph[pre].push_back(course);
}

for(int i = 0; i < numCourses; i++){
if(dfs(i, graph, visited) == false)
return false;
}

return true;
}
};

// BFS
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> graph (numCourses);
vector<int> inDegree (numCourses, 0);

for(int i = 0; i < prerequisites.size(); i++){
int course = prerequisites[i][0];
int pre = prerequisites[i][1];

graph[pre].push_back(course);
inDegree[course]++;
}

queue<int> q;

for(int i = 0; i < numCourses; i++){
if(inDegree[i] == 0)
q.push(i);
}

int count = 0;

while(!q.empty()){
int curr = q.front();
q.pop();
count++;

for(int i = 0; i < graph[curr].size(); i++){
if(--inDegree[graph[curr][i]] == 0)
q.push(graph[curr][i]);
}
}

return count == numCourses;;
}
};
14 changes: 14 additions & 0 deletions invert-binary-tree/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++에서는 TreeNode*처럼 포인터임을 명확히 표시해서 TreeNode 객체와 구분할 수 있어 좋으네요...

if(root == NULL)
return NULL;

TreeNode* tmp = root->left;

root->left = invertTree(root->right);
root->right = invertTree(tmp);

return root;
}
};
13 changes: 13 additions & 0 deletions jump-game/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
bool canJump(vector<int>& nums) {
int goal = nums.size() - 1;

for(int i = nums.size() - 2; i >= 0; i--){
if(nums[i] + i >= goal)
goal = i;
}

return goal == 0;
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 식으로 역방향으로 접근할 수도 있군요. 👍

36 changes: 36 additions & 0 deletions merge-k-sorted-lists/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
struct Compare{
bool operator()(ListNode* a, ListNode* b){
return a->val > b->val;
}
};

class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<ListNode*, vector<ListNode*>, Compare> pq;
ListNode* head = nullptr;
ListNode* tail;

for(ListNode* node : lists){
if(node) pq.push(node);
}

while(!pq.empty()){
ListNode* node = pq.top();

pq.pop();
if(head == nullptr){
head = node;
tail = head;
}else{
tail->next = node;
tail = tail->next;
}

if(node->next)
pq.push(node->next);
}

return head;
}
};
28 changes: 28 additions & 0 deletions search-in-rotated-sorted-array/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
int search(vector<int>& nums, int target) {
int start = 0;
int end = nums.size() - 1;

while(start <= end){
int mid = start + (end - start) / 2;

if(nums[mid] == target)
return mid;

if(nums[start] <= nums[mid]){
if(nums[start] <= target && nums[mid] > target)
end = mid - 1;
else
start = mid + 1;
}else{
if(nums[mid] < target && nums[end] >= target)
start = mid + 1;
else
end = mid - 1;
}
}

return -1;
}
};