You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ✅ Pattern 15: 0-1 Knapsack (Dynamic Programming).md
+61-15Lines changed: 61 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,14 +118,21 @@ Let’s apply this knowledge to solve some of the frequently asked <b>DP</b> pro
118
118
119
119
# Pattern 1: 0/1 Knapsack
120
120
<b>Problem Set</b>
121
+
121
122
[🔎 0/1 Knapsack](#🔎-01-knapsack-medium)
123
+
122
124
[Equal Subset Sum Partition](#equal-subset-sum-partition-medium)
125
+
123
126
[Subset Sum](#🔎-subset-sum-medium)
127
+
124
128
[Minimum Subset Sum Difference ](#minimum-subset-sum-difference-hard)
129
+
125
130
[🌟Count of Subset Sum](#🌟count-of-subset-sum-hard)
131
+
126
132
[🌟 Target Sum](#🌟-target-sum-hard)
127
133
128
134
135
+
129
136
<b>0/1 Knapsack pattern</b> is based on the famous problem with the same name which is efficiently solved using <b>Dynamic Programming (DP)</b>.
130
137
131
138
In this pattern, we will go through a set of problems to develop an understanding of <b>DP</b>. We will always start with a brute-force recursive solution to see the overlapping subproblems, i.e., realizing that we are solving the same problems repeatedly.
@@ -1544,14 +1551,23 @@ console.log(
1544
1551
1545
1552
# Pattern 2: Unbounded Knapsack
1546
1553
<b>Problem Set</b>
1554
+
1555
+
[Unbounded Knapsack](#unbounded-knapsack)
1556
+
1557
+
[Rod Cutting](#rod-cutting)
1558
+
1559
+
[🔎👩🏽🦯 Coin Change](#🔎👩🏽🦯-coin-change)
1560
+
1561
+
[Minimum Coin Change](#minimum-coin-change)
1562
+
1563
+
[Maximum Ribbon Cut](#maximum-ribbon-cut)
1564
+
1547
1565
[]()
1566
+
1548
1567
[]()
1568
+
1549
1569
[]()
1550
-
[]()
1551
-
[]()
1552
-
[]()
1553
-
[]()
1554
-
[]()
1570
+
1555
1571
##
1556
1572
1557
1573
> Given the weights and profits of `N` items, we are asked to put these items in a knapsack with a capacity `C`. The goal is to get the `maximum profit` out of the knapsack items. The only difference between the <b>[0/1 Knapsack pattern](#pattern-1-01-knapsack)</b> problem and this problem is that we are allowed to use an unlimited quantity of an item.
We are given a ribbon of length `n` and a set of possible `ribbonLengths`. We need to cut the ribbon into the maximum number of pieces that comply with the above-mentioned possible lengths. Write a method that will return the count of pieces.
2350
2366
2367
+
> Given a number array to represent possible `ribbonLengths` and a total ribbon length `n`, we need to find the maximum number of pieces that the ribbon can be cut into.
2368
+
2369
+
This problem follows the <b>[Unbounded Knapsack pattern](#pattern-2-unbounded-knapsack)</b> and is quite similar to <b>[Minimum Coin Change (MCC)](#minimum-coin-change)</b>. The only difference is that in <b>[Minimum Coin Change (MCC)](#minimum-coin-change)</b>, we were asked to find the <b>minimum</b> number of coin changes, whereas, in this problem, we need to find the <b>maximum</b> number of pieces.
2370
+
2351
2371
#### Example 1:
2352
2372
2353
2373
```
@@ -2374,10 +2394,6 @@ Ribbon Lengths: {3,5,7}
2374
2394
Output: 3
2375
2395
Explanation: Ribbon pieces will be {3,3,7}.
2376
2396
```
2377
-
##
2378
-
> Given a number array to represent possible `ribbonLengths` and a total ribbon length `n`, we need to find the maximum number of pieces that the ribbon can be cut into.
2379
-
2380
-
This problem follows the <b>[Unbounded Knapsack pattern](#pattern-2-unbounded-knapsack)</b> and is quite similar to <b>[Minimum Coin Change (MCC)](#minimum-coin-change)</b>. The only difference is that in <b>[Minimum Coin Change (MCC)](#minimum-coin-change)</b>, we were asked to find the <b>minimum</b> number of coin changes, whereas, in this problem, we need to find the <b>maximum</b> number of pieces.
2381
2397
2382
2398
### Basic Brute Force Solution
2383
2399
@@ -2505,15 +2521,24 @@ console.log(
2505
2521
2506
2522
# Pattern 3: Fibonacci Numbers
2507
2523
<b>Problem Set</b>
2524
+
2525
+
[Fibonacci numbers](#fibonacci-numbers)
2526
+
2527
+
[🔎👩🏽🦯 Staircase](#🔎👩🏽🦯-staircase)
2528
+
2529
+
[Number factors](#number-factors)
2530
+
2531
+
[🌴 Minimum jumps to reach the end](#🌴-minimum-jumps-to-reach-the-end)
2532
+
2533
+
[Minimum jumps with fee](#minimum-jumps-with-fee)
2534
+
2535
+
[🌴 🔎 👩🏽🦯 House thief](#🌴-🔎-👩🏽🦯-house-thief)
2536
+
2508
2537
[]()
2509
-
[]()
2510
-
[]()
2511
-
[]()
2512
-
[]()
2513
-
[]()
2514
-
[]()
2538
+
2515
2539
[]()
2516
2540
2541
+
2517
2542
## Fibonacci numbers
2518
2543
https://leetcode.com/problems/fibonacci-number/
2519
2544
@@ -3283,12 +3308,19 @@ We can clearly see that this problem follows the <b>[Fibonacci number pattern](#
0 commit comments