Greedy vs DP How to Know Which to Apply in DSA Rust - Complexity Comparison
We want to understand how to decide between greedy and dynamic programming approaches by looking at their time costs.
Which method grows faster as input size increases and why?
Analyze the time complexity of these two approaches solving the same problem.
// Greedy approach example (simplified)
fn greedy_solution(items: &[i32]) -> i32 {
let mut total = 0;
for &item in items {
if item > 0 {
total += item;
}
}
total
}
// Dynamic Programming example (simplified)
fn dp_solution(items: &[i32]) -> i32 {
let n = items.len();
let mut dp = vec![0; n + 1];
for i in 1..=n {
dp[i] = dp[i - 1] + if items[i - 1] > 0 { items[i - 1] } else { 0 };
}
dp[n]
}
Greedy picks positive items quickly; DP builds solutions step-by-step storing results.
Both use loops over the input array.
- Primary operation: Loop through all items once.
- How many times: Exactly once for both approaches.
Both approaches do one pass over the input, so operations grow directly with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: Operations increase linearly as input size grows.
Time Complexity: O(n)
This means the time to complete grows in direct proportion to the number of items.
[X] Wrong: "Greedy is always faster and simpler than dynamic programming."
[OK] Correct: Sometimes greedy fails to find the best answer and DP is needed despite similar time costs.
Knowing when to use greedy or dynamic programming shows your understanding of problem patterns and efficiency trade-offs.
"What if the problem required checking all combinations of items? How would the time complexity change?"
Practice
Solution
Step 1: Understand greedy algorithm conditions
Greedy algorithms work when choosing the best option at each step leads to a global optimum, which means the problem has an optimal substructure and no overlapping subproblems.Step 2: Compare with dynamic programming conditions
Dynamic programming is used when the problem has overlapping subproblems and optimal substructure, requiring storing intermediate results.Final Answer:
When the problem has an optimal substructure and no overlapping subproblems -> Option BQuick Check:
Greedy = optimal substructure + no overlap [OK]
- Confusing overlapping subproblems as a greedy condition
- Using DP when greedy suffices
- Assuming greedy always finds optimal solution
- Ignoring problem structure before choosing approach
Solution
Step 1: Identify correct vector declaration and sorting
let mut intervals = vec![(1,3), (2,5), (4,7)]; intervals.sort_by_key(|k| k.1); declares a mutable vector and sorts it by the second element (end time), which is needed for greedy interval scheduling.Step 2: Check other options for correctness
let intervals = vec![(1,3), (2,5), (4,7)]; intervals.sort(); tries to sort an immutable vector and uses default sort (by first element), let mut intervals = vec![(1,3), (2,5), (4,7)]; intervals.sort_by(|a,b| a.0.cmp(&b.0)); sorts by start time, not end time, and let intervals = vec![(1,3), (2,5), (4,7)]; intervals.reverse(); reverses without sorting.Final Answer:
let mut intervals = vec![(1,3), (2,5), (4,7)]; intervals.sort_by_key(|k| k.1); -> Option DQuick Check:
Sort intervals by end time with mutable vector [OK]
- Sorting by start time instead of end time
- Using immutable vector when sorting is needed
- Not sorting intervals before greedy selection
- Using reverse instead of sort
let mut intervals = vec![(1,3), (2,5), (4,7)];
intervals.sort_by_key(|k| k.1);
let mut count = 0;
let mut end = 0;
for &(start, finish) in &intervals {
if start >= end {
count += 1;
end = finish;
}
}
println!("{}", count);Solution
Step 1: Sort intervals by end time
Sorted intervals: (1,3), (2,5), (4,7)Step 2: Iterate and count non-overlapping intervals
First interval (1,3): start=1 >= end=0, count=1, end=3 Second interval (2,5): start=2 < end=3, skip Third interval (4,7): start=4 >= end=3, count=2, end=7Final Answer:
2 -> Option AQuick Check:
Greedy interval count = 2 [OK]
- Counting overlapping intervals
- Not updating end after selection
- Sorting incorrectly
- Confusing start and end values
let coins = vec![1, 3, 4];
let mut amount = 6;
let mut count = 0;
for &coin in coins.iter().rev() {
while amount >= coin {
amount -= coin;
count += 1;
}
}
println!("{}", count);Solution
Step 1: Understand greedy coin change limitation
Greedy works only if coin denominations are canonical (like 1,5,10). Here, 1,3,4 can cause greedy to fail.Step 2: Check code behavior
Greedy picks 4 once (amount=2), then picks 1 twice (total 3 coins). But optimal is 2 coins (3+3).Final Answer:
Greedy fails because coin denominations do not guarantee optimal solution -> Option CQuick Check:
Greedy fails on non-canonical coins [OK]
- Assuming greedy always works for coin change
- Ignoring coin order importance
- Not checking for optimal substructure
- Confusing mutability of variables
Solution
Step 1: Analyze greedy approach limitations
Greedy may pick 4 then 1s, resulting in 3 coins, but optimal is 2 coins (3+3).Step 2: Understand dynamic programming advantage
DP stores minimum coins needed for each amount, ensuring optimal solution even with tricky coin sets.Step 3: Compare other options
Brute force is slow; sorting ascending and picking smallest first is not optimal strategy.Final Answer:
Dynamic programming storing minimum coins for each amount -> Option AQuick Check:
DP handles overlapping subproblems for optimal coin count [OK]
- Using greedy for non-canonical coin sets
- Ignoring performance of brute force
- Assuming sorting smallest first helps
- Not recognizing overlapping subproblems
Ready to prove it?
Pass the test and this pattern moves unknown
