diff --git a/non-overlapping-intervals/hwi-middle.cpp b/non-overlapping-intervals/hwi-middle.cpp new file mode 100644 index 0000000000..ee517d5a57 --- /dev/null +++ b/non-overlapping-intervals/hwi-middle.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int eraseOverlapIntervals(vector>& intervals) { + sort(intervals.begin(), intervals.end(), [](vector& a, vector& b) { + return a[1] < b[1]; + }); + + int ans = 0; + int k = INT_MIN; + + for (int i = 0; i < intervals.size(); i++) + { + int s = intervals[i][0]; + int e = intervals[i][1]; + + if (s >= k) + { + k = e; + } + else + { + ans++; + } + } + + return ans; + } +}; diff --git a/number-of-connected-components-in-an-undirected-graph/hwi-middle.cpp b/number-of-connected-components-in-an-undirected-graph/hwi-middle.cpp new file mode 100644 index 0000000000..c05a854675 --- /dev/null +++ b/number-of-connected-components-in-an-undirected-graph/hwi-middle.cpp @@ -0,0 +1,51 @@ +class Solution { +public: + int countComponents(int n, vector>& edges) { + int components = n; + vector p(n, -1); + for (auto& e : edges) + { + if (uni(p, e[0], e[1])) // 이 엣지를 통해 합쳐지면 + { + components--; // 컴포넌트 수는 하나 감소 + } + } + + return components; + } + + // union-find + int find(vector&p, int x) + { + if(p[x] < 0) + { + return x; + } + + return p[x] = find(p, p[x]); + } + + bool uni(vector&p, int u, int v) + { + u = find(p, u); + v = find(p, v); + + if (u == v) + { + return false; + } + + if (p[v] < p[u]) + { + swap(u, v); + } + + if (p[u] == p[v]) + { + p[u]--; + } + + p[v] = u; + return true; + } +}; diff --git a/remove-nth-node-from-end-of-list/hwi-middle.cpp b/remove-nth-node-from-end-of-list/hwi-middle.cpp new file mode 100644 index 0000000000..609ca9b05b --- /dev/null +++ b/remove-nth-node-from-end-of-list/hwi-middle.cpp @@ -0,0 +1,40 @@ +/** + * 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) { + // 스택에 넣고 뒤집어서 n번째 노드 찾기 + stack s; + ListNode* cur = head; + while (cur != nullptr) + { + s.push(cur); + cur = cur->next; + } + + for (int i = 0; i < n; ++i) + { + cur = s.top(); + s.pop(); + } + + // 스택이 빌 때 까지 pop한 경우는 head를 제거하는 경우 + if (s.empty()) + { + return head->next; + } + + // 그 외에는 prev가 존재 + ListNode* prev = s.top(); + prev->next = cur->next; + return head; + } +}; diff --git a/same-tree/hwi-middle.cpp b/same-tree/hwi-middle.cpp new file mode 100644 index 0000000000..bb63fbb074 --- /dev/null +++ b/same-tree/hwi-middle.cpp @@ -0,0 +1,28 @@ +/** + * 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 isSameTree(TreeNode* p, TreeNode* q) { + if (p == nullptr || q == nullptr) + { + return p == q; + } + + bool l = isSameTree(p->left, q->left); + bool r = isSameTree(p->right, q->right); + bool cur = p->val == q->val; + + return l && r && cur; + } +}; diff --git a/serialize-and-deserialize-binary-tree/hwi-middle.cpp b/serialize-and-deserialize-binary-tree/hwi-middle.cpp new file mode 100644 index 0000000000..fa8ba544bd --- /dev/null +++ b/serialize-and-deserialize-binary-tree/hwi-middle.cpp @@ -0,0 +1,116 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; + */ + + // 그래야할 필요는 없다고 명시하지만, LeetCode 형식과 동일하게 직렬화/역직렬화 구현 +class Codec { +public: + // Encodes a tree to a single string. + string serialize(TreeNode* root) { + vector tokens; + queue q; + q.push(root); + while (!q.empty()) + { + auto node = q.front(); + q.pop(); + if (node == nullptr) + { + tokens.push_back("null"); + continue; + } + + tokens.push_back(to_string(node->val)); + q.push(node->left); + q.push(node->right); + } + + // 굳이 지우지 않아도 역직렬화가 가능 + // 하지만 LeetCode와 동일하게 구현하기 위한 처리 + while (!tokens.empty() && tokens.back() == "null") + { + tokens.pop_back(); + } + + string s = "["; + for (int i = 0; i < tokens.size(); i++) + { + if (i) + { + s += ","; + + } + s += tokens[i]; + } + + s += "]"; + //cout << s; + return s; + } + + // Decodes your encoded data to tree. + TreeNode* deserialize(string data) { + if (data == "[]") + { + return nullptr; + } + + data = data.substr(1, data.size() - 2); // '[', ']' 제거 + stringstream ss(data); + string token; + + queue v; + while (getline(ss, token, ',')) + { + v.push(token); + } + + TreeNode* root = new TreeNode(stoi(v.front())); + v.pop(); + queue q; + q.push(root); + while (!q.empty()) + { + auto cur = q.front(); + q.pop(); + + // 만약 직렬화 과정에서 null을 제거하지 않았다면 이러한 확인은 필요없어짐 + if (v.empty()) + { + break; + } + + string l = v.front(); + v.pop(); + if (l != "null") + { + cur->left = new TreeNode(stoi(l)); + q.push(cur->left); + } + + if (v.empty()) + { + break; + } + + string r = v.front(); + v.pop(); + if (r != "null") + { + cur->right = new TreeNode(stoi(r)); + q.push(cur->right); + } + } + return root; + } +}; + +// Your Codec object will be instantiated and called as such: +// Codec ser, deser; +// TreeNode* ans = deser.deserialize(ser.serialize(root));