From 982944adf2165327985bb2b3a1193f2f2f3b5481 Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Wed, 3 Dec 2025 20:44:45 +0900 Subject: [PATCH] #224 & #227 & #245 & #255 & #269 solutions --- coin-change/ys-han00.cpp | 15 ++++ .../ys-han00.cpp | 52 ++++++++++++++ maximum-depth-of-binary-tree/ys-han00.cpp | 49 +++++++++++++ merge-two-sorted-lists/ys-han00.cpp | 69 +++++++++++++++++++ word-search/ys-han00.cpp | 40 +++++++++++ 5 files changed, 225 insertions(+) create mode 100644 coin-change/ys-han00.cpp create mode 100644 find-minimum-in-rotated-sorted-array/ys-han00.cpp create mode 100644 maximum-depth-of-binary-tree/ys-han00.cpp create mode 100644 merge-two-sorted-lists/ys-han00.cpp create mode 100644 word-search/ys-han00.cpp diff --git a/coin-change/ys-han00.cpp b/coin-change/ys-han00.cpp new file mode 100644 index 0000000000..db1860fb93 --- /dev/null +++ b/coin-change/ys-han00.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int coinChange(vector& coins, int amount) { + + vector dp(amount + 1, 0xfffffff); + dp[0] = 0; + + for(int i = 0; i < coins.size(); i++) + for(int j = coins[i]; j <= amount; j++) + dp[j] = min(dp[j], dp[j - coins[i]] + 1); + + return (dp[amount] == 0xfffffff ? -1 : dp[amount]); + } +}; + diff --git a/find-minimum-in-rotated-sorted-array/ys-han00.cpp b/find-minimum-in-rotated-sorted-array/ys-han00.cpp new file mode 100644 index 0000000000..b3096b2e7d --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/ys-han00.cpp @@ -0,0 +1,52 @@ +// class Solution { +// public: +// int findMin(vector& nums) { +// queue> que; +// int left = 0, right = nums.size() - 1, mid, previous, next; + +// if(nums.size() == 1) +// return nums[0]; +// if(nums.size() == 2) +// return min(nums[0], nums[1]); + +// que.push({left, right}); +// while(!que.empty()) { +// left = que.front().first; +// right = que.front().second; +// que.pop(); + +// mid = (left + right) / 2; +// previous = (mid == 0 ? nums.size() - 1 : mid - 1); +// next = (mid + 1 == nums.size() ? 0 : mid + 1); + +// if(nums[previous] > nums[mid] && nums[mid] < nums[next]) +// return nums[mid]; + +// if(left <= mid - 1) +// que.push({left, mid - 1}); +// if(mid + 1 <= right) +// que.push({mid + 1, right}); +// } + +// return -1; +// } +// }; + +class Solution { +public: + int findMin(vector& nums) { + int left = 0, right = nums.size() - 1, mid; + + while(left < right) { + mid = (left + right) / 2; + + if(nums[right] < nums[mid]) + left = mid + 1; + else + right = mid; + } + + return nums[left]; + } +}; + diff --git a/maximum-depth-of-binary-tree/ys-han00.cpp b/maximum-depth-of-binary-tree/ys-han00.cpp new file mode 100644 index 0000000000..75ae3f455d --- /dev/null +++ b/maximum-depth-of-binary-tree/ys-han00.cpp @@ -0,0 +1,49 @@ +/** + * Definition for a binary tree node. + * 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 == nullptr) +// return 0; + +// int ans = -1, depth; +// queue> que; + +// que.push({1, root}); +// while(!que.empty()) { +// depth = que.front().first; +// TreeNode* Node = que.front().second; +// que.pop(); + +// if(!Node->left && !Node->right) { +// ans = max(ans, depth); +// continue; +// } +// if(Node->left) +// que.push({depth + 1, Node->left}); +// if(Node->right) +// que.push({depth + 1, Node->right}); +// } + +// return ans; +// } +// }; + +class Solution { +public: + int maxDepth(TreeNode* root) { + if(!root) + return 0; + return 1 + max(maxDepth(root->left), maxDepth(root->right)); + } +}; + diff --git a/merge-two-sorted-lists/ys-han00.cpp b/merge-two-sorted-lists/ys-han00.cpp new file mode 100644 index 0000000000..6a3c513677 --- /dev/null +++ b/merge-two-sorted-lists/ys-han00.cpp @@ -0,0 +1,69 @@ +/** + * Definition for singly-linked list. + * 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) { +// vector nums; + +// while(list1) { +// nums.push_back(list1->val); +// list1 = list1->next; +// } +// while(list2) { +// nums.push_back(list2->val); +// list2 = list2->next; +// } + +// if (nums.empty()) { +// return nullptr; +// } + +// sort(nums.begin(), nums.end()); +// ListNode* head = new ListNode(nums[0]); +// ListNode* current = head; +// for(int i = 1; i < nums.size(); i++) { +// current->next = new ListNode(nums[i]); +// current = current->next; +// } + +// return head; +// } +// }; + +class Solution { +public: + ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { + ListNode* dummy = new ListNode(0); + ListNode* current = dummy; + + while(list1 && list2) { + if(list1->val < list2->val) { + current->next = list1; + list1 = list1->next; + } else { + current->next = list2; + list2 = list2->next; + } + current = current->next; + } + + if(list1) + current->next = list1; + else + current->next = list2; + + ListNode* head = dummy->next; + delete dummy; + + return head; + } +}; + diff --git a/word-search/ys-han00.cpp b/word-search/ys-han00.cpp new file mode 100644 index 0000000000..a6405c569c --- /dev/null +++ b/word-search/ys-han00.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + int m, n; + int dx[4] = {-1, 0, 1, 0}; + int dy[4] = {0, 1, 0, -1}; + + bool exist(vector>& board, string word) { + m = board.size(); + n = board[0].size(); + + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) + if (dfs(board, word, i, j, 0)) + return true; + + return false; + } + + bool dfs(vector>& board, string& word, int x, int y, int idx) { + if (idx == word.size()) return true; + + if (x < 0 || y < 0 || x >= m || y >= n || board[x][y] != word[idx]) + return false; + + char temp = board[x][y]; + board[x][y] = '*'; + + bool found = false; + for (int k = 0; k < 4; k++) + if (dfs(board, word, x + dx[k], y + dy[k], idx + 1)) { + found = true; + break; + } + + board[x][y] = temp; + + return found; + } +}; +