Skip to content

Commit ceac9fb

Browse files
committed
Week 12 solution
1 parent 922d20b commit ceac9fb

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
4+
5+
sort(intervals.begin(), intervals.end(), [] (vector<int>& a, vector<int>& b){ return a[1] < b[1]; });
6+
7+
int cnt = 0;
8+
int lastEnd = INT_MIN;
9+
10+
for (auto& interval : intervals) {
11+
if (interval[0] < lastEnd)
12+
cnt++;
13+
else
14+
lastEnd = interval[1];
15+
}
16+
17+
return cnt;
18+
}
19+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <vector>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
/**
7+
* @param n: the number of vertices
8+
* @param edges: the edges of undirected graph
9+
* @return: the number of connected components
10+
*/
11+
int countComponents(int n, vector<vector<int>> &edges) {
12+
vector<vector<int>> adjs(n);
13+
vector<bool> visited(n, false);
14+
int result = 0;
15+
16+
for (auto& edge : edges) {
17+
adjs[edge[0]].push_back(edge[1]);
18+
adjs[edge[1]].push_back(edge[0]);
19+
}
20+
21+
for (int i = 0; i < n; i++) {
22+
if (!visited[i]) {
23+
dfs(i, adjs, visited);
24+
result++;
25+
}
26+
}
27+
return result;
28+
}
29+
30+
void dfs(int curr, vector<vector<int>>& adjs, vector<bool>& visited) {
31+
if (visited[curr])
32+
return ;
33+
visited[curr] = true;
34+
for (auto& adj : adjs[curr])
35+
dfs(adj, adjs, visited);
36+
}
37+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* removeNthFromEnd(ListNode* head, int n) {
14+
ListNode* tmp = head;
15+
vector<ListNode*> list;
16+
17+
while (tmp) {
18+
list.push_back(tmp);
19+
tmp = tmp->next;
20+
}
21+
int idx = list.size() - n;
22+
if (idx == 0)
23+
return head->next;
24+
else {
25+
list[idx - 1]->next = list[idx]->next;
26+
return head;
27+
}
28+
}
29+
};

same-tree/sungjinwi.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool isSameTree(TreeNode* p, TreeNode* q) {
15+
if (!p || !q)
16+
return p == q;
17+
if (p->val != q->val)
18+
return false;
19+
if (!isSameTree(p->left, q->left))
20+
return false;
21+
if (!isSameTree(p->right, q->right))
22+
return false;
23+
return true;
24+
}
25+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Codec {
11+
public:
12+
13+
// Encodes a tree to a single string.
14+
string serialize(TreeNode* root) {
15+
queue<TreeNode*> q;
16+
ostringstream out;
17+
18+
out << "[";
19+
if (root)
20+
q.push(root);
21+
while (!q.empty()) {
22+
TreeNode* node = q.front();
23+
if (node == nullptr) {
24+
out << "null";
25+
}
26+
else {
27+
out << node->val;
28+
q.push(node->left);
29+
q.push(node->right);
30+
}
31+
q.pop();
32+
out << ",";
33+
}
34+
35+
string result = out.str();
36+
if (result.back() == ',')
37+
result.pop_back();
38+
result += "]";
39+
40+
return result;
41+
}
42+
43+
// Decodes your encoded data to tree.
44+
TreeNode* deserialize(string data) {
45+
if (data == "[]")
46+
return nullptr;
47+
string s = data.substr(1, data.size() - 2);
48+
vector<string> tokens;
49+
string token;
50+
stringstream ss(s);
51+
52+
while (getline(ss, token, ','))
53+
tokens.push_back(token);
54+
55+
TreeNode* root = new TreeNode(stoi(tokens[0]));
56+
queue<TreeNode*> q;
57+
q.push(root);
58+
59+
int index = 1;
60+
61+
while (!q.empty() && index < tokens.size()) {
62+
TreeNode* node = q.front();
63+
q.pop();
64+
65+
if (tokens[index] != "null") {
66+
node->left = new TreeNode(stoi(tokens[index]));
67+
q.push(node->left);
68+
}
69+
index++;
70+
71+
if (index < tokens.size() && tokens[index] != "null") {
72+
node->right = new TreeNode(stoi(tokens[index]));
73+
q.push(node->right);
74+
}
75+
index++;
76+
}
77+
return root;
78+
}
79+
};
80+
81+
// Your Codec object will be instantiated and called as such:
82+
// Codec ser, deser;
83+
// TreeNode* ans = deser.deserialize(ser.serialize(root));

0 commit comments

Comments
 (0)