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

[๋ฌธ์ œ]

https://school.programmers.co.kr/learn/courses/30/lessons/118666

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

[ํ’€์ด]

์ดˆ๊ธฐ ์„ธํŒ…

  • type ๋ฐฐ์—ด์„ ์„ ์–ธํ•ด, ๊ฐ๊ฐ์˜ ์„ฑ๊ฒฉ ์œ ํ˜•์— ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ž„
  • survey๊ฐ€ RT ํ˜•์‹์œผ๋กœ ๋“ค์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์„ฑ๊ฒฉ ์œ ํ˜• ๋ฌธ์ž๋ฅผ ํ†ตํ•ด index๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ map์„ ๋งŒ๋“ฆ
  • ์ ์ˆ˜๋ฅผ ์ €์žฅํ•  scroe๋ฐฐ์—ด์„ ์„ ์–ธ

ํ’€์ด ๊ณผ์ •

 

1. survey๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ˆœํšŒํ•˜๋ฉฐ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

  • ํ˜„์žฌ ํ™•์ธ์ค‘์ธ ์„ค๋ฌธ์ด AN ์ด๋ผ๋ฉด A์™€ N์„ split ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋ฝ‘์•„๋ƒ„
  • ๋งŒ์•ฝ ํ˜„์žฌ ํ™•์ธ ์ค‘์ธ i๋ฒˆ์งธ ์„ค๋ฌธ์˜ ์„ ํƒ์ง€ choice[i]์˜ ๊ฐ’์ด 4๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด, A์˜ score์— 4 - choice[i]๋ฅผ ๋”ํ•จ
  • ๋งŒ์•ฝ ํ˜„์žฌ ํ™•์ธ์ค‘์ธ i๋ฒˆ์งธ ์„ค๋ฌธ์˜ ์„ ํƒ์ง€ choice[i]์˜ ๊ฐ’์ด 4๋ณด๋‹ค ํฌ๋””๋ฉด, N์˜ score + choice[i] - 4๋ฅผ ๋”ํ•จ

2. ๊ณ„์‚ฐ๋œ score๋ฅผ ํ†ตํ•ด ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์„ฑ๊ฒฉ ์œ ํ˜• ๋ฌธ์ž๋ฅผ answer์— ๋”ํ•ด์ฃผ์–ด ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

 

[์ฝ”๋“œ]

function solution(survey, choices) {
    var answer = '';
    
    const map = new Map();
    
    const type = ['R', 'T', 'C', 'F', 'J', 'M', 'A', 'N'];
    type.map((i, idx) => {
        map.set(i, idx);
    });
    
    const score = Array.from({length: 8}, () => 0);
    for(let i=0;i<survey.length;i++) {
        const [a, b] = survey[i].split("");
        if(choices[i] <= 4) {
            score[map.get(a)] += 4 - choices[i];
        } else {
            score[map.get(b)] += choices[i] - 4;
        }
    }
    
    for(let i=0;i<type.length;i+=2) {
        const [a, b] = [type[i], type[i+1]];
        if(score[i] >= score[i+1]) {
            answer += a;
        } else {
            answer += b;
        }
    }
    
    return answer;
}