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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


 

범위를 봤더니 최대 13개의 원소가 들어온다.

13*12*11/6 은 컴퓨터가 무난하게 처리할 수 있는 개수라 생각해서 조합으로 ~

 

- Java

class Solution {
    static int cnt = 0;
    static int K=3;
    static boolean[]V;
    static int[] R;
    static int N;
    public int solution(int[] number) {
        int[] A= new int[K];
        N=number.length;
        R=number.clone();
        Combi(A,0,0);
        return cnt;
    }
    
    static void Combi(int[] a, int loc, int from) {
        if (loc == K) {
            Eval(a); return;
        }
        for (int i=from; i<N; i++) {
            a[loc] =i;
            Combi(a, loc+1, i+1);
        }
    }
    
    static void Eval(int[]a){
        if(R[a[0]]+R[a[1]]+R[a[2]]==0)
            cnt++;
    }
}

 

- Ruby

루비에는 그냥 배열을 조합으로 만들어주는 모듈이 존재한다.

ㅠㅠ...

def solution(number)
    answer=0
    number.combination(3){
        |c|
        if c.sum==0
            answer=answer+1
        end
    }
    return answer
end

+ Recent posts