Skip to content

[crumbs22] Week 12 Solutions #1601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions non-overlapping-intervals/crumbs22.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지울 개수를 구하는게 아니라 선택할 개수를 구해서 빼는 방법도 있었네요

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {

// 끝나는 시간을 기준으로 정렬
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) { return a[1] < b[1]; });

int count = 1; // 첫 구간은 항상 선택
int end = intervals[0][1]; // 첫 구간의 끝

for (int i = 1; i < intervals.size(); i++) {
// 현재 구간이 겹치지 않으면 선택
if (intervals[i][0] >= end) {
count++;
end = intervals[i][1];
}
}

// 제거해야 하는 구간 수 = 전체 - 선택된 개수
return intervals.size() - count;
}
};
35 changes: 35 additions & 0 deletions number-of-connected-components-in-an-undirected-graph/crumbs22.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

풀이는 저와 비슷한데 훨씬 깔끔한 것 같습니다~

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*

*/
class Solution {
public:
int countComponents(int n, vector<vector<int>> &edges) {
vector<vector<int>> graph(n);
vector<bool> visited(n, false);

for (auto edge : edges) {
int u = edge[0];
int v = edge[1];
graph[u].push_back(v);
graph[v].push_back(u);
}

int cnt = 0;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
dfs(i, graph, visited);
cnt++;
}
}
return cnt;
}

void dfs(int node, vector<vector<int>> &graph, vector<bool> &visited) {
visited[node] = true;
for (int neighbor : graph[node]) {
if (!visited[neighbor]) {
dfs(neighbor, graph, visited);
}
}
}
};
35 changes: 35 additions & 0 deletions remove-nth-node-from-end-of-list/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
노드의 개수 cnt를 세고, n과 cnt를 통해 움직여야 할 위치 mv를 계산한다
mv가 0일 때 첫번째 노드를 제거하므로 head->next를 반환
mv가 0이 아닐 때는 head부터 mv만큼 이동한 후, 그 노드의 이전 노드(fh)와 다음 노드(tmp->next)를 연결한다
시간복잡도는 O(n)이고 추가적인 공간은 사용하지 않으므로 공간복잡도는 O(1)이다
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {

int cnt = 0;
ListNode* end = head;
while (end) {
end = end->next;
cnt++;
}

int mv = cnt - n;
if (mv == 0)
return (head->next);
ListNode* fh = nullptr;
ListNode* tmp = head;
for (int i = 0; i < mv; i++) {
fh = tmp;
tmp = tmp->next;
}
if (!fh)
return (nullptr);
if (tmp->next)
fh->next = tmp->next;
else
fh->next = nullptr;
return (head);
}
};
24 changes: 24 additions & 0 deletions same-tree/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
전위순회 하면서 두 트리가 같은지 비교
재귀적으로 탐색하므로
두 트리의 자식노드 중 하나라도 없다면 탈출한다
p->val == q->val 조건이 아닌 p->val != q->val 조건을 판단해야
p->val과 q->val이 같을 때 그 다음 자식노드로 내려가는 return문으로 빠질 수 있다
(p->val == q->val 조건을 사용하면 true가 반환되므로 중간에 종결된다)
시간복잡도는 트리의 높이와 같다
다른 추가적 공간은 사용하지 않으므로 공간복잡도는 O(1)이다
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {

if (!p && !q)
return (true);
if (!p || !q)
return (false);
if (p->val != q->val)
return (false);

return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
}
};
35 changes: 35 additions & 0 deletions serialize-and-deserialize-binary-tree/crumbs22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Codec {
public:
string serialize(TreeNode* root) {
string res;
dfs(root, res);
return res;
}

TreeNode* deserialize(string data) {
istringstream iss(data);
return dfs(iss);
}

private:
void dfs(TreeNode* node, string& res) {
if (!node) {
res += "N,";
return;
}
res += to_string(node->val) + ",";
dfs(node->left, res);
dfs(node->right, res);
}

TreeNode* dfs(istringstream& iss) {
string val;
getline(iss, val, ','); // ','를 기준으로 하나씩 읽음
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

미리 저장해놓는게 아니라 함수 호출 마다 해당하는 string만 쓰면 추가 배열없이 쓸 수 있군요~

if (val == "N") return nullptr;

TreeNode* node = new TreeNode(stoi(val));
node->left = dfs(iss);
node->right = dfs(iss);
return node;
}
};