ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

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

  1. ์ „์ฒด ์Šน๋ฅ ์ด ๋†’์€ ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์•ž์ชฝ์œผ๋กœ ๊ฐ‘๋‹ˆ๋‹ค. ์•„์ง ๋‹ค๋ฅธ ๋ณต์„œ๋ž‘ ๋ถ™์–ด๋ณธ ์ ์ด ์—†๋Š” ๋ณต์„œ์˜ ์Šน๋ฅ ์€ 0%๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.
  2. ์Šน๋ฅ ์ด ๋™์ผํ•œ ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๋“ค ์ค‘์—์„œ๋Š” ์ž์‹ ๋ณด๋‹ค ๋ชธ๋ฌด๊ฒŒ๊ฐ€ ๋ฌด๊ฑฐ์šด ๋ณต์„œ๋ฅผ ์ด๊ธด ํšŸ์ˆ˜๊ฐ€ ๋งŽ์€ ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์•ž์ชฝ์œผ๋กœ ๊ฐ‘๋‹ˆ๋‹ค.
  3. ์ž์‹ ๋ณด๋‹ค ๋ฌด๊ฑฐ์šด ๋ณต์„œ๋ฅผ ์ด๊ธด ํšŸ์ˆ˜๊นŒ์ง€ ๋™์ผํ•œ ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๋“ค ์ค‘์—์„œ๋Š” ์ž๊ธฐ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ ๋ฌด๊ฑฐ์šด ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์•ž์ชฝ์œผ๋กœ ๊ฐ‘๋‹ˆ๋‹ค.
  4. ์ž๊ธฐ ๋ชธ๋ฌด๊ฒŒ๊นŒ์ง€ ๋™์ผํ•œ ๋ณต์„œ์˜ ๋ฒˆํ˜ธ๋“ค ์ค‘์—์„œ๋Š” ์ž‘์€ ๋ฒˆํ˜ธ๊ฐ€ ์•ž์ชฝ์œผ๋กœ ๊ฐ‘๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

  • weights์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 1,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • weights์˜ ๋ชจ๋“  ๊ฐ’์€ 45 ์ด์ƒ 150 ์ดํ•˜์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.
    • weights[i] ๋Š” i+1๋ฒˆ ๋ณต์„œ์˜ ๋ชธ๋ฌด๊ฒŒ(kg)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • head2head์˜ ๊ธธ์ด๋Š” weights์˜ ๊ธธ์ด์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    • head2head์˜ ๋ชจ๋“  ๋ฌธ์ž์—ด์€ ๊ธธ์ด๊ฐ€ weights์˜ ๊ธธ์ด์™€ ๋™์ผํ•˜๋ฉฐ, 'N', 'W', 'L'๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    • head2head[i] ๋Š” i+1๋ฒˆ ๋ณต์„œ์˜ ์ „์ ์„ ์˜๋ฏธํ•˜๋ฉฐ, head2head[i][j]๋Š” i+1๋ฒˆ ๋ณต์„œ์™€ j+1๋ฒˆ ๋ณต์„œ์˜ ๋งค์น˜ ๊ฒฐ๊ณผ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
      • 'N' (None)์€ ๋‘ ๋ณต์„œ๊ฐ€ ์•„์ง ๋ถ™์–ด๋ณธ ์ ์ด ์—†์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
      • 'W' (Win)๋Š” i+1๋ฒˆ ๋ณต์„œ๊ฐ€ j+1๋ฒˆ ๋ณต์„œ๋ฅผ ์ด๊ฒผ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
      • 'L' (Lose)๋Š” i+1๋ฒˆ ๋ณต์‚ฌ๊ฐ€ j+1๋ฒˆ ๋ณต์„œ์—๊ฒŒ ์กŒ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • ์ž„์˜์˜ i์— ๋Œ€ํ•ด์„œ head2head[i][i] ๋Š” ํ•ญ์ƒ 'N'์ž…๋‹ˆ๋‹ค. ์ž๊ธฐ ์ž์‹ ๊ณผ ์‹ธ์šธ ์ˆ˜๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
    • ์ž„์˜์˜ i, j์— ๋Œ€ํ•ด์„œ head2head[i][j] = 'W' ์ด๋ฉด, head2head[j][i] = 'L'์ž…๋‹ˆ๋‹ค.
    • ์ž„์˜์˜ i, j์— ๋Œ€ํ•ด์„œ head2head[i][j] = 'L' ์ด๋ฉด, head2head[j][i] = 'W'์ž…๋‹ˆ๋‹ค.
    • ์ž„์˜์˜ i, j์— ๋Œ€ํ•ด์„œ head2head[i][j] = 'N' ์ด๋ฉด, head2head[j][i] = 'N'์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

