From e9688ff79a54cbaa9813a3a50c1d22c2c7f9894b Mon Sep 17 00:00:00 2001 From: crumbs22 Date: Sat, 26 Apr 2025 03:47:28 +0900 Subject: [PATCH 1/7] solve #224 --- merge_two_sorted_lists.cpp | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 merge_two_sorted_lists.cpp diff --git a/merge_two_sorted_lists.cpp b/merge_two_sorted_lists.cpp new file mode 100644 index 000000000..291332e9b --- /dev/null +++ b/merge_two_sorted_lists.cpp @@ -0,0 +1,44 @@ +#include + +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); + } + }; From 26d6d6fa02dc5f279910bc42504b3d126323d99d Mon Sep 17 00:00:00 2001 From: crumbs22 Date: Sat, 26 Apr 2025 03:53:36 +0900 Subject: [PATCH 2/7] fix path #224 --- merge_two_sorted_lists.cpp => merge-two-sorted-lists/crumbs22.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename merge_two_sorted_lists.cpp => merge-two-sorted-lists/crumbs22.cpp (100%) diff --git a/merge_two_sorted_lists.cpp b/merge-two-sorted-lists/crumbs22.cpp similarity index 100% rename from merge_two_sorted_lists.cpp rename to merge-two-sorted-lists/crumbs22.cpp From f83aad5409a6133e54af1d332255d2454e3ecf09 Mon Sep 17 00:00:00 2001 From: vmop899 Date: Sat, 26 Apr 2025 23:25:57 +0900 Subject: [PATCH 3/7] Feat: solve #227 --- maximum-depth-of-binary-tree/crumbs22.cpp | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 maximum-depth-of-binary-tree/crumbs22.cpp diff --git a/maximum-depth-of-binary-tree/crumbs22.cpp b/maximum-depth-of-binary-tree/crumbs22.cpp new file mode 100644 index 000000000..a196d3120 --- /dev/null +++ b/maximum-depth-of-binary-tree/crumbs22.cpp @@ -0,0 +1,25 @@ +#include + +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 (getMaxDepth(root)); + } + + int getMaxDepth(TreeNode* node, int depth = 0) { + if (!node) + return (depth); + return std::max(getMaxDepth(node->left, depth + 1), getMaxDepth(node->right, depth + 1)); + } + }; From a133ead07877e6626c494b967d799c50e599cc96 Mon Sep 17 00:00:00 2001 From: vmop899 Date: Sun, 27 Apr 2025 00:09:59 +0900 Subject: [PATCH 4/7] Feat: solve #245 --- .../crumbs22.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/crumbs22.cpp diff --git a/find-minimum-in-rotated-sorted-array/crumbs22.cpp b/find-minimum-in-rotated-sorted-array/crumbs22.cpp new file mode 100644 index 000000000..7dc865fd3 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/crumbs22.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +/* + O(logn)으로 풀어라 + - 이진 탐색 느낌 + - mid값과 오른쪽 끝을 비교, + - mid > 오른쪽 끝 : 최소는 오른쪽에 + - mid <= 오른쪽 끝 : 최소는 mid 포함 왼쪽에 +*/ + +class Solution { + public: + int findMin(vector& 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]); + } + }; From 42b40d0ad2652de07d8c34436c126dbcab5eb973 Mon Sep 17 00:00:00 2001 From: vmop899 Date: Sun, 27 Apr 2025 03:16:51 +0900 Subject: [PATCH 5/7] REFACTOR: #227 --- maximum-depth-of-binary-tree/crumbs22.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/maximum-depth-of-binary-tree/crumbs22.cpp b/maximum-depth-of-binary-tree/crumbs22.cpp index a196d3120..603b75eae 100644 --- a/maximum-depth-of-binary-tree/crumbs22.cpp +++ b/maximum-depth-of-binary-tree/crumbs22.cpp @@ -14,12 +14,6 @@ class Solution { int maxDepth(TreeNode* root) { if (!root) return (0); - return (getMaxDepth(root)); - } - - int getMaxDepth(TreeNode* node, int depth = 0) { - if (!node) - return (depth); - return std::max(getMaxDepth(node->left, depth + 1), getMaxDepth(node->right, depth + 1)); + return (std::max(maxDepth(root->left), maxDepth(root->right)) + 1); } }; From 7aa8d5f4dc035d3ee9cd9af9016982fee07aea96 Mon Sep 17 00:00:00 2001 From: vmop899 Date: Sun, 27 Apr 2025 03:17:13 +0900 Subject: [PATCH 6/7] Feat: solve #255 --- word-search/crumbs22.cpp | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 word-search/crumbs22.cpp diff --git a/word-search/crumbs22.cpp b/word-search/crumbs22.cpp new file mode 100644 index 000000000..bb0b874c8 --- /dev/null +++ b/word-search/crumbs22.cpp @@ -0,0 +1,41 @@ +#include +#include + +using namespace std; + +class Solution { +public: + bool exist(vector>& 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>& 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); + } +}; From 3b6be2306cc59109f0c77fb33693f3fbe3ab4903 Mon Sep 17 00:00:00 2001 From: vmop899 Date: Sun, 27 Apr 2025 04:32:14 +0900 Subject: [PATCH 7/7] Feat: solve #269 --- coin-change/crumbs22.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 coin-change/crumbs22.cpp diff --git a/coin-change/crumbs22.cpp b/coin-change/crumbs22.cpp new file mode 100644 index 000000000..15f124384 --- /dev/null +++ b/coin-change/crumbs22.cpp @@ -0,0 +1,36 @@ +#include +#include +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& coins, int amount) { + // dp 테이블 초기화 + vector 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]; + } +};