From df3f9b1fac8cccf3362e79fa1576336adff9f52a Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Thu, 15 Jan 2026 01:03:38 +0900 Subject: [PATCH] Solutions #226 & #246 & #261 & #276 & #286 --- course-schedule/ys-han00.cpp | 38 ++++++++++++ invert-binary-tree/ys-han00.cpp | 33 ++++++++++ jump-game/ys-han00.cpp | 34 +++++++++++ merge-k-sorted-lists/ys-han00.cpp | 68 +++++++++++++++++++++ search-in-rotated-sorted-array/ys-han00.cpp | 29 +++++++++ 5 files changed, 202 insertions(+) create mode 100644 course-schedule/ys-han00.cpp create mode 100644 invert-binary-tree/ys-han00.cpp create mode 100644 jump-game/ys-han00.cpp create mode 100644 merge-k-sorted-lists/ys-han00.cpp create mode 100644 search-in-rotated-sorted-array/ys-han00.cpp diff --git a/course-schedule/ys-han00.cpp b/course-schedule/ys-han00.cpp new file mode 100644 index 0000000000..87e8bcf8a0 --- /dev/null +++ b/course-schedule/ys-han00.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + bool canFinish(int numCourses, vector>& prerequisites) { + int visited = 0; + vector> outdegree(numCourses, vector()); + vector indegree(numCourses, 0); + + for(auto edge : prerequisites) { + outdegree[edge[0]].push_back(edge[1]); + indegree[edge[1]]++; + } + + queue que; + for(int i = 0; i < numCourses; i++) { + if(indegree[i] == 0) { + que.push(i); + visited++; + } + } + + + while(!que.empty()) { + int now = que.front(); + que.pop(); + + for(auto next : outdegree[now]) { + indegree[next]--; + if(indegree[next] == 0) { + visited++; + que.push(next); + } + } + } + + return (visited == numCourses ? true : false); + } +}; + diff --git a/invert-binary-tree/ys-han00.cpp b/invert-binary-tree/ys-han00.cpp new file mode 100644 index 0000000000..857aaffd95 --- /dev/null +++ b/invert-binary-tree/ys-han00.cpp @@ -0,0 +1,33 @@ +/** + * 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: + TreeNode* invertTree(TreeNode* root) { + queue que; + que.push(root); + + while(!que.empty()) { + TreeNode *curr = que.front(); + que.pop(); + + if(!curr) + continue; + + swap(curr->left, curr->right); + que.push(curr->left); + que.push(curr->right); + } + + return root; + } +}; + diff --git a/jump-game/ys-han00.cpp b/jump-game/ys-han00.cpp new file mode 100644 index 0000000000..0eaab55597 --- /dev/null +++ b/jump-game/ys-han00.cpp @@ -0,0 +1,34 @@ +// class Solution { +// public: +// bool canJump(vector& nums) { +// int n = nums.size(); +// vector dp(n); + +// dp[n - 1] = true; +// for(int i = n - 2; i >= 0; i--) { +// for(int j = min(n - 1, nums[i] + i); i <= j; j--) { +// if(dp[j]) { +// dp[i] = true; +// break; +// } +// } +// } + +// return dp[0]; +// } +// }; + +class Solution { +public: + bool canJump(vector& nums) { + int maxPos = 0; + for(int i = 0; i < nums.size(); i++) { + if(maxPos < i) + return false; + maxPos = max(maxPos, i + nums[i]); + } + + return true; + } +}; + diff --git a/merge-k-sorted-lists/ys-han00.cpp b/merge-k-sorted-lists/ys-han00.cpp new file mode 100644 index 0000000000..9a9a8c0870 --- /dev/null +++ b/merge-k-sorted-lists/ys-han00.cpp @@ -0,0 +1,68 @@ +/** + * 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* mergeKLists(vector& lists) { +// vector nums; +// for(ListNode* list : lists) { +// while(list != nullptr) { +// nums.push_back(list->val); +// list = list->next; +// } +// } + +// if(nums.size() == 0) +// return nullptr; + +// sort(nums.begin(), nums.end()); +// ListNode* root = new ListNode(nums[0]); +// ListNode* curr = root; + +// for(int i = 1; i < nums.size(); i++) { +// curr->next = new ListNode(nums[i]); +// curr = curr->next; +// } + +// return root; +// } +// }; + +class Solution { +public: + ListNode* mergeKLists(vector& lists) { + priority_queue< + pair, + vector>, + greater> + > pq; + + for(int i = 0; i < lists.size(); i++) + if(lists[i]) + pq.push({lists[i]->val, i}); + + ListNode dummy(-1); + ListNode* curr = &dummy; + while(!pq.empty()) { + auto [val, idx] = pq.top(); + pq.pop(); + + curr->next = new ListNode(val); + curr = curr->next; + + lists[idx] = lists[idx]->next; + if(lists[idx]) + pq.push({lists[idx]->val, idx}); + } + + return dummy.next; + } +}; + diff --git a/search-in-rotated-sorted-array/ys-han00.cpp b/search-in-rotated-sorted-array/ys-han00.cpp new file mode 100644 index 0000000000..d978f9bd3c --- /dev/null +++ b/search-in-rotated-sorted-array/ys-han00.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int search(vector& nums, int target) { + int left = 0, right = nums.size() - 1, mid; + + while(left <= right) { + mid = (left + right) / 2; + + if(target == nums[mid]) + return mid; + + if(nums[left] <= nums[mid]) { + if(nums[left] <= target && target < nums[mid]) + right = mid - 1; + else + left = mid + 1; + } + else { + if(nums[mid] < target && target <= nums[right]) + left = mid + 1; + else + right = mid - 1; + } + } + + return -1; + } +}; +