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
11 changes: 11 additions & 0 deletions leetcode3/남효정/3701. Compute Alternating Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def alternatingSum(self, nums: List[int]) -> int:
even, odd = 0, 0

for i in range(len(nums)):
if i % 2 == 0:
even += nums[i]
else:
odd += nums[i]

return even - odd
27 changes: 27 additions & 0 deletions leetcode3/남효정/785. Is Graph Bipartite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 풀이 실패
class Solution:
def isBipartite(self, graph: List[List[int]]) -> bool:
colors = {} # 노드별 색상

def dfs(node, color):
colors[node] = color

for neighbor in graph[node]:
# 인접한 노드가 같은 색상이면 실패
if neighbor in colors:
if colors[neighbor] == color:
return False
# 아직 색칠 안 된 노드면 재귀 호출
else:
if not dfs(neighbor, -color):
return False
return True

# 분리된 그래프 누락하지 않게 전부 순회
for i in range(len(graph)):
if i not in colors:
if not dfs(i, 1):
return False

return True