Skip to content
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
23 changes: 23 additions & 0 deletions non-overlapping-intervals/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
Time Complexity: O(n log n)
- intervals 배열을 정렬하는데 소요되는 시간 (n은 intervals의 길이)
- 배열을 순회하며 겹치는 구간을 찾는데 소요되는 시간 O(n)이나 전체 시간 복잡도에 영향을 주지 않음

# Space Complexity: O(1)
- last_end, count 변수를 사용하는데 필요한 공간
'''


class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key=lambda x: x[1])
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 시작점을 기준으로 sorting해서 interval이 발생했을때 비교하는 코드가 필요했는데, 작성해주신 것처럼 end 기준으로 할 경우 그 부분이 필요 없겠네요. 배워갑니다!

last_end = intervals[0][1]
count = 0

for val in intervals[1:]:
if (val[0] < last_end):
print(val)
count += 1
if (val[0] >= last_end):
last_end = val[1]
return count
43 changes: 43 additions & 0 deletions number-of-connected-components-in-an-undirected-graph/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'''
Time Complexity: O(N + E)
- 노드의 개수 N, 간선의 개수 E
- 노드의 개수와 간선의 수를 합친만큼 재귀 함수 호출 필요

Space Complexity: O(N + E)
- 인접 리스트의 크기가 노드와 간선의 수의 합에 비례
- 집합에 최대 N개의 숫자 저장
'''
from typing import (
List,
)

class Solution:
"""
@param n: the number of vertices
@param edges: the edges of undirected graph
@return: the number of connected components
"""
def count_components(self, n: int, edges: List[List[int]]) -> int:
# 연결관계를 표현하기 위한 그래프
graph = [[] for _ in range(n)]

for a,b in edges:
graph[a].append(b)
graph[b].append(a)

# 깊은탐색에서 노드 방문여부 표기
visited = set()

def dfs(node):
# 재귀함수 실행시 방문 처리
visited.add(node)
for adj in graph[node]:
if adj not in visited:
dfs(adj)

count = 0
for node in range(n):
if node not in visited:
dfs(node)
count += 1
return count
42 changes: 42 additions & 0 deletions remove-nth-node-from-end-of-list/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:

def remove_node(node):
if node is None:
return 0
# 2. 재귀 호출
count = remove_node(node.next) + 1
# 4. 삭제 로직 (count가 n+1일 때)
if count == n + 1:
node.next = node.next.next
# 5. 현재 count 반환
return count

# 재귀 함수 호출 시작
total_count = remove_node(head)

if total_count == n:
return head.next
return head


# Two Pointer Solution
# One loop

class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
first = head
# 삭제할 노드의 바로 전 노드에 도착
for _ in range(n+1):
first = first.next

dummy = ListNode(None, head)
second = dummy

while first:
first = first.next
second = second.next

second.next = second.next.next

return dummy.next
18 changes: 18 additions & 0 deletions same-tree/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if p == None and q == None:
return True

if p == None or q == None:
return False

if p.val != q.val:
return False

return self.isSameTree(p.left, q.left) and self.isSameTree(p.right,q.right)