๋ฌธ์ ๋งํฌ
https://school.programmers.co.kr/learn/courses/30/lessons/118666
๋ฌธ์ ๋งํฌ
function solution(survey, choices) {
var answer = '';
const map = {}
const types = ['RT','CF','JM','AN']
types.forEach((type)=>{
type.split("").forEach((v)=>{
map[v]=0
})
})
survey.forEach((v,i)=>{
const [disagree, agree] = v.split('')
map[choices[i]>4?agree:disagree] += Math.abs(choices[i]-4)
})
types.forEach((type)=>{
const [type1,type2] = type.split("").sort()
if(map[type1]>=map[type2]){
answer+=type1
}else{
answer+=type2
}
})
return answer;
}
ํ๊ธฐ
์ฌ์ค ์ ์ฝ๋๋ ๋ด๊ฐ ์ฒ์ ์ ์ถํ ์ฝ๋๋ฅผ ์กฐ๊ธ ๋ ์ธ๋ จ๋๊ฒ ์์ ํ ์ฝ๋์ด๋ค.
ํด๋น ๋ฌธ์ ๋ฅผ ํ๊ณ ๋ค๋ฅธ ์ฌ๋๋ค์ ์ฝ๋๋ฅผ ๋ณด๋ ์์ ํ์ฌ ์์ฑํ๊ณ ์ ํ๋ ์๊ตฌ๊ฐ ์๊ฒผ๋ค.
์์ ํ๋ค ๋ณด๋ map [ choice[i]>4? agree:disagree ] ์ ๊ฐ์ด ์์ฑ ๊ฐ์ ์ฐ์ฐ์ ๋ฃ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด๋ณด๊ฒ ๋์๋ค.
'๐Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค (JS) > ๊ฐ์ฅ ํฐ์ (0) | 2024.07.04 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค[JS] > ํ๋ฐฐ์์ (0) | 2024.06.29 |
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ํ์ด(js) > ์์ฃผํ์ง๋ชปํ์ ์(lv1) (0) | 2023.08.04 |
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ํ์ด(js) > ๋ ๋ฐ๋จน๊ธฐ(lv2) (0) | 2023.08.03 |
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ํ์ด(js) > ๊ฐ์ ์ซ์๋ ์ซ์ด(lv1) (0) | 2023.08.02 |