weightshead2headresult

[50,82,75,120] ["NLWL","WNLL","LWNW","WWLN"] [3,4,1,2]
[145,92,86] ["NLW","WNL","LWN"] [2,3,1]
[60,70,60] ["NNN","NNN","NNN"] [2,1,3]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋‹ค์Œ์€ ์„ ์ˆ˜๋“ค์˜ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ธ ํ‘œ์ž…๋‹ˆ๋‹ค.

์„ ์ˆ˜ ๋ฒˆํ˜ธvs 1๋ฒˆvs 2๋ฒˆvs 3๋ฒˆvs 4๋ฒˆ์Šน๋ฅ ์ž๊ธฐ๋ณด๋‹ค ๋ฌด๊ฑฐ์šด ๋ณต์„œ๋ฅผ ์ด๊ธด ํšŸ์ˆ˜๋ชธ๋ฌด๊ฒŒ

1๋ฒˆ - ํŒจ๋ฐฐ ์Šน๋ฆฌ ํŒจ๋ฐฐ 33.33% 1ํšŒ 50kg
2๋ฒˆ ์Šน๋ฆฌ - ํŒจ๋ฐฐ ํŒจ๋ฐฐ 33.33% 0ํšŒ 82kg
3๋ฒˆ ํŒจ๋ฐฐ ์Šน๋ฆฌ - ์Šน๋ฆฌ 66.66% 2ํšŒ 75kg
4๋ฒˆ ์Šน๋ฆฌ ์Šน๋ฆฌ ํŒจ๋ฐฐ - 66.66% 0ํšŒ 120kg
  • ๋ณธ๋ฌธ์— ์„œ์ˆ ๋œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋”ฐ๋ผ [3,4,1,2] ๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ๋‹ค์Œ์€ ์„ ์ˆ˜๋“ค์˜ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ธ ํ‘œ์ž…๋‹ˆ๋‹ค.

์„ ์ˆ˜ ๋ฒˆํ˜ธvs 1๋ฒˆvs 2๋ฒˆvs 3๋ฒˆ์Šน๋ฅ ์ž๊ธฐ๋ณด๋‹ค ๋ฌด๊ฑฐ์šด ๋ณต์„œ๋ฅผ ์ด๊ธด ํšŸ์ˆ˜๋ชธ๋ฌด๊ฒŒ

1๋ฒˆ - ํŒจ๋ฐฐ ์Šน๋ฆฌ 50% 0ํšŒ 145kg
2๋ฒˆ ์Šน๋ฆฌ - ํŒจ๋ฐฐ 50% 1ํšŒ 92kg
3๋ฒˆ ํŒจ๋ฐฐ ์Šน๋ฆฌ - 50% 1ํšŒ 86kg
  • ๋ณธ๋ฌธ์— ์„œ์ˆ ๋œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋”ฐ๋ผ [2,3,1] ์„ return ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3

  • ๋‹ค์Œ์€ ์„ ์ˆ˜๋“ค์˜ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ธ ํ‘œ์ž…๋‹ˆ๋‹ค.

์„ ์ˆ˜ ๋ฒˆํ˜ธvs 1๋ฒˆvs 2๋ฒˆvs 3๋ฒˆ์Šน๋ฅ ์ž๊ธฐ๋ณด๋‹ค ๋ฌด๊ฑฐ์šด ๋ณต์„œ๋ฅผ ์ด๊ธด ํšŸ์ˆ˜๋ชธ๋ฌด๊ฒŒ

