File tree Expand file tree Collapse file tree 4 files changed +126
-0
lines changed
non-overlapping-intervals
number-of-connected-components-in-an-undirected-graph
remove-nth-node-from-end-of-list Expand file tree Collapse file tree 4 files changed +126
-0
lines changed 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