๐Ÿฅ‡๐Ÿฅˆ๐Ÿฅ‰[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋“ฑ์ˆ˜ ๋งค๊ธฐ๊ธฐ

Chobbyยท2022๋…„ 10์›” 22์ผ
1

Programmers

๋ชฉ๋ก ๋ณด๊ธฐ
139/349
post-custom-banner

๐Ÿงก๋ฌธ์ œ ์„ค๋ช…

์˜์–ด ์ ์ˆ˜์™€ ์ˆ˜ํ•™ ์ ์ˆ˜์˜ ํ‰๊ท  ์ ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•™์ƒ๋“ค์˜ ๋“ฑ์ˆ˜๋ฅผ ๋งค๊ธฐ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜์–ด ์ ์ˆ˜์™€ ์ˆ˜ํ•™ ์ ์ˆ˜๋ฅผ ๋‹ด์€ 2์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด score๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์˜์–ด ์ ์ˆ˜์™€ ์ˆ˜ํ•™ ์ ์ˆ˜์˜ ํ‰๊ท ์„ ๊ธฐ์ค€์œผ๋กœ ๋งค๊ธด ๋“ฑ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


๐Ÿ’›์ œํ•œ์‚ฌํ•ญ

  • 0 โ‰ค score[0], score[1] โ‰ค 100
  • 1 โ‰ค score์˜ ๊ธธ์ด โ‰ค 10
  • score์˜ ์›์†Œ ๊ธธ์ด๋Š” 2์ž…๋‹ˆ๋‹ค.
  • score๋Š” ์ค‘๋ณต๋œ ์›์†Œ๋ฅผ ๊ฐ–์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๐Ÿ’š์ž…์ถœ๋ ฅ ์˜ˆ

scoreresult
[[80, 70], [90, 50], [40, 70], [50, 80]][1, 2, 4, 3]
[[80, 70], [70, 80], [30, 50], [90, 100], [100, 90], [100, 100], [10, 30]][4, 4, 6, 2, 2, 1, 7]

๐Ÿ’™์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

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

  • ํ‰๊ท ์€ ๊ฐ๊ฐ 75, 70, 55, 65 ์ด๋ฏ€๋กœ ๋“ฑ์ˆ˜๋ฅผ ๋งค๊ฒจ [1, 2, 4, 3]์„ returnํ•ฉ๋‹ˆ๋‹ค.

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

  • ํ‰๊ท ์€ ๊ฐ๊ฐ 75, 75, 40, 95, 95, 100, 20 ์ด๋ฏ€๋กœ [4, 4, 6, 2, 2, 1, 7] ์„ returnํ•ฉ๋‹ˆ๋‹ค.
    ๊ณต๋™ 2๋“ฑ์ด ๋‘ ๋ช…, ๊ณต๋™ 4๋“ฑ์ด 2๋ช… ์ด๋ฏ€๋กœ 3๋“ฑ๊ณผ 5๋“ฑ์€ ์—†์Šต๋‹ˆ๋‹ค.

๐Ÿ’œ๋‚˜์˜ ํ’€์ด

function solution(score) {
    // ๋“ฑ์ˆ˜๋ฅผ ๋‹ด์„ object ์ƒ์„ฑ
    const dict = {}
    // ํ˜„์žฌ์˜ ์ ์ˆ˜๋ฅผ ํ‰๊ท  ๋ณ„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ•œ ํ›„ ์ž๋ฃŒ ์‚ฌ์ „์— ์ž…๋ ฅ
    const rank = score
        .map(([a,b]) => (a+b)/2)
        .sort((a,b) => b-a)
        .forEach((a,i) => dict[a] === undefined ? dict[a] = i+1 : null)
    // ํ˜„์žฌ ์ ์ˆ˜์˜ ํ‰๊ท ์„ ์‚ฌ์ „์—์„œ ์ฐพ์•„ ๋“ฑ์ˆ˜ ๋ฐ˜ํ™˜
    return score.map(([a,b]) => dict[(a+b)/2])
}
profile
๋‚ด ์ง€์‹์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋‹ดํ•จ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€