1๋ฒˆ - - - 0% (๋ฌด์ „์ ) 0ํšŒ 60kg
2๋ฒˆ - - - 0% (๋ฌด์ „์ ) 0ํšŒ 70kg
3๋ฒˆ - - - 0% (๋ฌด์ „์ ) 0ํšŒ 60kg
  • ๋ณธ๋ฌธ์— ์„œ์ˆ ๋œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋”ฐ๋ผ [2,1,3] ์„ return ํ•ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ

function solution(weights, head2head) {
  var answer = [];
  let result = [];

  for(let i = 0; i < head2head.length; i++) {
    let score = 0;
    let myWeight = weights[i];
    for(let j = 0; j < head2head[i].length; j++) { //์ƒ๋Œ€๋ฐฉ๊ณผ์˜ ๊ฒฝ๊ธฐ ๊ฒฐ๊ณผ ๋น„๊ต
      let fight = head2head[i][j]; // ๊ฒฝ๊ธฐ ๊ฒฐ๊ณผ
      let opWeight = weights[j];
      if(fight === 'N') continue; // ์ž๊ธฐ ์ž์‹ ๊ณผ ๊ฒฝ๊ธฐํ–ˆ์„ ๊ฒฝ์šฐ
      if(fight === 'W') { 
        if(myWeight < opWeight) score += 2; // ์ƒ๋Œ€๋ฐฉ์„ ์ด๊ฒผ์œผ๋ฉฐ ์ƒ๋Œ€๋ฐฉ๋ณด๋‹ค ๋ฌด๊ฒŒ๊ฐ€ ๋‚ฎ์„ ๊ฒฝ์šฐ์—๋Š” 2๋ฅผ ๊ฐ€์‚ฐ
        else score += 1; // ๋ฌด๊ฒŒ๊ฐ€ ๋‚ฎ์„ ๊ฒฝ์šฐ์—๋Š” 1์„ ๊ฐ€์‚ฐ
      } 
      else if(fight === 'L') score -= 1; // ์ƒ๋Œ€๋ฐฉ์—๊ฒŒ ์กŒ์„ ๊ฒฝ์šฐ์—๋Š” 1์„ ๊ฐ์‚ฐ
    }
    result.push([score, myWeight, i + 1]); // [์ ์ˆ˜, ๋ฌด๊ฒŒ, ์„ ์ˆ˜ ๋ฒˆํ˜ธ]
  }
    
  result.sort((a, b) => {
    if(a[0] === b[0]) return b[1] - a[1];
    else return b[0] - a[0]
  })
 
  answer = result.map(elem => {return elem[2]});
  return answer;
}

 

์˜ค๋‹ต

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ๋Š” ๋ชจ๋‘ ๋งž์•˜์ง€๋งŒ ์ฝ”๋“œ๋ฅผ ์ œ์ถœํ–ˆ์„ ๋•Œ ๋งŽ์€ ์ผ€์ด์Šค์—์„œ ์˜ค๋‹ต ์ฒ˜๋ฆฌ๋˜์—ˆ๋‹ค.

 

