Greedy vs DP How to Know Which to Apply in DSA C - Complexity Comparison
Choosing between Greedy and Dynamic Programming affects how fast your solution runs.
We want to know how the time needed grows as the problem size grows.
Analyze the time complexity of these two approaches solving the same problem.
// Greedy approach example
int greedySolution(int arr[], int n) {
int result = 0;
for (int i = 0; i < n; i++) {
if (canTake(arr[i])) {
result += arr[i];
}
}
return result;
}
// Dynamic Programming example
int dpSolution(int arr[], int n) {
int dp[n+1];
dp[0] = 0;
for (int i = 1; i <= n; i++) {
dp[i] = max(dp[i-1], (i >= 2 ? dp[i-2] : 0) + arr[i-1]);
}
return dp[n];
}
The first tries a simple choice each step; the second builds answers using past results.
Look at loops and repeated calculations.
- Primary operation: Both use a single loop over the input array.
- How many times: Each loop runs exactly n times, where n is input size.
Both approaches do work proportional to the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The time grows linearly as input size grows.
Time Complexity: O(n)
This means the time needed grows in direct proportion to the input size.
[X] Wrong: "Greedy always runs faster and is always better than DP."
[OK] Correct: Both can have similar time costs; the difference is in correctness, not speed.
Understanding when to use Greedy or DP shows you can pick the right tool, a key skill interviewers look for.
"What if the problem had overlapping subproblems but no greedy choice property? How would the time complexity change if you tried greedy instead of DP?"
Practice
Solution
Step 1: Understand greedy choice property
A greedy algorithm works when choosing the best option at each step leads to a global optimum.Step 2: Compare with dynamic programming conditions
Dynamic programming is used when subproblems overlap and greedy choice property does not hold.Final Answer:
When the problem has the greedy choice property and optimal substructure -> Option AQuick Check:
Greedy choice property = Use greedy [OK]
- Confusing overlapping subproblems with greedy choice
- Using greedy when problem needs full exploration
- Assuming sorting problems always need greedy
Solution
Step 1: Recall C array declaration syntax
In C, arrays are declared with fixed size and optional initialization using braces.Step 2: Check each option
int memo[1000] = {0}; correctly declares an int array of size 1000 initialized to zero. Others are invalid C syntax.Final Answer:
int memo[1000] = {0}; -> Option AQuick Check:
C arrays use [] with size [OK]
- Using parentheses instead of brackets for arrays
- Trying to assign 0 directly to array
- Using new keyword in C (it's C++ syntax)
int coins[] = {1, 5, 10, 25};
int amount = 30;
int count = 0;
for (int i = 3; i >= 0; i--) {
while (amount >= coins[i]) {
amount -= coins[i];
count++;
}
}
printf("%d\n", count);Solution
Step 1: Trace the greedy coin selection
Start with coin 25: amount=30, 30>=25, subtract 25, amount=5, count=1.Step 2: Continue with smaller coins
Next coin 10: amount=5<10, skip. Next coin 5: amount=5>=5, subtract 5, amount=0, count=2.Step 3: No more coins needed
Loop ends, total coins used = 2.Final Answer:
2 -> Option CQuick Check:
25 + 5 = 30 using 2 coins [OK]
- Counting coins incorrectly in loops
- Not updating amount after subtraction
- Confusing coin order in loop
int fib(int n) {
if (n <= 1) return n;
int a = 0, b = 1, c = 0;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}Solution
Step 1: Understand Fibonacci calculation
Fibonacci requires summing previous two numbers, which is a classic dynamic programming problem.Step 2: Identify greedy approach misuse
The code uses iteration correctly but calling it greedy is wrong because Fibonacci does not have greedy choice property.Final Answer:
Greedy approach is incorrect for Fibonacci calculation -> Option DQuick Check:
Fibonacci needs DP, not greedy [OK]
- Thinking iterative is greedy
- Ignoring problem properties
- Confusing initialization with error
Solution
Step 1: Test greedy approach on example
For amount 6, greedy picks 4 + 1 + 1 = 3 coins, but optimal is 3 + 3 = 2 coins.Step 2: Recognize overlapping subproblems
Dynamic programming stores solutions for smaller amounts to build up answer correctly.Step 3: Conclude best approach
Greedy fails due to lack of greedy choice property; DP handles all cases optimally.Final Answer:
Dynamic Programming, because greedy fails for some amounts -> Option BQuick Check:
Greedy fails, DP works [OK]
- Assuming greedy always works for coin change
- Ignoring counterexamples
- Choosing brute force unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
