Skip to content

Commit 4bc6889

Browse files
authored
Update dynamic_programming.md
1 parent 1b15781 commit 4bc6889

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

notes/dynamic_programming.md

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -292,27 +292,40 @@ We build a two-dimensional table $L[0..m][0..n]$ using the above recurrence.
292292

293293
#### Identifying DP Problems
294294

295-
* If the problem asks for the number of *ways* to do something:
296-
* Example: Counting paths in a grid.
297-
* Consequence: Without DP, you would need to enumerate every route.
298-
* If the task is to find the *minimum* or *maximum* value under constraints:
299-
* Example: Knapsack problem.
300-
* Consequence: Without DP, you would need to check every subset of items.
301-
* If the same *inputs* appear again during recursion:
302-
* Example: Fibonacci numbers.
303-
* Consequence: Without DP, Fibonacci numbers would be recomputed many times.
304-
* If the solution depends on both the *current step* and *remaining resources* (time, weight, money, length):
305-
* Example: Scheduling tasks within a time limit.
306-
* Consequence: Without DP, brute force would be required.
307-
* If the problem works with *prefixes, substrings, or subsequences*:
308-
* Example: Longest common subsequence.
309-
* Consequence: Without DP, exponential checking would be needed.
310-
* If choices at each step must be explored and combined carefully:
311-
* Example: Coin change with mixed denominations.
312-
* Consequence: Without DP, you cannot guarantee the fewest coins.
313-
* If the state space can be stored in a *table or array*:
314-
* Example: Problems with discrete states.
315-
* Consequence: Without this, problems with infinitely many possibilities (like arbitrary real numbers) cannot be handled.
295+
I. If the problem asks for the number of *ways* to do something:
296+
297+
* *Example:* Counting paths in a grid.
298+
* *Consequence:* Without DP, you would need to enumerate every route.
299+
300+
II. If the task is to find the *minimum* or *maximum* value under constraints:
301+
302+
* *Example:* Knapsack problem.
303+
* *Consequence:* Without DP, you would need to check every subset of items.
304+
305+
III. If the same *inputs* appear again during recursion:
306+
307+
* *Example:* Fibonacci numbers.
308+
* *Consequence:* Without DP, Fibonacci numbers would be recomputed many times.
309+
310+
IV. If the solution depends on both the *current step* and *remaining resources* (time, weight, money, length):
311+
312+
* *Example:* Scheduling tasks within a time limit.
313+
* *Consequence:* Without DP, brute force would be required.
314+
315+
V. If the problem works with *prefixes, substrings, or subsequences*:
316+
317+
* *Example:* Longest common subsequence.
318+
* *Consequence:* Without DP, exponential checking would be needed.
319+
320+
VI. If choices at each step must be explored and combined carefully:
321+
322+
* *Example:* Coin change with mixed denominations.
323+
* *Consequence:* Without DP, you cannot guarantee the fewest coins.
324+
325+
VII. If the state space can be stored in a *table or array*:
326+
327+
* *Example:* Problems with discrete states.
328+
* *Consequence:* Without this, problems with infinitely many possibilities (like arbitrary real numbers) cannot be handled.
316329

317330
#### State Design and Transition
318331

@@ -326,9 +339,20 @@ We build a two-dimensional table $L[0..m][0..n]$ using the above recurrence.
326339

327340
#### Common Pitfalls
328341

329-
* Missing *base cases* causes results to fail, while including them ensures correct foundations; in grid path counting, setting `dp[0][0] = 1` allows all later counts to build properly.
330-
* Updating *dependencies* in the wrong order leads to invalid reuse, while correct order avoids errors; in knapsack with a 1D array, iterating weights backward prevents an item from being counted twice.
331-
* Ignoring *edge inputs* results in crashes or incorrect answers, while handling them ensures robustness; for example, knapsack with zero capacity must return a value of zero instead of failing.
342+
I. Failure to Define Proper Base Cases
343+
344+
* *Example*: In grid path counting, omitting `dp[0][0] = 1` prevents any valid paths from being constructed.
345+
* *Consequence*: Without correct starting values, the DP table propagates errors and produces incorrect results.
346+
347+
II. Updating States in the Wrong Dependency Order
348+
349+
* *Example*: In knapsack with a 1D array, iterating weights from low to high causes items to be reused multiple times.
350+
* *Consequence*: Using the wrong order inflates computed values and leads to invalid or impossible solutions.
351+
352+
III. Ignoring Special or Edge Case Inputs
353+
354+
* *Example*: In knapsack, a zero-capacity input should return zero value rather than throwing an error.
355+
* *Consequence*: Overlooking edge inputs causes crashes or incorrect answers in boundary conditions.
332356

333357
### List of Problems
334358

0 commit comments

Comments
 (0)