์ด์— ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ์ฝ์–ด๋ณด๋‹ˆ ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์กฐ๊ฑด์„ ์ œ๋Œ€๋กœ ์ถฉ์กฑ์‹œํ‚ค์ง€ ๋ชปํ–ˆ์Œ์„ ํ™•์ธํ•˜์˜€๋‹ค.

 

  • ์Šน๋ฅ ์„ ํ† ๋Œ€๋กœ ์ˆœ์œ„๋ฅผ ๊ฒฐ์ •ํ–ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ ๋‹จ์ˆœํžˆ ์ ์ˆ˜๋ฅผ ํ†ตํ•ด ์ˆœ์œ„๋ฅผ ๊ฒฐ์ •ํ–ˆ๋‹ค
  • ์œ„ ์กฐ๊ฑด์„ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•ด 'N'์ธ ๊ฒฝ์šฐ์— ์–ด๋– ํ•œ ์—ฐ์‚ฐ ์ฒ˜๋ฆฌ ์—†์ด ๋„˜์–ด๊ฐ”๋‹ค_ (์Šน๋ฅ ) = (์ด๊ธด ๊ฒฝ๊ธฐ ์ˆ˜) / (์น˜๋ฃฌ ๊ฒฝ๊ธฐ ์ˆ˜)
  • ๋ฌด๊ฒŒ๊ฐ€ ๋‚ฎ์€ ์„ ์ˆ˜๊ฐ€ ๋” ๋†’์€ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค ๋ผ๋Š” ์กฐ๊ฑด์€ ์ฒซ ๋ฒˆ์งธ ์กฐ๊ฑด์—์„œ ์ˆœ์œ„๋ฅผ ๊ฒฐ์ • ์ง“์ง€ ๋ชปํ•  ๊ฒฝ์šฐ์— ์ฐจํ›„๋กœ ํƒ์ƒ‰ํ•ด์คฌ์–ด์•ผ ํ–ˆ์ง€๋งŒ, ๋ณธ ์ฝ”๋“œ์—์„œ๋Š” ์ ์ˆ˜ 1์ ์„ ๋” ์ฃผ๋Š” ์‹์œผ๋กœ ์—ฐ์‚ฐ ์ฒ˜๋ฆฌํ–ˆ๋‹ค.

์œ„์˜ ์‚ฌํ•ญ์„ ํ† ๋Œ€๋กœ ๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์•˜๋‹ค.

 

์ฝ”๋“œ

function rating(arr1, arr2, num) {
    const ratings = []
    for(let i=0; i<num; i++) {
        let wins = 0
        let weight = 0
        let none = 0
        for(let j=0; j<num; j++) {
            if(arr1[i][j] === "W") {
                wins++
                if(arr2[j] > arr2[i]) weight++
            } else if(arr1[i][j] === "N") none ++; // ์น˜๋ฃฌ ๊ฒฝ๊ธฐ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด N์ธ ๊ฒฝ์šฐ์—๋„ ๊ด€๋ จ ๋ณ€์ˆ˜์— ๊ฐ€์‚ฐ      
        }
        if(num-none !== 0) ratings.push([i+1, wins/(num-none)*100, weight, arr2[i]]) // ๊ฒฝ๊ธฐ๋ฅผ ํ•˜๋‚˜๋„ ์น˜๋ฃจ์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด 
        else ratings.push([i+1, 0, weight, arr2[i]])
    }
    return ratings // [์„ ์ˆ˜ ๋ฒˆํ˜ธ, ์Šน๋ฅ , ์ž์‹ ๋ณด๋‹ค ์ฒด๊ธ‰์ด ๋†’์€ ์„ ์ˆ˜์— ๋Œ€ํ•œ ์Šน๋ฆฌ ์ˆ˜, ์„ ์ˆ˜ ๋ฌด๊ฒŒ] 
}


function solution(weights, head2head) {
    const num = weights.length;
    const rate = rating(head2head, weights, num);
    const answer = rate.sort((a, b) => {
        if(a[1] != b[1]) return b[1] - a[1] // ์Šน๋ฅ 
        else {
            if(a[2] != b[2]) return b[2] - a[2] // ์ž์‹ ๋ณด๋‹ค ์ฒด๊ธ‰์ด ๋†’์€ ์„ ์ˆ˜์— ๋Œ€ํ•œ ์Šน๋ฆฌ ์ˆ˜
            else {
                if(a[3] != b[3]) return b[3] - a[3] // ์„ ์ˆ˜ ์ž์‹ ์˜ ์ฒด๊ธ‰
                else { // ์„ ์ˆ˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‚ฎ์€ ์ˆœ์„œ
                    return a[0] - b[0]
                }
            }
        }
    }).map(v => v[0]) // ์ตœ์ข…์ ์œผ๋กœ ์„ ์ˆ˜ ๋ฒˆํ˜ธ ๋ฐ˜ํ™˜
    return answer
}
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   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
๊ธ€ ๋ณด๊ด€ํ•จ