Notice
Recent Posts
Recent Comments
Link
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„ํด๋ฆฌ ์ฑŒ๋ฆฐ์ง€ 6์ฃผ์ฐจ (javascript ํ’€์ด) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„ํด๋ฆฌ ์ฑŒ๋ฆฐ์ง€ 6์ฃผ์ฐจ (javascript ํ’€์ด)

Lennon 2021. 9. 25. 17:42
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/85002?language=javascript 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - 6์ฃผ์ฐจ_๋ณต์„œ ์ •๋ ฌํ•˜๊ธฐ

๋ณต์„œ ์„ ์ˆ˜๋“ค์˜ ๋ชธ๋ฌด๊ฒŒ weights์™€, ๋ณต์„œ ์„ ์ˆ˜๋“ค์˜ ์ „์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” head2head๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ณต์„œ ์„ ์ˆ˜๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ •๋ ฌํ•œ ํ›„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”

programmers.co.kr

 

 

function solution(weights, head2head) {
    let winCount = 0;
    let loseCount = 0;
    let heavyCount = 0;
    let answer = [];
    for(let i = 0; i < head2head.length; i++){
        for(let j = 0; j < head2head[i].length; j++){
            let res = weights[i];
            if(head2head[i][j] === "W"){
                winCount++;
                if(res < weights[j]) heavyCount++;
            }
            else if(head2head[i][j] === "L") loseCount++;
        }
        if(loseCount+winCount === 0){
            answer.push({W : 0, Up : heavyCount, Weight: weights[i], Number : i+1});
        } 
        else answer.push({W : winCount / (loseCount+winCount), Up : heavyCount, Weight: weights[i], Number : i+1});
        winCount = 0;
        heavyCount = 0;
        loseCount = 0;
    }
    answer.sort((a,b) => {
        if(b.W !== a.W) return b.W - a.W;
        else if(b.W === a.W && b.Up === a.Up && a.Weight === b.Wight) return a.Weight - b.Weight;
        else if(b.W === a.W && b.Up === a.Up ) return b.Weight - a.Weight;
        else if(b.W === a.W) return b.Up - a.Up;
    })
    // console.log(answer);
    return answer.reduce((cur,arr) => {
        return cur.concat(arr.Number);    
    }, []);
}

 

์ •๋ ฌ์ด ์ค‘์š”ํ•œ ๋ฌธ์ œ์˜€๋‹ค!

728x90
๋ฐ˜์‘ํ˜•
Comments