-
-
Notifications
You must be signed in to change notification settings - Fork 195
[crumbs22] Week 04 Solution #1372
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
8 commits
Select commit
Hold shift + click to select a range
e9688ff
solve #224
crumbs22 26d6d6f
fix path #224
crumbs22 109ce61
Merge branch 'DaleStudy:main' into main
crumbs22 f83aad5
Feat: solve #227
crumbs22 a133ead
Feat: solve #245
crumbs22 42b40d0
REFACTOR: #227
crumbs22 7aa8d5f
Feat: solve #255
crumbs22 3b6be23
Feat: solve #269
crumbs22 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,36 @@ | ||
#include <vector> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
/* | ||
- dp[i]: 금액 i를 만드는 최소 코인수 | ||
- 초기값 | ||
- dp[0] = 0 | ||
- 그 외에는 amount + 1 (도달할 수 없는 값)으로 초기화 | ||
- 현재 금액 i를 만들기 위해서 | ||
이전 금액인 i - c를 만들고, 거기에 코인 c를 더 썼을 때 최소값을 갱신함 | ||
예시) coins = [1, 2, 5], i = 3일 때 | ||
c = 1 -> 3 >= 1 이므로 dp[3] = min(dp[3], dp[2] + 1) 갱신 | ||
c = 2 -> 3 >= 2 이므로 dp[3] = min(dp[3], dp[1] + 1) 갱신 | ||
c = 5 -> 3 < 5 이므로 건너뜀 | ||
=> i - c가 음수면 배열 범위를 벗어나므로 i >= c일때만 연산산 | ||
*/ | ||
class Solution { | ||
public: | ||
int coinChange(vector<int>& coins, int amount) { | ||
// dp 테이블 초기화 | ||
vector<int> dp(amount + 1, amount + 1); | ||
dp[0] = 0; | ||
|
||
// bottom up 연산 | ||
for (int i = 1; i <= amount; ++i) { | ||
for (int c : coins) { | ||
if (i >= c) { | ||
dp[i] = min(dp[i], dp[i - c] + 1); | ||
} | ||
} | ||
} | ||
|
||
return (dp[amount] > amount) ? -1 : 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,32 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
/* | ||
O(logn)으로 풀어라 | ||
- 이진 탐색 느낌 | ||
- mid값과 오른쪽 끝을 비교, | ||
- mid > 오른쪽 끝 : 최소는 오른쪽에 | ||
- mid <= 오른쪽 끝 : 최소는 mid 포함 왼쪽에 | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int findMin(vector<int>& nums) { | ||
int left = 0; | ||
int right = nums.size() - 1; | ||
|
||
while (left < right) { | ||
int mid = left + (right - left) / 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,19 @@ | ||
#include <algorithm> | ||
|
||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
int maxDepth(TreeNode* root) { | ||
if (!root) | ||
return (0); | ||
return (std::max(maxDepth(root->left), maxDepth(root->right)) + 1); | ||
} | ||
}; |
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,44 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
struct ListNode { | ||
int val; | ||
ListNode *next; | ||
ListNode() : val(0), next(nullptr) {} | ||
ListNode(int x) : val(x), next(nullptr) {} | ||
ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { | ||
if (!list1) | ||
return (list2); | ||
if (!list2) | ||
return (list1); | ||
|
||
ListNode ans_head = ListNode(); | ||
ListNode* tmp = &ans_head; | ||
|
||
while (list1 && list2) { | ||
if (list1->val <= list2->val) { | ||
tmp->next = list1; | ||
list1 = list1->next; | ||
tmp = tmp->next; | ||
} | ||
else { | ||
tmp->next = list2; | ||
list2 = list2->next; | ||
tmp = tmp->next; | ||
} | ||
} | ||
if (list1) { | ||
tmp->next = list1; | ||
} | ||
else if (list2) { | ||
tmp->next = list2; | ||
} | ||
return (ans_head.next); | ||
} | ||
}; | ||
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,41 @@ | ||
#include <vector> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
bool exist(vector<vector<char>>& board, string word) { | ||
// 단어 시작점을 탐색, 시작점 찾으면 dfs 시작하고 마지막 단어까지 도달 가능할 때 true 반환 | ||
for (int i = 0; i < board.size(); i++) { | ||
for (int j = 0; j < board[0].size(); j++) { | ||
if (board[i][j] == word[0] && dfs(board, i, j, 0, word)) { | ||
return (true); | ||
} | ||
} | ||
} | ||
return (false); | ||
} | ||
bool dfs(vector<vector<char>>& board, int i, int j, int idx, string& word) { | ||
if (idx == word.size()) | ||
return (true); | ||
if (i < 0 || i >= board.size() || \ | ||
j < 0 || j >= board[0].size() || \ | ||
board[i][j] != word[idx]) { | ||
return (false); | ||
} | ||
|
||
char tmp = board[i][j]; | ||
|
||
// 방문 표시 | ||
board[i][j] = '1'; | ||
|
||
// 4방향으로 탐색 | ||
bool found = dfs(board, i + 1, j, idx + 1, word) || dfs(board, i - 1, j, idx + 1, word) || \ | ||
dfs(board, i, j + 1, idx + 1, word) || dfs(board, i, j - 1, idx + 1, word); | ||
|
||
// 다른 경로 탐색을 위해서 방문 표시를 원래대로 복원 | ||
board[i][j] = tmp; | ||
return (found); | ||
} | ||
}; |
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++ 코드는 오랜만에 보는데 java와 다르게 포인터를 써서 주소값을 tmp에 넣어서 하는 것이 차이점으로 보이네요!