Greedy techniques Flashcards
(5 cards)
What is the pattern and when is it used in Greedy Techniques?
Makes locally optimal choices for global solution.
Use Case: Scheduling, minimum operations.
Example: [No LeetCode match, course example: Jump Game].
Action: Verbalize use case aloud.
What are the key steps in Greedy Techniques?
- Identify optimal choice at each step.
- Apply choice, update state.
- Repeat until solution reached.
Action: Explain steps for Jump Game aloud.
How does Greedy Techniques apply to Jump Game?
Determine if end reachable in array jumps.
Approach: Track max reachable index, greedily update.
Example: Jump Game. Action: Verbalize solution logic aloud.
What are the complexity and gotchas in Greedy Techniques?
Time: O(n), Space: O(1).
Gotchas: Non-greedy problems, invalid assumptions.
Action: List edge cases for Jump Game aloud.
Provide a visual or code example for Greedy Techniques.
```python
from typing import List
def can_jump(nums: List[int]) -> bool:
max_reach = 0
for i in range(len(nums)):
if i > max_reach:
return False
max_reach = max(max_reach, i + nums[i])
if max_reach >= len(nums) - 1:
return True
return True
~~~
Action: Sketch jump path on paper.