Skip to content

Commit 0e215d8

Browse files
committed
week 12 add comment
1 parent ceac9fb commit 0e215d8

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

non-overlapping-intervals/sungjinwi.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
풀이 :
3+
종료시간을 오름차순으로 정렬
4+
intervals를 순회하면서 시간이 겹치면 현재의 interval을 삭제 (종료시간이 큰 interval이 삭제되는게 더 최소한으로 삭제할 수 있으므로)
5+
겹치지 않으면 lastEnd를 현재 interval의 end로 업데이트
6+
7+
TC : O (N log N)
8+
sort에 n log n의 시간복잡도
9+
10+
SC : O (1)
11+
*/
12+
13+
#include <vector>
14+
#include <algorithm>
15+
#include <limits.h>
16+
17+
using namespace std;
18+
119
class Solution {
220
public:
321
int eraseOverlapIntervals(vector<vector<int>>& intervals) {

number-of-connected-components-in-an-undirected-graph/sungjinwi.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
풀이 :
3+
인접리스트 방식으로 그래프를 저장
4+
visited에 방문한 노드 표시
5+
노드를 순회하면서 방문하지 않은 노드이면 dfs를 통해 방문으로 표시함 -> 연결된 모든 노드 방문 처리
6+
component 개수 + 1
7+
8+
이 로직을 노드 전체에 대해 반복
9+
10+
정점 개수 : V 간선 개수 : E
11+
12+
TC : O (V + E)
13+
14+
SC : O (V + E)
15+
16+
*/
17+
118
#include <vector>
219
using namespace std;
320

remove-nth-node-from-end-of-list/sungjinwi.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*
2+
풀이 :
3+
list를 vector에 저장한 뒤 끝에서 n - 1 번째 노드->next를 n번째 노드->next로 치환
4+
5+
노드 개수 : N
6+
7+
TC : O (N)
8+
9+
SC : O (N)
10+
*/
11+
112
/**
213
* Definition for singly-linked list.
314
* struct ListNode {

same-tree/sungjinwi.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
풀이 :
3+
전위순회로 root 먼저 체크
4+
둘 다 null이면 true, 둘 중 하나만 null이면 false, val이 다르면 false 리턴
5+
left, right 노드에 재귀적으로 함수 호출하고 둘 중 false인 경우 있으면 false
6+
정상적으로 함수 끝에 다르면 true
7+
8+
트리 일치하는 노드 개수 : min (P, Q) = M
9+
M은 P, Q중 하나에 비례
10+
11+
TC : O (M)
12+
13+
SC : O (M)
14+
재귀호출스택 메모리
15+
*/
16+
117
/**
218
* Definition for a binary tree node.
319
* struct TreeNode {

serialize-and-deserialize-binary-tree/sungjinwi.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
/*
2+
풀이 :
3+
- serialize
4+
- BFS로 트리->val을 큐에 넣으면서 string (ex. [1,2,3,null,null,null,null])으로 합친다
5+
- 노드가 없을 때는 null을 string에 넣어서 어떤 부분이 비어있는지를 표시한다
6+
7+
- deserialize
8+
- string을 split 시켜서 문자열 배열로 만들고
9+
- 원래 트리와 순서를 맞추기위해 serialize와 마찬가지로 BFS를 통해 tree를 재구성
10+
- 문자열이 null이 아니면 해당 val을 가지는 노드를 만들어 연결
11+
12+
노드 개수 : N
13+
14+
serialize
15+
TC : O(N)
16+
전체 노드 순회
17+
SC : O(N)
18+
큐 크기는 노드 개수에 비례
19+
20+
deserialize
21+
TC : O(N)
22+
노드 단위로 다시 쪼개서 전체 노드 순회
23+
SC : O(N)
24+
큐 및 split된 문자열 배열 크기는 노드 개수 비례
25+
*/
26+
27+
128
/**
229
* Definition for a binary tree node.
330
* struct TreeNode {
@@ -7,6 +34,11 @@
734
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
835
* };
936
*/
37+
#include <string>
38+
#include <queue>
39+
#include <iostream>
40+
#include <sstream>
41+
using namespace std;
1042
class Codec {
1143
public:
1244

0 commit comments

Comments
 (0)