Summary: Find all pairs of 5-digit numbers that use the digits 0 through 9 each such that the first number divided by the second number is equal to N with 2 ≤ N ≤ 79: abdef/fghij = N
Naive Complete Search:
Both numbers are permuted from 10 digits (no replacement): 10! iterations ~ 3M
Analysis:
Max possible value: 98,765
Denominator fghij: max value = 98,765 / N, which is at most 49,382
Instead of checking pairs, go through all possible values of fghij and check if fghij * N is a numbers whose digits combining with fghij’s digits match 0 through 9 (bit masking technique):
Recall: to set (turn on) the bit at position j, use bitwise OR: S = S | (1 << j)
Time complexity: ~ 49,382 * 10 ~ 500K (comparing to 3M)
Make locally optimal choice at each step with the hope of eventually reaching the globally optimal solution.
Sliding windows is an example of greedy technique.
Works for some cases, not for many others.
Required characteristics
Optimal solution to the problem contains optimal solutions to the sub-problem
Has greedy property: if we make the choice that seems like best at the moment and proceed to the sub-problem, eventually we will reach the optimal solution.
Number of models for each garment, 1 ≤ K ≤ 20 , followed by price for each model (1 ≤ p ≤ K).
Output:
Either a single number or no solution.
Example test case 1:
M = 20, C = 3
g0: 3 6 4 8
g1: 2 5 10
g2: 4 1 5 3 5
Answer: 19 (8, 10, 1) or (6, 10, 3) or (4, 10, 5)
Example test case 2:
M = 9, C = 3
g0: 3 6 4 8
g1: 2 5 10
g2: 4 1 5 3 5
Answer: no solution
Non DP Approaches
Greedy Approach: Wrong Answer
Example test case 3:
M = 12, C = 3
g0: 3 6 4 8
g1: 2 5 10
g2: 4 1 5 3 5
Greedy principle: a optimal solution also contains a local solution for a sub-problem. Therefore we should pick the most expensive model for each garment.
Case (0,8): no budget for anything in g1lead to no solution.
Case (1,10): no budget for anything in g0 lead to no solution.
Case (2, 5): no budget for the third garment piece lead to no solution.
Correct answer: (4, 5, 3).
D&C approach: does not work because sub-problems are dependent on one another.
Complete Search:
20 possible garments, each has up to 20 possible models
C(20,20) ~ 1026
TLE
Top-down DP:
Does have the optimal sub-structure as a Greedy problem.
If we select model i for garment g = 0, for the final selection to be optimal, our choice for garment g = 1 and above must also be the optimal choice for a reduce budget M (minus cost of i).
Lack the greedy characteristics:
The costliest model i for g = 0 is not guaranteed to be the best selection.
Distinction between Greedy and DP: find counter-example.
Does have over-lapping sub-problem.
If there are two models in a certain garment g with the same price p.
A complete search will move to the same sub-problem (garment + 1, budget - p) after picking either model.
Similar scenario if some combination of budget and models’ prices lead to: b1 - p1 = b2 - p2
Overlapping: Pricing of each model can not exceed 20, and the maximum value for budget is 201.
Problem state: The current budget when a garment is selected.
Maximum possible search space: 20 * 201 = 4020 (comparing to 1026 of Complete Search)
Implementation
Add an efficient data structure (memo table) to map state to value. Initialize the table with dummy values.
At the start of the recursive function, check if the state has been computed.
If it has, return the value of the memo table.
If it has not, perform the computation as normal and store the computed value in the memo table.
Bottom-up DP:
True form of DP
DP is original known as tabular method.
Basic steps:
Determine the required set of parameters that uniquely describe the problem (the state).
If there are N parameters required to represent the states, prepare an N dimensional array with one entry per state.
Only initialize some cells (base cases).
With the base cases filled out, determine the cells/states that can be filled next.
Repeat the process until the DP table is complete.