-
-
Notifications
You must be signed in to change notification settings - Fork 195
[PDKhan] WEEK 04 solutions #1332
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
5 commits
Select commit
Hold shift + click to select a range
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,20 @@ | ||
class Solution { | ||
public: | ||
int coinChange(vector<int>& coins, int amount) { | ||
vector<int> dp(amount+1, amount+1); | ||
|
||
dp[0] = 0; | ||
|
||
for(int i = 1; i <= amount; i++){ | ||
for(int j = 0; j < coins.size(); j++){ | ||
if(i >= coins[j]) | ||
dp[i] = min(dp[i], 1 + dp[i - coins[j]]); | ||
} | ||
} | ||
|
||
if(dp[amount] > amount) | ||
return -1; | ||
|
||
return dp[amount]; | ||
} | ||
}; |
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,18 @@ | ||
class Solution { | ||
public: | ||
int findMin(vector<int>& nums) { | ||
int left = 0; | ||
int right = nums.size() - 1; | ||
|
||
while(left < right){ | ||
int mid = (left + right) / 2; | ||
|
||
if(nums[mid] > nums[right]) | ||
left = mid + 1; | ||
else | ||
right = mid; | ||
} | ||
|
||
return nums[left]; | ||
} | ||
}; |
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,9 @@ | ||
class Solution { | ||
public: | ||
int maxDepth(TreeNode* root) { | ||
if(root == nullptr) | ||
return 0; | ||
|
||
return 1 + max(maxDepth(root->left), maxDepth(root->right)); | ||
} | ||
}; |
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,37 @@ | ||
class Solution { | ||
public: | ||
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { | ||
ListNode* new_head = NULL; | ||
ListNode* tail; | ||
|
||
while(list1 || list2){ | ||
ListNode* curr; | ||
|
||
if(list1 && list2){ | ||
if(list1->val < list2->val){ | ||
curr = list1; | ||
list1 = list1->next; | ||
}else{ | ||
curr = list2; | ||
list2 = list2->next; | ||
} | ||
}else if(list1){ | ||
curr = list1; | ||
list1 = list1->next; | ||
}else{ | ||
curr = list2; | ||
list2 = list2->next; | ||
} | ||
|
||
if(new_head == NULL){ | ||
new_head = curr; | ||
tail = new_head; | ||
}else{ | ||
tail->next = curr; | ||
tail = tail->next; | ||
} | ||
} | ||
|
||
return new_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,43 @@ | ||
class Solution { | ||
public: | ||
bool search(int r, int c, int index, vector<vector<char>>& board, string word){ | ||
if(index == word.length()) | ||
return true; | ||
|
||
if(r < 0 || r >= board.size() || c < 0 || c >= board[0].size() || board[r][c] != word[index]) | ||
return false; | ||
|
||
char curr = board[r][c]; | ||
|
||
board[r][c] = '0'; | ||
|
||
if(search(r + 1, c, index + 1, board, word) == true) | ||
return true; | ||
|
||
if(search(r - 1, c, index + 1, board, word) == true) | ||
return true; | ||
|
||
if(search(r, c + 1, index + 1, board, word) == true) | ||
return true; | ||
|
||
if(search(r, c - 1, index + 1, board, word) == true) | ||
return true; | ||
|
||
board[r][c] = curr; | ||
|
||
return false; | ||
} | ||
|
||
bool exist(vector<vector<char>>& board, string word) { | ||
for(int i = 0; i < board.size(); i++){ | ||
for(int j = 0; j < board[i].size(); j++){ | ||
if(board[i][j] == word[0]){ | ||
if(search(i, j, 0, board, word) == true) | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
}; |
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.
저번 주차의 combination sums랑 비슷한 방식으로 풀이할 수 있군요! 풀이 하나 배워갑니다 ㅎㅎ