Greedy trains you to think: “What is the most progress I can make right now without ruining the future?” — a skill that transfers well to DP state transitions, graph traversal heuristics, and resource allocation problems.
https://leetcode.com/problems/lemonade-change/
Last review: Aug 29, 2025 Next review: You got a little hint to solve it, maybe, practice again.
O(n)O(1)func lemonadeChange(bills []int) bool {
// give the change before earning the profit
var note5 int
var note10 int
for _, bill := range bills {
switch bill {
case 5:
note5++
case 10:
if note5 < 1 {
return false
}
note5--
note10++
case 20:
if note5 < 3 && (note5 < 1 || note10 < 1) {
return false
}
if note5 >= 1 && note10 >= 1 {
note5--
note10--
} else {
note5 -= 3
}
}
}
return true
}
https://leetcode.com/problems/can-place-flowers/description/
Last review: Feb 13, 2026 Next review: .
O(n!)
O(n)func canPlaceFlowers(flowerbed []int, n int) bool {
if n == 0 {
return true
}
length := len(flowerbed)
noPathCache := make(map[int]bool)
var dfs func(i, planted int) bool
dfs = func(i, planted int) bool {
for j := i; j < length; j++ {
if flowerbed[j] == 1 ||
(j > 0 && flowerbed[j-1] == 1) ||
(j < length-1 && flowerbed[j+1] == 1) ||
noPathCache[j] {
continue
}
planted++
flowerbed[j] = 1
if planted == n {
return true
}
if dfs(j+1, planted) {
return true
}
planted--
flowerbed[j] = 0
noPathCache[j] = true
}
return false
}
return dfs(0, 0)
}
3 contiguous empty pots can plant a flower.
When we see three consecutive empty plots (0 0 0), plant immediately in the middle.
[1,0,1] -- X
[1,0,0,1] -- X
[1,0,0,0,1] -- O
About edge cases: