Greedy Cheat Sheet
Quick reference for coding interviews. Bookmark this page!
What is Greedy?
Make locally optimal choices for globally optimal solutions.
Trigger Words
When you see these in a problem, think Greedy:
Template Code
def maxSubArray(nums: List[int]) -> int:
max_sum = nums[0]
current_sum = nums[0]
for i in range(1, len(nums)):
# Either extend current subarray or start new
current_sum = max(nums[i], current_sum + nums[i])
max_sum = max(max_sum, current_sum)
return max_sum
Complexity
| Typical Time | O(n) |
|---|---|
| Typical Space | O(1) |
Common Variations
- Basic Greedy
- Greedy with constraints
- Optimized Greedy
Practice Problems
See all Greedy problems →Practice Greedy
Learn to recognize patterns instantly with interactive MCQs.
Download LeetEye Free