Greedy Algorithms
Classic Greedy Problems
Master classic Greedy array algorithms: Jump Game reach checks, Gas Station surplus traversals, and Fractional Knapsack density ratios.
Last Updated: August 2, 2026
•
18 min read
1. Introduction
What are Classic Greedy Problems?
Classic Greedy Problems are standard array and packing optimization tasks:0 to the last index.Why study them?
These problems are technical interview favorites. They demonstrate how tracking a single state variable (like maximum reachable index or fuel surplus) avoids complex recursion or dynamic programming states, running in optimalO(N) time.
2. Mental Model: The Dashboard Range Tracker
Imagine driving a car across a desert:
maxReachable). If your current location exceeds this distance, you are stranded.tank < 0) between station A and B, it means starting at any station from A to B is impossible. You must reset your start search to station B + 1.3. Core Algorithms & Implementations
1. Jump Game (LeetCode 55)
maxReachable = 0.i. If i > maxReachable, return false (you cannot reach this index).maxReachable = max(maxReachable, i + nums[i]).maxReachable >= n - 1, return true immediately.2. Gas Station (LeetCode 134)
-1.tank = 0 and startNode = 0. Iterate through stations. If tank += gas[i] - cost[i] < 0, it means starting at startNode failed. Reset tank = 0 and update startNode = i + 1.4. Visualizing Jump Game Pointers
Tracing array nums = [2, 3, 1, 1, 4]:
0, we can reach up to index 2.1, our reach expands to index 4, which matches the goal.5. Real-World Examples
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers test index arithmetic:startNode = i + 1 is correct. If starting at A fails at station B, then starting at any station between A and B will also fail because you would arrive at B with ≥ 0 gas, which was already not enough.maxReachable, currentEnd (end of current jump range), and jumps counter. Once i reaches currentEnd, increment jumps and update currentEnd = maxReachable.Common Mistakes
Warning: 1. Using DP / Recursion: Solving Jump Game or Gas Station with recursion or 2D Dynamic Programming. While correct, it takes
O(N²) time, which will result in a Time Limit Exceeded (TLE) on large inputs.> 2. Wrong index wrap-around bounds: Attempting to simulate circular index checks using nested loops ((i + j) % N), leading to complexO(N²)logic. The single passtotalGas >= totalCostcheck avoids this.
7. Summary
maxReachable boundary. Return false if i > maxReachable.O(N) time and O(1) space.8. Quiz
Question 1: What is the output of canJump([3, 2, 1, 0, 4])?
Answer:false. At index 3 (value 0), maxReachable remains 3. When loop index reaches 4 (4 > 3), we return false.
Question 2: In Gas Station, if totalGas >= totalCost, why is a unique solution guaranteed to exist?
Answer: Because the net sum of gas minus cost over the entire loop is non-negative (≥ 0). Thus, there must be at least one starting station where the cumulative sum never drops below zero.
Question 3: What is the difference between 0/1 Knapsack and Fractional Knapsack?
Answer: In 0/1 Knapsack, you must either take an item fully or leave it, which is NP-hard and requires DP. In Fractional Knapsack, you can take fractions of items, which can be solved greedily by sorting by value density.Question 4: True or False: Jump Game II can be solved in O(N) time.
Answer: True, using a greedy sliding window range tracker to update jump boundaries.Question 5: What is the space complexity of Gas Station?
Answer:O(1) auxiliary space, since we only track running scalar tank variables.