diff --git "a/leetcode3/\354\227\274\355\230\234\354\240\225/3701. Compute Alternating Sum.java" "b/leetcode3/\354\227\274\355\230\234\354\240\225/3701. Compute Alternating Sum.java" new file mode 100644 index 00000000..54daa7e1 --- /dev/null +++ "b/leetcode3/\354\227\274\355\230\234\354\240\225/3701. Compute Alternating Sum.java" @@ -0,0 +1,13 @@ +// 짝수는 더하고 홀수는 빼기 +// o(n) + +class Solution { + public int alternatingSum(int[] nums) { + int sum = 0; + for (int i = 0; i queue = new LinkedList<>(); + queue.offer(i); + color[i] = 1; + + while (!queue.isEmpty()) { + int node = queue.poll(); + for (int next : graph[node]) { + if (color[next] == color[node]) return false; + else if (color[next] == 0) { + color[next] = -color[node]; + queue.offer(next); + } + } + } + } + return true; + } +}