Why Greedy Works and When It Fails in DSA Java - Performance Analysis
We want to understand how fast greedy algorithms run and why they sometimes give the best answer quickly.
How does the number of steps grow as the problem size grows?
Analyze the time complexity of the following greedy approach for the coin change problem.
public int greedyCoinChange(int[] coins, int amount) {
int count = 0;
for (int i = coins.length - 1; i >= 0; i--) {
while (amount >= coins[i]) {
amount -= coins[i];
count++;
}
}
return count;
}
This code tries to make change by always using the largest coin possible first.
Look for loops and repeated steps.
- Primary operation: The inner while loop subtracting coins from amount.
- How many times: Up to amount divided by smallest coin value times.
As the amount grows, the number of subtractions grows roughly in proportion.
| Input Size (amount) | Approx. Operations |
|---|---|
| 10 | About 10 subtractions if smallest coin is 1 |
| 100 | About 100 subtractions |
| 1000 | About 1000 subtractions |
Pattern observation: The steps grow roughly linearly with the amount.
Time Complexity: O(n + amount / c_min)
This means the time grows with the number of coin types and how many times the smallest coin fits into the amount.
[X] Wrong: "Greedy always finds the best answer quickly."
[OK] Correct: Sometimes greedy picks local best choices that don't lead to the overall best solution, especially with tricky coin sets.
Understanding when greedy works helps you explain your approach clearly and choose the right method in real problems.
"What if the coin denominations are not sorted or include unusual values? How would that affect the time complexity and correctness?"
Practice
Solution
Step 1: Understand greedy choice property
A greedy algorithm chooses the best option available at each step without reconsidering previous choices.Step 2: Connect greedy choice to global optimum
This local best choice leads to a global optimum only if the problem has the greedy choice property.Final Answer:
Because it makes the best choice at each step hoping for a global optimum -> Option CQuick Check:
Greedy choice property = Because it makes the best choice at each step hoping for a global optimum [OK]
- Thinking greedy tries all solutions
- Confusing greedy with recursion
- Assuming sorting alone makes greedy correct
arr of length n?Solution
Step 1: Identify Java loop syntax
Java uses for loops with initialization, condition, and increment: for (int i = 0; i < n; i++)Step 2: Check options for Java correctness
for (int i = 0; i < n; i++) matches Java syntax; B is Python, C is incomplete and incorrect, D is incorrect syntax for array iteration.Final Answer:
for (int i = 0; i < n; i++) -> Option DQuick Check:
Java for loop syntax = for (int i = 0; i < n; i++) [OK]
- Using Python syntax in Java
- Incorrect loop conditions
- Misusing foreach syntax
int[][] intervals = {{1,3},{2,5},{4,7}};
Arrays.sort(intervals, (a,b) -> a[1] - b[1]);
int count = 0, end = 0;
for (int[] interval : intervals) {
if (interval[0] >= end) {
count++;
end = interval[1];
}
}
System.out.println(count);
What is the output?Solution
Step 1: Sort intervals by finish time
Sorted intervals by end time: {1,3}, {2,5}, {4,7}Step 2: Select intervals greedily
Select {1,3} first (count=1, end=3). Next {2,5} starts at 2 < 3, skip. Next {4,7} starts at 4 >= 3, select (count=2).Final Answer:
2 -> Option BQuick Check:
Greedy interval selection = 2 [OK]
- Not sorting intervals
- Selecting overlapping intervals
- Counting all intervals without check
int[] coins = {1, 3, 4};
int amount = 6;
int count = 0;
for (int i = coins.length - 1; i >= 0; i--) {
while (amount >= coins[i]) {
amount -= coins[i];
count++;
}
}
System.out.println(count);
What is the output and what is the problem with this approach?Solution
Step 1: Trace greedy coin selection
Start with coin 4: amount=6>=4, subtract 4 (amount=2), count=1. Next coin 3: amount=2<3 skip. Next coin 1: amount=2>=1 subtract twice, count=3 total coins.Step 2: Check minimum coins possible
Minimum coins for 6 are 2 (3+3), but greedy picks 3 coins (4+1+1). Greedy fails here.Final Answer:
3; Greedy fails because it doesn't find minimum coins -> Option AQuick Check:
Greedy coin change fails = 3; Greedy fails because it doesn't find minimum coins [OK]
- Assuming greedy always finds minimum coins
- Ignoring coin order importance
- Confusing output count with amount
int[][] meetings = {{0, 30}, {5, 10}, {15, 20}, {25, 35}}. Which greedy strategy will always give the optimal solution and why?Solution
Step 1: Understand meeting scheduling problem
Goal is to maximize non-overlapping meetings in one room.Step 2: Analyze greedy strategies
Choosing earliest finish time frees room earliest, allowing more meetings after.Step 3: Check other strategies
Earliest start or shortest duration can cause overlaps or miss optimal sets.Final Answer:
Select meetings by earliest finish time because it leaves room for more meetings -> Option AQuick Check:
Earliest finish time = optimal scheduling [OK]
- Choosing earliest start time
- Picking shortest duration meetings
- Ignoring finish times
Ready to prove it?
Pass the test and this pattern moves unknown
