mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 13:12:44 +00:00
docs(greedy): add Walk and Carry and Sort and Pick sections to Greedy Skeleton
This commit is contained in:
1 parent
fa74adeda7
commit
f2a2d2d7de
1 file changed
+24
@@ -54,6 +54,30 @@ hidden: true
|
||||
|
||||
## The Greedy Skeleton
|
||||
|
||||
### Type 1: Walk and Carry
|
||||
|
||||
:::info
|
||||
No sort. Walk the list once. Carry one number. At each step, decide: keep it, or drop it.
|
||||
:::
|
||||
|
||||
```python
|
||||
def walk_and_carry(nums):
|
||||
carry = start_value
|
||||
best = start_value
|
||||
for n in nums:
|
||||
if carry_is_useless(carry):
|
||||
carry = fresh_start
|
||||
carry = update(carry, n)
|
||||
best = max(best, carry)
|
||||
return best
|
||||
```
|
||||
|
||||
### Type 2: Sort and Pick
|
||||
|
||||
:::info
|
||||
Used when order doesn't matter, so you pick the order. Meeting rooms, intervals, "most tasks by a deadline."
|
||||
:::
|
||||
|
||||
```python
|
||||
def greedy(items):
|
||||
# 1. Sort by the greedy key (deadline, end time, size, ratio, ...)
|
||||
|
||||
Reference in new issue
Block a user