File tree Expand file tree Collapse file tree
non-overlapping-intervals
number-of-connected-components-in-an-undirected-graph
remove-nth-node-from-end-of-list Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ '''
2+ Time Complexity: O(n log n)
3+ - intervals 배열을 정렬하는데 소요되는 시간 (n은 intervals의 길이)
4+ - 배열을 순회하며 겹치는 구간을 찾는데 소요되는 시간 O(n)이나 전체 시간 복잡도에 영향을 주지 않음
5+
6+ # Space Complexity: O(1)
7+ - last_end, count 변수를 사용하는데 필요한 공간
8+ '''
9+
10+
11+ class Solution :
12+ def eraseOverlapIntervals (self , intervals : List [List [int ]]) -> int :
13+ intervals .sort (key = lambda x : x [1 ])
14+ last_end = intervals [0 ][1 ]
15+ count = 0
16+
17+ for val in intervals [1 :]:
18+ if (val [0 ] < last_end ):
19+ print (val )
20+ count += 1
21+ if (val [0 ] >= last_end ):
22+ last_end = val [1 ]
23+ return count
Original file line number Diff line number Diff line change 1+ '''
2+ Time Complexity: O(N + E)
3+ - 노드의 개수 N, 간선의 개수 E
4+ - 노드의 개수와 간선의 수를 합친만큼 재귀 함수 호출 필요
5+
6+ Space Complexity: O(N + E)
7+ - 인접 리스트의 크기가 노드와 간선의 수의 합에 비례
8+ - 집합에 최대 N개의 숫자 저장
9+ '''
10+ from typing import (
11+ List ,
12+ )
13+
14+ class Solution :
15+ """
16+ @param n: the number of vertices
17+ @param edges: the edges of undirected graph
18+ @return: the number of connected components
19+ """
20+ def count_components (self , n : int , edges : List [List [int ]]) -> int :
21+ # 연결관계를 표현하기 위한 그래프
22+ graph = [[] for _ in range (n )]
23+
24+ for a ,b in edges :
25+ graph [a ].append (b )
26+ graph [b ].append (a )
27+
28+ # 깊은탐색에서 노드 방문여부 표기
29+ visited = set ()
30+
31+ def dfs (node ):
32+ # 재귀함수 실행시 방문 처리
33+ visited .add (node )
34+ for adj in graph [node ]:
35+ if adj not in visited :
36+ dfs (adj )
37+
38+ count = 0
39+ for node in range (n ):
40+ if node not in visited :
41+ dfs (node )
42+ count += 1
43+ return count
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def removeNthFromEnd (self , head : ListNode , n : int ) -> ListNode :
3+
4+ def remove_node (node ):
5+ if node is None :
6+ return 0
7+ # 2. 재귀 호출
8+ count = remove_node (node .next ) + 1
9+ # 4. 삭제 로직 (count가 n+1일 때)
10+ if count == n + 1 :
11+ node .next = node .next .next
12+ # 5. 현재 count 반환
13+ return count
14+
15+ # 재귀 함수 호출 시작
16+ total_count = remove_node (head )
17+
18+ if total_count == n :
19+ return head .next
20+ return head
21+
22+
23+ # Two Pointer Solution
24+ # One loop
25+
26+ class Solution :
27+ def removeNthFromEnd (self , head : ListNode , n : int ) -> ListNode :
28+ first = head
29+ # 삭제할 노드의 바로 전 노드에 도착
30+ for _ in range (n + 1 ):
31+ first = first .next
32+
33+ dummy = ListNode (None , head )
34+ second = dummy
35+
36+ while first :
37+ first = first .next
38+ second = second .next
39+
40+ second .next = second .next .next
41+
42+ return dummy .next
Original file line number Diff line number Diff line change 1+ # Definition for a binary tree node.
2+ # class TreeNode:
3+ # def __init__(self, val=0, left=None, right=None):
4+ # self.val = val
5+ # self.left = left
6+ # self.right = right
7+ class Solution :
8+ def isSameTree (self , p : Optional [TreeNode ], q : Optional [TreeNode ]) -> bool :
9+ if p == None and q == None :
10+ return True
11+
12+ if p == None or q == None :
13+ return False
14+
15+ if p .val != q .val :
16+ return False
17+
18+ return self .isSameTree (p .left , q .left ) and self .isSameTree (p .right ,q .right )
You can’t perform that action at this time.
0 commit comments