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

+36
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+
};
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+
};
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+
};
+44
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

+41
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)