diff --git "a/leetcode3/\353\202\250\355\232\250\354\240\225/3701. Compute Alternating Sum.py" "b/leetcode3/\353\202\250\355\232\250\354\240\225/3701. Compute Alternating Sum.py" new file mode 100644 index 00000000..6546a2d0 --- /dev/null +++ "b/leetcode3/\353\202\250\355\232\250\354\240\225/3701. Compute Alternating Sum.py" @@ -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 \ No newline at end of file diff --git "a/leetcode3/\353\202\250\355\232\250\354\240\225/785. Is Graph Bipartite.py" "b/leetcode3/\353\202\250\355\232\250\354\240\225/785. Is Graph Bipartite.py" new file mode 100644 index 00000000..f962c325 --- /dev/null +++ "b/leetcode3/\353\202\250\355\232\250\354\240\225/785. Is Graph Bipartite.py" @@ -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 +