-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
66f9809
Invert Binary Tree Solution
PDKhan 591c5e1
Search In Rotated Sorted Arry Solution
PDKhan 1d62ca4
Course Schedule Solution
PDKhan 5f9eca0
Jump Game Solution
PDKhan 7e0d648
Merge K Sorted Lists Solution
PDKhan 4c9c1c0
Add new line
PDKhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
public: | ||
TreeNode* invertTree(TreeNode* root) { | ||
if(root == NULL) | ||
return NULL; | ||
|
||
TreeNode* tmp = root->left; | ||
|
||
root->left = invertTree(root->right); | ||
root->right = invertTree(tmp); | ||
|
||
return root; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런 식으로 역방향으로 접근할 수도 있군요. 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C++에서는 TreeNode*처럼 포인터임을 명확히 표시해서 TreeNode 객체와 구분할 수 있어 좋으네요...