[백준] 1296 팀 이름 정하기 - Node.js ❓

송철진·2023년 4월 5일
0

백준-Node.js

목록 보기
55/69

문제

https://www.acmicpc.net/problem/1296

나의 풀이❓

왜 답이 DAVE 일까? 난 JOHN이 나오는데..

const input = `MERYLOV
5
JOHN
DAVE
STEVE
JOHN
DAVE`.split('\n')
const word = input[0].split('')
const num = Number(input[1])
const teams = [...new Set(input.slice(2))].map(el => el.split(''))

const solution = (word, num, teams) => {
  const w = [...new Set(word)]  
  let wObj = word.reduce( (ac,v) => ({...ac, [v] : ( ac[v] || 0 ) + 1 }), 0)
  const r = []
  
  const getCombinations = (arr, selectNumber) => {
    const results = [];
    if (selectNumber === 1) return arr.map((el) => [wObj[el]]); 
    arr.forEach((fixed, index, origin) => {
      const rest = origin.slice(index + 1); 
      const combinations = getCombinations(rest, selectNumber - 1); 
      const attached = combinations.map((el) => [wObj[fixed], ...el]); 
      results.push(...attached); 
    });
    return results; 
  }
  
  teams.forEach(el => {
    wObj = word.reduce( (ac,v) => ({...ac, [v] : ( ac[v] || 0 ) + 1 }), 0)
    w.forEach(v => {
      el.forEach(a => {
        if(a === v) wObj[v] += 1
      })
    })
    
    let per = getCombinations(word, 2).map(v => v[0]+v[1]).reduce((a,c)=>a*c, 1) % 100 
    let name = el.join('')
    r.push([name, per])
  })
  return r.sort().sort((a, b) => b[1] - a[1])
  // [ [ 'JOHN', 72 ], [ 'STEVE', 40 ], [ 'DAVE', 4 ] ]
}
console.log(solution(word, num, teams))

참조
순열과 조합
https://velog.io/@devjade/JavaScript%EB%A1%9C-%EC%88%9C%EC%97%B4%EA%B3%BC-%EC%A1%B0%ED%95%A9-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%EA%B5%AC%ED%98%84%ED%95%98%EA%B8%B0#%EC%BD%94%EB%93%9C%EB%A5%BC-%EC%A2%85%ED%95%A9%ED%95%B4%EB%B3%B4%EB%A9%B4

profile
검색하고 기록하며 학습하는 백엔드 개발자

0개의 댓글