ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Codewars] 6kyu Are they the "same"? (JAVA)
    IT/개발 기록 2019. 1. 23. 00:18

    문제


    Given two arrays a and b write a function comp(a, b) (compSame(a, b) in Clojure) that checks whether the two arrays have the "same" elements, with the same multiplicities. "Same" means, here, that the elements in b are the elements in a squared, regardless of the order.

     
     
     

    예제


    Valid arrays
    a = [121, 144, 19, 161, 19, 144, 19, 11]  b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]

    comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares:
    a = [121, 144, 19, 161, 19, 144, 19, 11] b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]

    Invalid arrays
    If we change the first number to something else, comp may not return true anymore:
    a = [121, 144, 19, 161, 19, 144, 19, 11]  b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]

    comp(a,b) returns false because in b 132 is not the square of any number of a.
    a = [121, 144, 19, 161, 19, 144, 19, 11]  b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361]

    comp(a,b) returns false because in b 36100 is not the square of any number of a.

     

     

     

    Remark


    a or b might be [] (all languages except R, Shell). a or b might be nil or null or None or nothing (except in Haskell, Elixir, C++, Rust, R, Shell).
    If a or b are nil (or null or None), the problem doesn't make sense so return false.
    If a or b are empty the result is evident by itself.


    코드1


    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
    public static boolean comp(int[] a, int[] b) {
        boolean output = true;
        
        if (a == null || b == null) {
            return false;
        }
        if (a.length == 0 && b.length == 0) {
            return true;
        } else if (a.length == 0 || b.length == 0) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            int num = a[i] * a[i];
            for (int j = 0; j < b.length; j++) {
                if (b[j] == num) {
                    b[j] = -1;
                    output = true;
                    break;
                } else {
                    output = false;
                }
            }
            if (output == false) {
                return output;
            }
        }
        return output;
    }
    cs




    생각해 볼 부분


     이번 문제의 핵심은 예외처리를 잘해야 한다.

    입력되는 배열의 사이즈가 0이거나 null일 때 확실한 예외처리를 해주어야한다.

    그리고 또 한가지는

     

    b[j] = -1;

     

    이 부분, a배열에서 제곱된 값을 b배열에서 찾을 때 

    만약, 찾는다면 그 찾은 값을 -1로 치환해주어 이미 찾은 값은 더 이상 찾지 않게 하기 위함이다.

     

    문제를 풀 때 정확한 답을 제출하기 위해

     

    1. 예외조건 명시

    2. 풀이법을 중간에 자주 변경하지 않기

     

    꼭 생각하면서 풀자

     

     

     

     

     

    댓글