[programmers] Lv2. 메뉴 리뉴얼 | 조합 | protect-me

protect-me·2021년 9월 7일
post-thumbnail

🕊 Link

Lv2. 메뉴 리뉴얼 Javascript
https://programmers.co.kr/learn/courses/30/lessons/72411

🧑🏻‍💻 Code(javascript)

function solution(orders, course) {
  const arr = []
  const count = []
  const lengths = []

  const sortedOrders = orders.map(order => {
    return order.split('').sort().join('')
  })

  course.forEach(crs => {
    sortedOrders.forEach(order => {
      if (crs < order.length) {
        const spread = order.split('')
        const combies = getCombinations(spread, crs)
        combies.forEach(combi => {
          verifyPush(combi.join(''))
        })
      } else if (crs === order.length) {
        verifyPush(order)
      }
    })
  })

  function verifyPush(order) {
    const dupIndex = arr.indexOf(order)
    if (dupIndex > -1) {
      count[dupIndex]++
    } else {
      arr.push(order)
      count.push(1)
      lengths.push(order.length)
    }
  }

  for (let i = 0; i < arr.length; i++) {
    if (count[i] === 1) {
      arr.splice(i, 1)
      count.splice(i, 1)
      lengths.splice(i, 1)
      i--
    }
  }

  const result = []
  let curMaxCount = 0
  let temp = []
  for (let i = 0; i < count.length; i++) {
    if (i === 0 || lengths[i - 1] !== lengths[i]) {
      if (i !== 0) {
        result.push(...temp)
        temp = []
      }
      curMaxCount = count[i]
      temp.push(arr[i])
    } else {
      if (curMaxCount === count[i]) {
        temp.push(arr[i])
      } else if (curMaxCount < count[i]) {
        curMaxCount = count[i]
        temp = [arr[i]]
      }
    }
  }
  result.push(...temp)
  return result.sort()
}

function getCombinations(arr, num) {
  const result = [];
  if (num === 1) return arr.map((value) => [value]);
  arr.forEach((fixed, index, origin) => {
    const rest = origin.slice(index + 1);
    const combinations = getCombinations(rest, num - 1);
    const attached = combinations.map((combination) => [fixed, ...combination]);
    result.push(...attached);
  });
  return result;
}

💡 Solution

function solution(orders, course) {
  const arr = []
  const count = []
  const lengths = []

  const sortedOrders = orders.map(order => {
    return order.split('').sort().join('')
  })

  course.forEach(crs => {
    sortedOrders.forEach(order => {
      if (crs < order.length) {
        const spread = order.split('')
        const combies = getCombinations(spread, crs)
        combies.forEach(combi => {
          verifyPush(combi.join(''))
        })
      } else if (crs === order.length) {
        verifyPush(order)
      }
    })
  })

  function verifyPush(order) {
    const dupIndex = arr.indexOf(order)
    if (dupIndex > -1) {
      count[dupIndex]++
    } else {
      arr.push(order)
      count.push(1)
      lengths.push(order.length)
    }
  }

  for (let i = 0; i < arr.length; i++) {
    if (count[i] === 1) {
      arr.splice(i, 1)
      count.splice(i, 1)
      lengths.splice(i, 1)
      i--
    }
  }

  const result = []
  let curMaxCount = 0
  let temp = []
  for (let i = 0; i < count.length; i++) {
    if (i === 0 || lengths[i - 1] !== lengths[i]) {
      if (i !== 0) {
        result.push(...temp)
        temp = []
      }
      curMaxCount = count[i]
      temp.push(arr[i])
    } else {
      if (curMaxCount === count[i]) {
        temp.push(arr[i])
      } else if (curMaxCount < count[i]) {
        curMaxCount = count[i]
        temp = [arr[i]]
      }
    }
  }
  result.push(...temp)
  return result.sort()
}

function getCombinations(arr, num) {
  const result = [];
  if (num === 1) return arr.map((value) => [value]);
  arr.forEach((fixed, index, origin) => {
    const rest = origin.slice(index + 1);
    const combinations = getCombinations(rest, num - 1);
    const attached = combinations.map((combination) => [fixed, ...combination]);
    result.push(...attached);
  });
  return result;
}

테스트 결과

테스트 1 〉 통과 (0.57ms, 30.1MB)
테스트 2 〉 통과 (0.46ms, 30MB)
테스트 3 〉 통과 (0.61ms, 30.1MB)
테스트 4 〉 통과 (0.63ms, 30.1MB)
테스트 5 〉 통과 (0.59ms, 30.2MB)
테스트 6 〉 통과 (1.16ms, 30.2MB)
테스트 7 〉 통과 (1.29ms, 30.2MB)
테스트 8 〉 통과 (19.46ms, 37.5MB)
테스트 9 〉 통과 (18.64ms, 40.4MB)
테스트 10 〉 통과 (27.37ms, 38.2MB)
테스트 11 〉 통과 (10.40ms, 35MB)
테스트 12 〉 통과 (15.05ms, 36.4MB)
테스트 13 〉 통과 (25.89ms, 39.4MB)
테스트 14 〉 통과 (20.82ms, 40.8MB)
테스트 15 〉 통과 (47.48ms, 37.6MB)
테스트 16 〉 통과 (9.96ms, 41.1MB)
테스트 17 〉 통과 (10.12ms, 41.6MB)
테스트 18 〉 통과 (8.70ms, 42MB)
테스트 19 〉 통과 (0.25ms, 30MB)
테스트 20 〉 통과 (14.82ms, 41.7MB)

👨‍👦‍👦 Others

👨🏻‍💻💭 Self Feedback

어찌어찌 풀긴했는데.. 최적화를 해야겠다.


  • 2021.09. - 최초 작성

댓글 환영 질문 환영
by.protect-me

profile
protect me from what i want

0개의 댓글