Skip to content

Commit 774d101

Browse files
authored
Merge pull request #2476 from hwi-middle/main
[hwi-middle] WEEK 04 solutions
2 parents 0f596fb + 4d795f0 commit 774d101

5 files changed

Lines changed: 207 additions & 0 deletions

File tree

โ€Žcoin-change/hwi-middle.cppโ€Ž

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
int coinChange(vector<int>& coins, int amount) {
4+
if (amount < 1)
5+
{
6+
return 0;
7+
}
8+
9+
vector<int> v(amount);
10+
return solve(coins, amount, v);
11+
}
12+
13+
int solve(vector<int>& coins, int amount, vector<int>& count)
14+
{
15+
if (amount < 0) return -1;
16+
if (amount == 0) return 0;
17+
if (count[amount - 1] != 0) return count[amount - 1];
18+
19+
int min = INT_MAX;
20+
for (int coin : coins)
21+
{
22+
int res = solve(coins, amount - coin, count); // coin์„ ์‚ฌ์šฉ
23+
if (res >= 0 && res < min) // ๊ฒฐ๊ณผ๊ฐ€ ์œ ํšจํ•˜๊ณ  ํ˜„์žฌ ์ตœ์†Ÿ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด ์—…๋ฐ์ดํŠธ
24+
{
25+
min = res + 1; // coin์„ ๋ฏธ๋ฆฌ ์‚ฌ์šฉํ•˜๊ณ  ์ฐพ์€ ๊ฐ’์ด๋ฏ€๋กœ 1 ๋”ํ•จ
26+
}
27+
}
28+
29+
count[amount - 1] = (min == INT_MAX) ? -1 : min;
30+
return count[amount - 1];
31+
}
32+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
int findMin(vector<int>& nums) {
4+
return solve(nums, 0, nums.size() - 1);
5+
}
6+
7+
int solve(vector<int>& num, int start, int end)
8+
{
9+
if (end - start < 2)
10+
{
11+
return min(num[start], num[end]);
12+
}
13+
14+
// ์ค‘๊ฐ„ ์ธ๋ฑ์Šค ๊ณ„์‚ฐ
15+
int mid = (start + end) / 2;
16+
17+
// ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด ๊นจ์ง€๊ธฐ ์‹œ์ž‘ํ•œ ๋ถ€๋ถ„ -> ์ตœ์†Ÿ๊ฐ’ ๋ฐœ๊ฒฌ
18+
if (num[mid] < num[mid - 1])
19+
{
20+
return num[mid];
21+
}
22+
23+
// ์™ผ์ชฝ ์˜์—ญ์ด ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ
24+
if (num[start] <= num[mid])
25+
{
26+
// ๊ทธ๋ฆฌ๊ณ  ์˜ค๋ฅธ์ชฝ ์˜์—ญ๊นŒ์ง€๋„ ์ •๋ ฌ๋œ ๊ฒฝ์šฐ
27+
if (num[mid] <= num[end])
28+
{
29+
// ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ๊ฐ’์ด ์ตœ์†Ÿ๊ฐ’
30+
return num[start];
31+
}
32+
33+
// ์™ผ์ชฝ ์˜์—ญ๋งŒ ์ •๋ ฌ๋œ ๊ฒฝ์šฐ
34+
// ์˜ค๋ฅธ์ชฝ ์˜์—ญ์—์„œ ์ •๋ ฌ์ด ๊นจ์ง€๋Š” ๋ถ€๋ถ„ ์ฐพ์œผ๋Ÿฌ ๊ฐ€๊ธฐ
35+
return solve(num, mid + 1, end);
36+
}
37+
38+
// ์˜ค๋ฅธ์ชฝ ์˜์—ญ์ด ์ •๋ ฌ๋œ ๊ฒฝ์šฐ
39+
// ์™ผ์ชฝ ์˜์—ญ์—์„œ ์ •๋ ฌ์ด ๊นจ์ง€๋Š” ๋ถ€๋ถ„ ์ฐพ์œผ๋Ÿฌ ๊ฐ€๊ธฐ
40+
return solve(num, start, mid - 1);
41+
}
42+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
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+
return solve(root, 0);
16+
}
17+
18+
int solve(TreeNode* root, int depth)
19+
{
20+
if (root == nullptr)
21+
{
22+
return depth;
23+
}
24+
25+
int l = solve(root->left, depth);
26+
int r = solve(root->right, depth);
27+
28+
return max(l, r) + 1;
29+
}
30+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14+
ListNode dummy; // head ์ดˆ๊ธฐํ™”์˜ ๋ณต์žก์„ฑ์„ ๋‚ฎ์ถ”๊ธฐ ์œ„ํ•œ ๋”๋ฏธ๋…ธ๋“œ
15+
ListNode* tail = &dummy;
16+
17+
while (list1 != nullptr && list2 != nullptr)
18+
{
19+
if (list1->val < list2->val)
20+
{
21+
tail->next = list1;
22+
list1 = list1->next;
23+
}
24+
else
25+
{
26+
tail->next = list2;
27+
list2 = list2->next;
28+
}
29+
30+
tail = tail->next;
31+
}
32+
33+
tail->next = (list1 == nullptr) ? list2 : list1;
34+
35+
return dummy.next;
36+
}
37+
}
38+
;

โ€Žword-search/hwi-middle.cppโ€Ž

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Solution {
2+
private:
3+
int row;
4+
int col;
5+
6+
public:
7+
bool exist(vector<vector<char>>& board, string word) {
8+
row = board.size();
9+
col = board[0].size();
10+
for (int r = 0; r < row; ++r)
11+
{
12+
for (int c = 0; c < col; ++c)
13+
{
14+
if (solve(board, word, r, c, 0))
15+
{
16+
return true;
17+
}
18+
}
19+
}
20+
21+
return false;
22+
}
23+
24+
// dfs ํ•จ์ˆ˜
25+
bool solve(vector<vector<char>>& board, string& word, int r, int c, int idx)
26+
{
27+
// ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฒฝ์šฐ
28+
if (r < 0 || r >= row || c < 0 || c >= col)
29+
{
30+
return false;
31+
}
32+
33+
// ๊ธ€์ž๊ฐ€ ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
34+
if (board[r][c] != word[idx])
35+
{
36+
return false;
37+
}
38+
39+
// ๋‹จ์–ด๋ฅผ ์ฐพ์€ ๊ฒฝ์šฐ
40+
if (idx == word.size() - 1)
41+
{
42+
return true;
43+
}
44+
45+
char ch = board[r][c];
46+
board[r][c] = '?'; // ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ์œผ๋กœ ๋Œ์•„์˜ค์ง€ ์•Š๋„๋ก ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค์ง€ ์•Š๋Š” ๋ฌธ์ž๋กœ ์น˜ํ™˜
47+
48+
// ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰
49+
int dr[4] = { 1, 0, -1, 0 };
50+
int dc[4] = { 0, 1, 0, -1 };
51+
52+
for (int dir = 0; dir < 4; ++dir)
53+
{
54+
int nr = r + dr[dir];
55+
int nc = c + dc[dir];
56+
if (solve(board, word, nr, nc, idx + 1))
57+
{
58+
return true;
59+
}
60+
}
61+
62+
board[r][c] = ch; // ์›๋ž˜ ๋ฌธ์ž๋กœ ๋˜๋Œ๋ฆฌ๊ธฐ
63+
return false;
64+
}
65+
};

0 commit comments

Comments
ย (0)