KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • recursion
  • string
  • C
  • Level 0
  • js
  • codeup
  • Level 1
  • Level1
  • 그래프
  • 제코베 JS 100제
  • 문자열
  • Level 2
  • 수학
  • 정렬
  • 백준
  • programmers
  • LeetCode
  • C++
  • 다이나믹 프로그래밍
  • tree

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Dynamic Programming / 322번 / Coin Change / JS

2023. 5. 14. 19:15

< 문제 바로가기 >

 

Coin Change - LeetCode

Can you solve this real interview question? Coin Change - You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the fewest number of coins that you need to make

leetcode.com

 

< 문제 간단설명 >

주어진 amount의 값을 만들기 위해 coins 배열에 담겨있는 coin의 동전을 최소 몇 개 써야하는지 반환하는 문제이다. 만약 coins의 동전들로 만들 수 없는 값이라면 -1을 반환한다.

 

/**
 * @param {number[]} coins
 * @param {number} amount
 * @return {number}
 */
var coinChange = function (coins, amount) {
  let dp = Array.from({ length: amount + 1 }, () => Infinity);
  dp[0] = 0; // 0원은 0개의 동전이 필요

  for (const COIN of coins) {
    for (let i = COIN; i <= amount; i += 1) {
      // i원은 (i - COIN)원 보다 COIN원의 동전 1개를 더 사용
      // 혹은 이미 더 적은 동전을 사용해서 만들 수 있다면 그대로
      dp[i] = Math.min(dp[i], dp[i - COIN] + 1);
    }
  }

  return dp[amount] === Infinity ? -1 : dp[amount];
};
저작자표시

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Two Pointer / 16번 / 3Sum Closese / JS  (0) 2023.05.14
LeetCode / Dynamic Programming / 416번 / Partition Equal Subset Sum / JS  (0) 2023.05.14
LeetCode / Dynamic Programming / 198번 / House Robber / JS  (0) 2023.05.14
LeetCode / Graph / 210번 / Course Schedule II / JS  (0) 2023.05.14
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Two Pointer / 16번 / 3Sum Closese / JS
    • LeetCode / Dynamic Programming / 416번 / Partition Equal Subset Sum / JS
    • LeetCode / Dynamic Programming / 198번 / House Robber / JS
    • LeetCode / Graph / 210번 / Course Schedule II / JS
    KimMinJun
    KimMinJun

    티스토리툴바