Coding Test

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - 76503 : ์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ ์‹œ์ฆŒ2 - ๋ชจ๋‘ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ (์ž๋ฐ” - ์œ„์ƒ์ •๋ ฌ , ์ธ์ ‘๋ฆฌ์ŠคํŠธ , ์ธ์ ‘ ํ–‰๋ ฌ ํ’€์ด ๋ฐ ์˜ค๋ฅ˜ 8๋ฒˆ, 11๋ฒˆ, 17๋ฒˆ ์˜ค๋‹ต๋…ธํŠธ

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2023. 11. 7.

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค / ์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ ์‹œ์ฆŒ2 / ๋ชจ๋‘ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ 

 

https://school.programmers.co.kr/learn/courses/30/lessons/76503

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

๋ฌธ์ œ ์„ค๋ช…


๊ฐ ์ ์— ๊ฐ€์ค‘์น˜๊ฐ€ ๋ถ€์—ฌ๋œ ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋‹น์‹ ์€ ๋‹ค์Œ ์—ฐ์‚ฐ์„ ํ†ตํ•˜์—ฌ, ์ด ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ์ ๋“ค์˜ ๊ฐ€์ค‘์น˜๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

์ž„์˜์˜ ์—ฐ๊ฒฐ๋œ ๋‘ ์ ์„ ๊ณจ๋ผ์„œ ํ•œ์ชฝ์€ 1 ์ฆ๊ฐ€์‹œํ‚ค๊ณ , ๋‹ค๋ฅธ ํ•œ์ชฝ์€ 1 ๊ฐ์†Œ์‹œํ‚ต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ, ๋ชจ๋“  ํŠธ๋ฆฌ๊ฐ€ ์œ„์˜ ํ–‰๋™์„ ํ†ตํ•˜์—ฌ ๋ชจ๋“  ์ ๋“ค์˜ ๊ฐ€์ค‘์น˜๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋‹น์‹ ์€ ์ฃผ์–ด์ง„ ํŠธ๋ฆฌ์— ๋Œ€ํ•ด์„œ ํ•ด๋‹น ์‚ฌํ•ญ์ด ๊ฐ€๋Šฅํ•œ์ง€ ํŒ๋ณ„ํ•˜๊ณ , ๋งŒ์•ฝ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์ตœ์†Œํ•œ์˜ ํ–‰๋™์„ ํ†ตํ•˜์—ฌ ๋ชจ๋“  ์ ๋“ค์˜ ๊ฐ€์ค‘์น˜๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

ํŠธ๋ฆฌ์˜ ๊ฐ ์ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์˜๋ฏธํ•˜๋Š” 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด a์™€ ํŠธ๋ฆฌ์˜ ๊ฐ„์„  ์ •๋ณด๋ฅผ ์˜๋ฏธํ•˜๋Š” edges๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ํ–‰๋™์„ ํ†ตํ•ด ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ์ ๋“ค์˜ ๊ฐ€์ค‘์น˜๋ฅผ 0์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด -1์„, ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์ตœ์†Œ ๋ช‡ ๋ฒˆ๋งŒ์— ๊ฐ€๋Šฅํ•œ์ง€๋ฅผ ์ฐพ์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. (๋งŒ์•ฝ ์ฒ˜์Œ๋ถ€ํ„ฐ ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ์ •์ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ 0์ด๋ผ๋ฉด, 0์„ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.)

์ œํ•œ์‚ฌํ•ญ


a์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 300,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
a์˜ ๋ชจ๋“  ์ˆ˜๋Š” ๊ฐ๊ฐ -1,000,000 ์ด์ƒ 1,000,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
a[i]๋Š” i๋ฒˆ ์ •์ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
edges์˜ ํ–‰์˜ ๊ฐœ์ˆ˜๋Š” (a์˜ ๊ธธ์ด - 1)์ž…๋‹ˆ๋‹ค.
edges์˜ ๊ฐ ํ–‰์€ [u, v] 2๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ์ด๋Š” u๋ฒˆ ์ •์ ๊ณผ v๋ฒˆ ์ •์ ์ด ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
edges๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ๊ทธ๋ž˜ํ”„๋Š” ํ•ญ์ƒ ํŠธ๋ฆฌ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

 

a edges result
[-5,0,2,1,2] [[0,1],[3,4],[2,3],[0,3]] 9
[0,1,0] [[0,1],[1,2]] -1



์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…
์ž…์ถœ๋ ฅ ์˜ˆ #1

๋‹ค์Œ ๊ทธ๋ฆผ์€ ์ฃผ์–ด์ง„ ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ์ •์ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ 0์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

2๋ฒˆ ์ •์ ๊ณผ 3๋ฒˆ ์ •์ ์„ ์„ ํƒํ•˜์—ฌ 2๋ฒˆ ์ •์ ์€ 1 ๊ฐ์†Œ์‹œํ‚ค๊ณ , 3๋ฒˆ ์ •์ ์€ 1 ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค. (2๋ฒˆ ๋ฐ˜๋ณต)
3๋ฒˆ ์ •์ ๊ณผ 4๋ฒˆ ์ •์ ์„ ์„ ํƒํ•˜์—ฌ 4๋ฒˆ ์ •์ ์€ 1 ๊ฐ์†Œ์‹œํ‚ค๊ณ , 3๋ฒˆ ์ •์ ์€ 1 ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค. (2๋ฒˆ ๋ฐ˜๋ณต)
0๋ฒˆ ์ •์ ๊ณผ 3๋ฒˆ ์ •์ ์„ ์„ ํƒํ•˜์—ฌ 3๋ฒˆ ์ •์ ์€ 1 ๊ฐ์†Œ์‹œํ‚ค๊ณ , 0๋ฒˆ ์ •์ ์€ 1 ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค. (5๋ฒˆ ๋ฐ˜๋ณต)
๋ชจ๋“  ์ •์ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ 0์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ ํ–‰๋™ ํšŸ์ˆ˜๋Š” 9๋ฒˆ์ด๋ฏ€๋กœ, 9๋ฅผ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ #2

์ฃผ์–ด์ง„ ํŠธ๋ฆฌ๋Š” ๋ชจ๋“  ์ •์ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ 0์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, -1์„ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 


๋‚˜์˜ ํ’€์ด

 

์ด ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ๋ณด์•˜์„ ๋• DFS๋ฅผ ์จ์„œ ์œ„์ƒ์ •๋ ฌ๋กœ ํ’€์–ด์•ผํ•œ๋‹ค๋Š” ๊ฑธ ์•Œ์•˜๋‹ค. ํ•˜์ง€๋งŒ ๋งค๋ฒˆ ํ’€์ด๊ฐ€ ์ง๊ด€์ ์œผ๋กœ ๋ณด๊ธฐ ์‰ฌ์šด ์ธ์ ‘ํ–‰๋ ฌ์„ ์ผ์–ด์„œ ์ด ๋ฌธ์ œ ๋˜ํ•œ ์ธ์ ‘ํ–‰๋ ฌ๋กœ ํ’€์–ด ๋ฒ„๋ ธ๋‹ค.

 

์ธ์ ‘ํ–‰๋ ฌ ํ’€์ด

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
 
public class makeAllZero2_matrix {
    static long answer = 0;
 
    public static void main(String[] args) {
        int[] a = {-50212};
        int[][] edges = {{01}, {34}, {23}, {03}};
 
        System.out.println(solution(a, edges));
    }
 
    public static long solution(int[] a, int[][] edges) {
        int nodeNum = a.length;
        int[][] graph = new int[nodeNum][nodeNum];
 
        for (int i = 0; i < edges.length; i++) {
            graph[edges[i][0]][edges[i][1]] = 1;
            graph[edges[i][1]][edges[i][0]] = 1;
 
        }
        Stack<Integer> dfs = new Stack<>();
        Stack<Integer> order = new Stack<>();
        Set<Integer> visited = new HashSet<>();
        dfs.push(0);
        visited.add(0);
 
        while (dfs.size() != 0) {
            int nextNode = dfs.pop();
            order.push(nextNode);
            for (int i = 0; i < nodeNum; i++) {
                if (graph[nextNode][i] == 1 && !visited.contains(i)) {
                    dfs.push(i);
                    visited.add(i);
                }
            }
        }
        Set<Integer> visited2 = new HashSet<>();
 
        while (order.size() > 1) {
            int leafNode = order.pop();
            visited2.add(leafNode);
            for (int i = 0; i < nodeNum; i++) {
                if (graph[leafNode][i] == 1 && !visited2.contains(i)) {
                    a[i] += a[leafNode];
                    answer += Math.abs(a[leafNode]);
                }
            }
        }
        if (a[0+ a[order.peek()] == 0) {
            return answer + Math.abs(a[order.peek()]);
        } else {
            return -1;
        }
    }
}
cs

 

 

์ œ์ถœ ๊ฒฐ๊ณผ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค.

 

ํ•˜์ง€๋งŒ ๋ฌธ์ œ์—์„œ ์›ํ•˜๋Š” ์ ์€ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ’€์–ด์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์•„๊ปด์„œ ํ‘ธ๋Š” ๋ฐฉ์‹์„ ์›ํ•œ ๊ฒƒ์ด๋‹ค.

(๊ทธ๋ฆฌ๊ณ  ๊ฐ€์ค‘์น˜๋ฅผ ๋”ํ•ด์„œ ์ €์žฅํ•  long[] ๋˜ํ•œ ๋งŒ๋“ค์–ด์ฃผ์ง€ ์•Š์•„ ํ‹€๋ฆฐ ๋‹ต์ด๊ธฐ๋„ ํ•˜๋‹ค.  long[]์„ ๋งŒ๋“ค์–ด์„œ ๋„ฃ์–ด์ฃผ์–ด ๋ฉ”๋ชจ๋ฆฌ์ œํ•œ์ด ์—†๋Š” ์ œ์ถœ์ง€์— ์ œ์ถœํ•œ๋‹ค๋ฉด ์•„๋งˆ ์ •๋‹ต์ฒ˜๋ฆฌ ๋  ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•œ๋‹ค.)

 

 

๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋กœ ์ธํ•ด ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋กœ ๋‹ค์‹œ ํ’€์—ˆ๋‹ค.

 

์ธ์ ‘๋ฆฌ์ŠคํŠธ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class makeAllZero2_list_answer2 {
    static long answer = 0;
 
    public static void main(String[] args) {
        int[] a = {-50212};
        int[][] edges = {{01}, {34}, {23}, {03}};
 
        System.out.println(solution(a, edges));
    }
 
    public static long solution(int[] a, int[][] edges) {
        int nodeNum = a.length;
        long[] answerArr = new long[nodeNum + 1];
        for (int i = 0; i < nodeNum; i++) {
            answerArr[i] = a[i];
        }
        List<List<Integer>> connection = new ArrayList<>();
 
        for (int i = 0; i <= nodeNum; i++) {
            connection.add(new ArrayList<>());
        }
 
        for (int i = 0; i < nodeNum - 1; i++) {
            connection.get(edges[i][0]).add(edges[i][1]);
            connection.get(edges[i][1]).add(edges[i][0]);
        }
        Stack<Integer> dfs = new Stack<>();
        Stack<Integer> order = new Stack<>();
        Set<Integer> visited = new HashSet<>();
        dfs.push(0);
        visited.add(0);
 
        while (dfs.size() != 0) {
            int nextNode = dfs.pop();
            order.push(nextNode);
            for (int i = 0; i < connection.get(nextNode).size(); i++) {
                if (!visited.contains(connection.get(nextNode).get(i))) {
                    dfs.push(connection.get(nextNode).get(i));
                    visited.add(connection.get(nextNode).get(i));
                }
            }
        }
        Set<Integer> visited2 = new HashSet<>();
 
        while (order.size() > 1) {
            int leafNode = order.pop();
            visited2.add(leafNode);
            for (int i = 0; i < connection.get(leafNode).size(); i++) {
                if (!visited2.contains(connection.get(leafNode).get(i))) {
                    answerArr[connection.get(leafNode).get(i)] += answerArr[leafNode];
                    answer += Math.abs(answerArr[leafNode]);
                }
            }
        }
        if (answerArr[0+ answerArr[order.peek()] == 0) {
            return answer + Math.abs(answerArr[order.peek()]);
        }
        return -1;
    }
}
cs

 

 

์ธ์ ‘ํ–‰๋ ฌ์€ ์ง๊ด€์ ์œผ๋กœ ๋…ธ๋“œ์™€ ๋…ธ๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋œ ๋ชจ์Šต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด ์ข‹์ง€๋งŒ, ์ด์ค‘ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๊ธฐ๋•Œ๋ฌธ์— ๋…ธ๋“œ์˜ ์ˆ˜๊ฐ€ ๋งŽ์•„์ง€๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋œจ๊ธฐ ์‰ฝ์ƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ

์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ํ’€์ด ๋ฒ•์ด ๋” ์•ˆ์ „ํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

๋Œ“๊ธ€