강의

멘토링

커뮤니티

Inflearn Community Q&A

frontendended's profile image
frontendended

asked

Introduction to Javascript Algorithm Problem Solving (Coding Test Preparation)

5. Finding the rank

이렇게 풀어도 괜찮을까요?

Written on

·

264

0

function solution (arr)

{

    let answer = [];

    let count = 1;

    for(i=0; i<arr.length; i++)

    {

        let max = Math.max(...arr);

   

        for(j=0; j<arr.length; j++)

        {

            if(arr[j] === max && arr[j] !== 0){

                answer[j] = count;

                arr[j] = 0;
                if(Math.max(...arr) !== max) count++;                           }    

        }   

}
    return answer;

}  

javascript코딩-테스트

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

위에 코드는 정답이 나오지 않는 코드입니다. 만약

let arr = [90, 90, 80, 80, 70];

이라면 정답은 [1, 1, 3, 3, 5] 가 답입니다.

코드 개선해 보았습니다.

function solution (arr){
    let answer = [];
    let count = 1;
    for(i=0; i<arr.length; i++){
        let max = Math.max(...arr);
        let cnt = 0;
        for(j=0; j<arr.length; j++){
            if(arr[j] === max && arr[j] !== 0){
                answer[j] = count;
                arr[j] = 0;
                cnt++;
            }    
        }  
        count+=cnt;
    }
    return answer;
}
frontendended's profile image
frontendended

asked

Ask a question