알고리즘

완전탐색 (Brute-Force)

해은 2020. 10. 26. 00:31

완전탐색이란

: '모든 경우의 수를 전부 찾아서 답을 찾는 알고리즘' 을 뜻한다. 영어로는 Exhaustive Search 라고 한다. 가능한 모든 경우의 수를 다 해보는 것이다. 알고리즘을 풀때 가장 강력하고 확실한 방법이지만 그만큼 시간이 가장 오래 걸리는 탐색 기법이다

 

완전탐색을 풀기 위한 방법 4가지

1. for 문

2. 순열, 조합

3. 재귀함수

4. 비트마스크

 

예시 문제

 

function solution(answers) {
    const answer = [];

    const solver_01 = [1,2,3,4,5];
    const solver_02 = [2,1,2,3,2,4,2,5];
    const solver_03 = [3,3,1,1,2,2,4,4,5,5];
    const score = [0,0,0];

    //맞춘 정답 개수 세기
    answers.forEach((answer, index)=>{
        if(answer === solver_01[index%5]) score[0]++;
        if(answer === solver_02[index%8]) score[1]++;
        if(answer === solver_03[index%10]) score[2]++;
    })

    //가장 높은 점수를 받은 사람 구하기
    const maxScore = Math.max.apply(null, score);
    score.forEach((element,index) => {
        if(element === maxScore){
            answer.push(index+1)
        }
    });

    return answer;
}

 

 

 

 

'알고리즘' 카테고리의 다른 글

그래프 알고리즘  (0) 2021.01.09
DP(Dynamic Programming) 알고리즘  (0) 2020.12.20
깊이우선탐색(DFS)와 너비우선탐색(BFS)  (0) 2020.11.24
정렬 알고리즘  (0) 2020.11.17
알고리즘 풀 때 참고 할 js 문법들  (0) 2020.05.10