Skip to content

Commit f876d15

Browse files
Problem 322 (#34)
* add time and space complexity to problem 208 * add solution and resources * update readme
1 parent 5c890a4 commit f876d15

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
4444
33. [Evaluate Reverse Polish Notation #150](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/polish-notation-150.md)
4545
34. [Course Schedule #207](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/course-schedule-207.md)
4646
35. [Implement Trie (Prefix Tree) #208](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/implement-trie-208.md)
47-
36. Coin Change #322
47+
36. [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
4848
37. Product of Array Except Self #238
4949
38. Min Stack #155
5050
39. Validate Binary Search Tree #98
@@ -109,6 +109,7 @@ In order to practice with similar data structures I'll be placing each problem i
109109
- [Evaluate Reverse Polish Notation #150](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/polish-notation-150.md)
110110
- [Maximum Units on a Truck #1710](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/max-units-truck-1710.md)
111111
- [Meeting Rooms II](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/meeting-rooms-ii-253.md)
112+
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
112113

113114
### Queue
114115

@@ -117,6 +118,7 @@ In order to practice with similar data structures I'll be placing each problem i
117118
- [Implement Queue using Stacks #232](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/implement-queue-stacks-232.md)
118119
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
119120
- [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)
121+
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
120122

121123
### Stack
122124

@@ -210,6 +212,7 @@ Within the problems above there are several patterns that often occur. I plan to
210212
- [Clone Graph #133](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/clone-graph-133.md)
211213
- [Number of Provinces #547](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-provinces-547.md)
212214
- [Course Schedule #207](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/course-schedule-207.md)
215+
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
213216

214217
### Depth First Search
215218

@@ -231,6 +234,7 @@ Within the problems above there are several patterns that often occur. I plan to
231234

232235
- [Maximum Subarray #53](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/maximum-subarray-53.md)
233236
- [01 Matrix #542](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/01-matrix-542.md)
237+
- [Coin Change #322](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/coin-change-322.md)
234238

235239
## Attribution
236240

medium/coin-change-322.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Coin Change
2+
3+
Page on leetcode: https://leetcode.com/problems/coin-change/
4+
5+
## Problem Statement
6+
7+
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
8+
9+
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
10+
11+
You may assume that you have an infinite number of each kind of coin.
12+
13+
### Constraints
14+
15+
- 1 <= coins.length <= 12
16+
- 1 <= coins[i] <= 231 - 1
17+
- 0 <= amount <= 104
18+
19+
### Example
20+
21+
```
22+
Input: coins = [1,2,5], amount = 11
23+
Output: 3
24+
Explanation: 11 = 5 + 5 + 1
25+
```
26+
27+
## Solution
28+
29+
- Greedy algorithm
30+
- Is the coin array sorted? No
31+
32+
### Pseudocode
33+
34+
1. Sort coins largest to smallest
35+
2. Create counter variable
36+
3. Greedy take largest coin until not divisible into remaining amount
37+
4. Move to next amount and repeat until amount is reached
38+
5. If at end of coins and can't reach amount return -1
39+
40+
### Initial Attempt
41+
42+
Greedy doesn't work as it takes a large coin and may return -1 even when you could find a solution with the second largest coin.
43+
44+
```javascript
45+
const coinChange = function (coins, amount) {
46+
coins.sort((a, b) => b - a);
47+
let count = 0;
48+
for (let i = 0; i < coins.length; i++) {
49+
if (amount === 0) {
50+
return count;
51+
}
52+
const numDivisible = Math.floor(amount / coins[i]);
53+
amount -= numDivisible * coins[i];
54+
count += numDivisible;
55+
}
56+
if (amount > 0) {
57+
return -1;
58+
}
59+
return count;
60+
};
61+
```
62+
63+
### Optimized Solution
64+
65+
This problem can be solved using bottom up dynamic programming. Time complexity for this solution is O(n\*m) and space complexity is O(n), where n is amount and m is coins length. You can see an explanation of the solution here: https://www.youtube.com/watch?v=H9bfqozjoqs
66+
67+
```javascript
68+
// Dynamic Programming
69+
const coinChange = function (coins, amount) {
70+
// Create array and fill with a max value. This will help later for determining if a solution was found.
71+
const dpCounts = Array(amount + 1).fill(amount + 1);
72+
// Base case
73+
dpCounts[0] = 0;
74+
75+
// Fill out all cache slots with the minimum amount of coins
76+
for (let i = 1; i < dpCounts.length; i++) {
77+
for (const coin of coins) {
78+
// Make sure that coin can actual make up amount i
79+
if (i - coin >= 0) {
80+
dpCounts[i] = Math.min(dpCounts[i], 1 + dpCounts[i - coin]);
81+
}
82+
}
83+
}
84+
// Make sure a solution was found
85+
if (dpCounts[amount] < amount + 1) {
86+
return dpCounts[amount];
87+
} else {
88+
return -1;
89+
}
90+
};
91+
92+
// Breadth First Search
93+
const coinChange = function (coins, amount) {
94+
// If amount is zero, always returns 0
95+
if (amount === 0) {
96+
return 0;
97+
}
98+
// Create array and fill with a temp value. This will help later for determining if a solution was found.
99+
const counts = Array(amount + 1).fill(-1);
100+
101+
// Create queue for BFS and numOfCoins to track the "depth" of the search
102+
const queue = [0];
103+
let numOfCoins = 0;
104+
105+
while (queue.length > 0) {
106+
numOfCoins++;
107+
// Checking nodes for this level
108+
let levelNodes = queue.length;
109+
while (levelNodes > 0) {
110+
const current = queue.shift();
111+
// Check current amount plus each coin
112+
for (const coin of coins) {
113+
if (current + coin === amount) {
114+
// Found a match at this level. Since it is BFS this will be the lowest level possible.
115+
return numOfCoins;
116+
}
117+
// Skip if the total is more than the amount or if it has already been calculated
118+
if (current + coin < counts.length && counts[current + coin] === -1) {
119+
// Mark amount as calculated and add to queue to be process with next set of coins
120+
counts[current + coin] = numOfCoins;
121+
queue.push(current + coin);
122+
}
123+
}
124+
levelNodes--;
125+
}
126+
}
127+
// If BFS completes and numOFCoins doesn't return then no solution exist
128+
return -1;
129+
};
130+
```

medium/implement-trie-208.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Trie.prototype.startsWith = function (prefix) {
130130

131131
### Optimized Solution
132132

133-
The below is roughly the same as above, just with some clean up to make the code more concise. You can see an explanation for this solution here: https://www.youtube.com/watch?v=oobqoCJlHA0
133+
The below is roughly the same as above, just with some clean up to make the code more concise. Time is O(n) for insertion and search. Space complexity id O(n) for insertion and O(1) for search.You can see an explanation for this solution here: https://www.youtube.com/watch?v=oobqoCJlHA0
134134

135135
```javascript
136136
const Node = function () {

0 commit comments

Comments
 (0)