본문 바로가기

알고리즘/HackerRank

[HackerRank] Organizing Containers of Balls(Java)

www.hackerrank.com/challenges/organizing-containers-of-balls/problem

 

Organizing Containers of Balls | HackerRank

Determine if David can perform some sequence of swap operations such that each container holds one distinct type of ball.

www.hackerrank.com

 

## 접근방법


이 문제는, 예제를 그리다가 아이디어가 쉽게 떠올랐다. 아이디어가 말로 표현하기 편해서 글을 쓰게 되었다.

 

 

 

이 문제의 목적은, 컨테이너에 한 가지의 공으로 가득채워야 한다. 어떻게 풀것인가? 과정을 생각하려 하지말고, 결과에 초점을 맞춰보자. 정답도 가능과 불가능으로 문자열을 리턴하면 된다.

 

 

 

컨테이너를 한 가지의 공으로 가득채우려면, 각 컨테이너는 어떤 한 가지의 공을 모두 수용할 수 있는 자리가 있어야 한다.

 

 

 

컨테이너마다 자리의 개수를 파악하고, 모든 종류의 공의 개수를 파악하면 된다. 모든 종류의 공의 개수를 확인하며 컨테이너에도 동일한 자리의 개수가 존재하면 한 가지의 공으로 가득 채울 수 있다. 그렇지 않으면, 채우지 못한다.

 

 

 

위의 글을 이해했다면, 코드를 작성할 수 있다.

 

 

## 해설코드


 

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
 
public class Solution {
 
    // Complete the organizingContainers function below.
    static String organizingContainers(int[][] container) {
        HashMap<Integer, Integer> hm = new HashMap<>();
        int[] ballCnt = new int[container.length];
        
        for(int i = 0; i < container.length; i++){
            int val = 0;
            
            for(int j = 0; j < container.length; j++){
                val += container[i][j];
            }
            
            if(hm.containsKey(val)){
                hm.put(val, hm.get(val) + 1);
            }else{
                hm.put(val, 1);
            }
        }
        
        for(int i = 0; i < container.length; i++){
            int val = 0;
            
            for(int j = 0; j < container.length; j++){
                val += container[j][i];
            }
            
            if(hm.containsKey(val)){
                if(hm.get(val) == 0)
                    return "Impossible";
                    
                hm.put(val, hm.get(val) - 1);
            }else{
                return "Impossible";
            }
        }
        
        return "Possible";
    }
 
    private static final Scanner scanner = new Scanner(System.in);
 
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
 
        int q = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        for (int qItr = 0; qItr < q; qItr++) {
            int n = scanner.nextInt();
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
            int[][] container = new int[n][n];
 
            for (int i = 0; i < n; i++) {
                String[] containerRowItems = scanner.nextLine().split(" ");
                scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
                for (int j = 0; j < n; j++) {
                    int containerItem = Integer.parseInt(containerRowItems[j]);
                    container[i][j] = containerItem;
                }
            }
 
            String result = organizingContainers(container);
 
            bufferedWriter.write(result);
            bufferedWriter.newLine();
        }
 
        bufferedWriter.close();
 
        scanner.close();
    }
}