javascripttypescriptdynamic-programmingcoin-change# Coin Change Dynamic Programming Problem (limited supply of coins)

I'm trying to solve the coin change problem with the variation that the same coin can be used at most twice. I solved the problem where the coins are unlimited but I still don't quite understand how to do it in case of limited coins.

```
function coinChange(coins: number[], amount: number): number[][] {
let M:number[][] = [];
for (let i = 0 ; i<coins.length ; i++)
M[i] = [0];
for(let i = 0 ; i < coins.length ; i++){
for(let j = 1 ; j <= amount ; j++){
if(coins[i] > j && i>0) M[i][j] = M[i-1][j];
else{
let diff:number = j-coins[i];
if(M[i][diff] != -1){
let c:number;
if(i>0){
c = Math.min(M[i-1][j],1+M[i][diff]);
}
else{
c = 1+M[i][diff];
}
M[i][j] = c;
}
else M[i][j] = -1;
}
}
}
return M;
}
```

Solution

Solution:

```
function coin2(coins:number[], amount:number):number{
let M:number[][] = [];
coins.push(...coins)
for(let i = 0 ; i <= coins.length ; i++){
M[i] = [];
}
M[0][0] = 0;
for(let j = 1 ; j <= amount ; j++){
M[0][j] = Infinity;
}
for(let i = 1 ; i <= coins.length ; i++){
for(let j = 0 ; j <= amount ; j++){
if(coins[i-1] > j) M[i][j] = M[i-1][j];
else{
let dif:number = j-coins[i-1];
M[i][j] = Math.min(M[i-1][j],1+M[i-1][dif])
}
}
}
console.log(M);
return M[coins.length][amount];
}
```

- Get timezone offset from timezone name using Javascript
- Best way to perform a full text search in MongoDB and Mongoose
- NextJs <Link> doesn't rerun scripts
- make a left outer join in javascript
- How can I apply CSS code from a server request to a react app?
- How to deploy to Vercel with dynamically generated package from Prisma
- I am taking a course on React, Next, Vite and learning the concept of Lifting the State up, But I am doing something wrong Help Please
- Add different sounds to different buttons
- Check if value is a Symbol in JavaScript
- Bootstrap Spinner Visible on submit
- MailChimp: Why is PUT method with javascript returning "Use PUT to insert or update list members"
- Query paramater is undefined in my API in Next.js app router
- How to store simultaneous keypresses
- how to replay a fetch in chrome developer tools
- How do I detect keypresses in Javascript?
- Javascript return number of days,hours,minutes,seconds between two dates
- mongoose to determine update-upsert is doing insert or update
- Enviromental value not working in .js file / Next.js
- How to implement the Plaid identity verification product in Laravel?
- Replacing responsive tables for better markup and accessibility
- Single page, scrolling site with fixed dynamic footer
- Pure javascript method to wrap content in a div
- React Native: Using lodash debounce
- Javascript change html or body css
- Create empty URL object from scratch in JavaScript
- React Native text string must be rendered within a text
- Repeated textures are severely distorted/shaking when rotating camera
- Escape dollar sign in JavaScript template literals (template strings)
- Need Assistance with a JavaScript Function for the "Paper, Rock, Sissors Challenge."
- How to call error() from within an async function in Svelte?