diff --git a/non-overlapping-intervals/ys-han00.cpp b/non-overlapping-intervals/ys-han00.cpp new file mode 100644 index 0000000000..485a849ac3 --- /dev/null +++ b/non-overlapping-intervals/ys-han00.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + static bool comp(const vector &a, const vector &b) { + return (a[1] == b[1] ? a[0] > b[0] : a[1] < b[1]); + } + int eraseOverlapIntervals(vector>& intervals) { + sort(intervals.begin(), intervals.end(), comp); + int ans = 0, prev_end = intervals[0][1], start, end; + for(int i = 1; i < intervals.size(); i++) { + if(prev_end > intervals[i][0]) + ans++; + else + prev_end = intervals[i][1]; + } + + return ans; + } +}; + diff --git a/remove-nth-node-from-end-of-list/ys-han00.cpp b/remove-nth-node-from-end-of-list/ys-han00.cpp new file mode 100644 index 0000000000..120bc95a9a --- /dev/null +++ b/remove-nth-node-from-end-of-list/ys-han00.cpp @@ -0,0 +1,53 @@ +/** + * 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* removeNthFromEnd(ListNode* head, int n) { + ListNode* first = head; + for(int i = 0; i < n; i++) + first = first->next; + + ListNode* dummy = new ListNode(-1, head); + ListNode* second = dummy; + while(first != nullptr) { + first = first->next; + second = second->next; + } + + second->next = second->next->next; + return dummy->next; + } + + // ListNode* removeNthFromEnd(ListNode* head, int n) { + // stack sta; + // ListNode* curr = head; + + // while(curr) { + // sta.push(curr); + // curr = curr->next; + // } + + // ListNode* prev = nullptr; + // while(n > 1) { + // n--; + // prev = sta.top(); + // sta.pop(); + // } + // sta.pop(); + // if(sta.empty()) + // return prev; + // else { + // sta.top()->next = prev; + // return head; + // } + // } +}; + diff --git a/same-tree/ys-han00.cpp b/same-tree/ys-han00.cpp new file mode 100644 index 0000000000..6da6819a08 --- /dev/null +++ b/same-tree/ys-han00.cpp @@ -0,0 +1,31 @@ +/** + * 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: + bool ans = true; + void rec(TreeNode* p, TreeNode* q) { + if(p == nullptr && q == nullptr) + return; + else if(p && q && p->val == q->val) { + rec(p->left, q->left); + rec(p->right, q->right); + } + else + ans = false; + } + + bool isSameTree(TreeNode* p, TreeNode* q) { + rec(p, q); + return ans; + } +}; + diff --git a/serialize-and-deserialize-binary-tree/ys-han00.cpp b/serialize-and-deserialize-binary-tree/ys-han00.cpp new file mode 100644 index 0000000000..519433b8d0 --- /dev/null +++ b/serialize-and-deserialize-binary-tree/ys-han00.cpp @@ -0,0 +1,92 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ +class Codec { +public: + + // Encodes a tree to a single string. + string serialize(TreeNode* root) { + queue que; + string str = "root = ["; + + if(root != nullptr) { + que.push(root); + str += to_string(root->val); + } + + while(!que.empty()) { + TreeNode* curr = que.front(); + que.pop(); + if(curr->left != nullptr) { + que.push(curr->left); + str += "," + to_string(curr->left->val); + } + else + str += ",null"; + + if(curr->right != nullptr) { + que.push(curr->right); + str += "," + to_string(curr->right->val); + } + else + str += ",null"; + } + str += "]"; + return str; + } + + // Decodes your encoded data to tree. + TreeNode* deserialize(string data) { + int start = data.find('[') + 1; + int end = data.find_last_of(']'); + + if (start >= end) return nullptr; + + string content = data.substr(start, end - start); + + vector nodes; + stringstream ss(content); + string temp; + while (getline(ss, temp, ',')) + nodes.push_back(temp); + + int idx = 0; + TreeNode* root = new TreeNode(stoi(nodes[idx++])); + queue que; + que.push(root); + + while(!que.empty() && idx < nodes.size()) { + TreeNode* curr = que.front(); + que.pop(); + + if(nodes[idx] == "null") { + curr->left = nullptr; + idx++; + } else { + curr->left = new TreeNode(stoi(nodes[idx++])); + que.push(curr->left); + } + + if(nodes[idx] == "null") { + curr->right = nullptr; + idx++; + } else { + curr->right = new TreeNode(stoi(nodes[idx++])); + que.push(curr->right); + } + } + + return root; + } +}; + +// Your Codec object will be instantiated and called as such: +// Codec ser, deser; +// TreeNode* ans = deser.deserialize(ser.serialize(root)); +