Skip to content

Commit 84ad16d

Browse files
authored
Merge pull request #2309 from ys-han00/main
[ys-han00] WEEK 12 solutions
2 parents bf00a7d + a45acf7 commit 84ad16d

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-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+
static bool comp(const vector<int> &a, const vector<int> &b) {
4+
return (a[1] == b[1] ? a[0] > b[0] : a[1] < b[1]);
5+
}
6+
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
7+
sort(intervals.begin(), intervals.end(), comp);
8+
int ans = 0, prev_end = intervals[0][1], start, end;
9+
for(int i = 1; i < intervals.size(); i++) {
10+
if(prev_end > intervals[i][0])
11+
ans++;
12+
else
13+
prev_end = intervals[i][1];
14+
}
15+
16+
return ans;
17+
}
18+
};
19+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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* first = head;
15+
for(int i = 0; i < n; i++)
16+
first = first->next;
17+
18+
ListNode* dummy = new ListNode(-1, head);
19+
ListNode* second = dummy;
20+
while(first != nullptr) {
21+
first = first->next;
22+
second = second->next;
23+
}
24+
25+
second->next = second->next->next;
26+
return dummy->next;
27+
}
28+
29+
// ListNode* removeNthFromEnd(ListNode* head, int n) {
30+
// stack<ListNode*> sta;
31+
// ListNode* curr = head;
32+
33+
// while(curr) {
34+
// sta.push(curr);
35+
// curr = curr->next;
36+
// }
37+
38+
// ListNode* prev = nullptr;
39+
// while(n > 1) {
40+
// n--;
41+
// prev = sta.top();
42+
// sta.pop();
43+
// }
44+
// sta.pop();
45+
// if(sta.empty())
46+
// return prev;
47+
// else {
48+
// sta.top()->next = prev;
49+
// return head;
50+
// }
51+
// }
52+
};
53+

same-tree/ys-han00.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 ans = true;
15+
void rec(TreeNode* p, TreeNode* q) {
16+
if(p == nullptr && q == nullptr)
17+
return;
18+
else if(p && q && p->val == q->val) {
19+
rec(p->left, q->left);
20+
rec(p->right, q->right);
21+
}
22+
else
23+
ans = false;
24+
}
25+
26+
bool isSameTree(TreeNode* p, TreeNode* q) {
27+
rec(p, q);
28+
return ans;
29+
}
30+
};
31+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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*> que;
16+
string str = "root = [";
17+
18+
if(root != nullptr) {
19+
que.push(root);
20+
str += to_string(root->val);
21+
}
22+
23+
while(!que.empty()) {
24+
TreeNode* curr = que.front();
25+
que.pop();
26+
if(curr->left != nullptr) {
27+
que.push(curr->left);
28+
str += "," + to_string(curr->left->val);
29+
}
30+
else
31+
str += ",null";
32+
33+
if(curr->right != nullptr) {
34+
que.push(curr->right);
35+
str += "," + to_string(curr->right->val);
36+
}
37+
else
38+
str += ",null";
39+
}
40+
str += "]";
41+
return str;
42+
}
43+
44+
// Decodes your encoded data to tree.
45+
TreeNode* deserialize(string data) {
46+
int start = data.find('[') + 1;
47+
int end = data.find_last_of(']');
48+
49+
if (start >= end) return nullptr;
50+
51+
string content = data.substr(start, end - start);
52+
53+
vector<string> nodes;
54+
stringstream ss(content);
55+
string temp;
56+
while (getline(ss, temp, ','))
57+
nodes.push_back(temp);
58+
59+
int idx = 0;
60+
TreeNode* root = new TreeNode(stoi(nodes[idx++]));
61+
queue<TreeNode*> que;
62+
que.push(root);
63+
64+
while(!que.empty() && idx < nodes.size()) {
65+
TreeNode* curr = que.front();
66+
que.pop();
67+
68+
if(nodes[idx] == "null") {
69+
curr->left = nullptr;
70+
idx++;
71+
} else {
72+
curr->left = new TreeNode(stoi(nodes[idx++]));
73+
que.push(curr->left);
74+
}
75+
76+
if(nodes[idx] == "null") {
77+
curr->right = nullptr;
78+
idx++;
79+
} else {
80+
curr->right = new TreeNode(stoi(nodes[idx++]));
81+
que.push(curr->right);
82+
}
83+
}
84+
85+
return root;
86+
}
87+
};
88+
89+
// Your Codec object will be instantiated and called as such:
90+
// Codec ser, deser;
91+
// TreeNode* ans = deser.deserialize(ser.serialize(root));
92+

0 commit comments

Comments
 (0)