Skip to content

Commit 8c7f94d

Browse files
authored
Merge pull request #1372 from crumbs22/main
[crumbs22] Week 04 Solution
2 parents 86c0949 + 3b6be23 commit 8c7f94d

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

β€Žcoin-change/crumbs22.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <vector>
2+
#include <algorithm>
3+
using namespace std;
4+
5+
/*
6+
- dp[i]: κΈˆμ•‘ iλ₯Ό λ§Œλ“œλŠ” μ΅œμ†Œ μ½”μΈμˆ˜
7+
- μ΄ˆκΈ°κ°’
8+
- dp[0] = 0
9+
- κ·Έ μ™Έμ—λŠ” amount + 1 (도달할 수 μ—†λŠ” κ°’)으둜 μ΄ˆκΈ°ν™”
10+
- ν˜„μž¬ κΈˆμ•‘ iλ₯Ό λ§Œλ“€κΈ° μœ„ν•΄μ„œ
11+
이전 κΈˆμ•‘μΈ i - cλ₯Ό λ§Œλ“€κ³ , 거기에 코인 cλ₯Ό 더 썼을 λ•Œ μ΅œμ†Œκ°’μ„ 갱신함
12+
μ˜ˆμ‹œ) coins = [1, 2, 5], i = 3일 λ•Œ
13+
c = 1 -> 3 >= 1 μ΄λ―€λ‘œ dp[3] = min(dp[3], dp[2] + 1) κ°±μ‹ 
14+
c = 2 -> 3 >= 2 μ΄λ―€λ‘œ dp[3] = min(dp[3], dp[1] + 1) κ°±μ‹ 
15+
c = 5 -> 3 < 5 μ΄λ―€λ‘œ κ±΄λ„ˆλœ€
16+
=> i - cκ°€ 음수면 λ°°μ—΄ λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λ―€λ‘œ i >= cμΌλ•Œλ§Œ μ—°μ‚°μ‚°
17+
*/
18+
class Solution {
19+
public:
20+
int coinChange(vector<int>& coins, int amount) {
21+
// dp ν…Œμ΄λΈ” μ΄ˆκΈ°ν™”
22+
vector<int> dp(amount + 1, amount + 1);
23+
dp[0] = 0;
24+
25+
// bottom up μ—°μ‚°
26+
for (int i = 1; i <= amount; ++i) {
27+
for (int c : coins) {
28+
if (i >= c) {
29+
dp[i] = min(dp[i], dp[i - c] + 1);
30+
}
31+
}
32+
}
33+
34+
return (dp[amount] > amount) ? -1 : dp[amount];
35+
}
36+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
/*
7+
O(logn)으둜 풀어라
8+
- 이진 탐색 λŠλ‚Œ
9+
- midκ°’κ³Ό 였λ₯Έμͺ½ 끝을 비ꡐ,
10+
- mid > 였λ₯Έμͺ½ 끝 : μ΅œμ†ŒλŠ” 였λ₯Έμͺ½μ—
11+
- mid <= 였λ₯Έμͺ½ 끝 : μ΅œμ†ŒλŠ” mid 포함 μ™Όμͺ½μ—
12+
*/
13+
14+
class Solution {
15+
public:
16+
int findMin(vector<int>& nums) {
17+
int left = 0;
18+
int right = nums.size() - 1;
19+
20+
while (left < right) {
21+
int mid = left + (right - left) / 2;
22+
23+
if (nums[mid] > nums[right]) {
24+
left = mid + 1;
25+
}
26+
else {
27+
right = mid;
28+
}
29+
}
30+
return (nums[left]);
31+
}
32+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <algorithm>
2+
3+
struct TreeNode {
4+
int val;
5+
TreeNode *left;
6+
TreeNode *right;
7+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
};
11+
12+
class Solution {
13+
public:
14+
int maxDepth(TreeNode* root) {
15+
if (!root)
16+
return (0);
17+
return (std::max(maxDepth(root->left), maxDepth(root->right)) + 1);
18+
}
19+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
struct ListNode {
6+
int val;
7+
ListNode *next;
8+
ListNode() : val(0), next(nullptr) {}
9+
ListNode(int x) : val(x), next(nullptr) {}
10+
ListNode(int x, ListNode *next) : val(x), next(next) {}
11+
};
12+
13+
class Solution {
14+
public:
15+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
16+
if (!list1)
17+
return (list2);
18+
if (!list2)
19+
return (list1);
20+
21+
ListNode ans_head = ListNode();
22+
ListNode* tmp = &ans_head;
23+
24+
while (list1 && list2) {
25+
if (list1->val <= list2->val) {
26+
tmp->next = list1;
27+
list1 = list1->next;
28+
tmp = tmp->next;
29+
}
30+
else {
31+
tmp->next = list2;
32+
list2 = list2->next;
33+
tmp = tmp->next;
34+
}
35+
}
36+
if (list1) {
37+
tmp->next = list1;
38+
}
39+
else if (list2) {
40+
tmp->next = list2;
41+
}
42+
return (ans_head.next);
43+
}
44+
};

β€Žword-search/crumbs22.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <vector>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
bool exist(vector<vector<char>>& board, string word) {
9+
// 단어 μ‹œμž‘μ μ„ 탐색, μ‹œμž‘μ  찾으면 dfs μ‹œμž‘ν•˜κ³  λ§ˆμ§€λ§‰ λ‹¨μ–΄κΉŒμ§€ 도달 κ°€λŠ₯ν•  λ•Œ true λ°˜ν™˜
10+
for (int i = 0; i < board.size(); i++) {
11+
for (int j = 0; j < board[0].size(); j++) {
12+
if (board[i][j] == word[0] && dfs(board, i, j, 0, word)) {
13+
return (true);
14+
}
15+
}
16+
}
17+
return (false);
18+
}
19+
bool dfs(vector<vector<char>>& board, int i, int j, int idx, string& word) {
20+
if (idx == word.size())
21+
return (true);
22+
if (i < 0 || i >= board.size() || \
23+
j < 0 || j >= board[0].size() || \
24+
board[i][j] != word[idx]) {
25+
return (false);
26+
}
27+
28+
char tmp = board[i][j];
29+
30+
// λ°©λ¬Έ ν‘œμ‹œ
31+
board[i][j] = '1';
32+
33+
// 4λ°©ν–₯으둜 탐색
34+
bool found = dfs(board, i + 1, j, idx + 1, word) || dfs(board, i - 1, j, idx + 1, word) || \
35+
dfs(board, i, j + 1, idx + 1, word) || dfs(board, i, j - 1, idx + 1, word);
36+
37+
// λ‹€λ₯Έ 경둜 탐색을 μœ„ν•΄μ„œ λ°©λ¬Έ ν‘œμ‹œλ₯Ό μ›λž˜λŒ€λ‘œ 볡원
38+
board[i][j] = tmp;
39+
return (found);
40+
}
41+
};

0 commit comments

Comments
Β (0)