Greedy algorithm solution एक step एक बार बनाता है, हमेशा वो option चुनता है जो अभी सबसे अच्छा लगे - बिना पुराने decisions पर दोबारा सोचे। न backtracking, न आगे देखना।
यह तब काम करता है जब दो properties हों:
Activities दी हैं जिनके start और end times हैं। Maximum non-overlapping activities चुनो।
Greedy strategy: end time से sort करो, हमेशा सबसे पहले खत्म होने वाली activity चुनो।
def max_activities(intervals):
intervals.sort(key=lambda x: x[1])
count, end = 0, 0
for s, e in intervals:
if s >= end:
count += 1
end = e
return count
यह क्यों काम करता है? सबसे पहले खत्म होने वाली activity चुनने से बाकी activities के लिए सबसे ज़्यादा जगह बचती है।
Optimal prefix-free binary code बनाने के लिए बार-बार दो सबसे कम frequency वाले symbols merge करो। यह greedy है: हर step पर सबसे सस्ती pair merge करो। Result एक tree है जहाँ frequent symbols को short codes मिलते हैं।
Array दिया है जहाँ nums[i] index i से maximum jump length है। बताओ last index तक पहुँच सकते हो या नहीं।
Greedy approach: अब तक पहुँचने योग्य farthest index track करो। Left से right scan करो - अगर कभी पीछे रह गए, return false।
def can_jump(nums):
farthest = 0
for i, jump in enumerate(nums):
if i > farthest:
return False
farthest = max(farthest, i + jump)
return True
Jump Game में greedy variable 'farthest' क्या represent करता है?
Sign in to join the discussion
Circular route पर gas stations हैं। Station i पर gas[i] fuel मिलता है और अगले station तक cost[i] खर्च होता है। वो starting station ढूँढो जिससे पूरा circuit complete हो, या −1 return करो।
Greedy insight: अगर total gas ≥ total cost, तो solution ज़रूर है। Running surplus track करो; जब भी यह negative हो, answer उसके बाद से शुरू होगा।
Tasks schedule करने हैं जिनमें identical tasks के बीच n cooldown हो। Greedy: हमेशा सबसे frequent remaining task चुनो।
Tasks: A A A B B C, cooldown = 2
Schedule: A B C A B _ A
Total = 7
Fractional knapsack: items के टुकड़े ले सकते हो। Greedy काम करता है - value-per-weight से sort करो, best ratio पहले लो।
0/1 knapsack: item पूरा लो या छोड़ो। Greedy यहाँ fail करता है क्योंकि भारी-but-valuable item छोड़ना दो हल्के items लेने से बुरा हो सकता है। इसके लिए dynamic programming चाहिए।
Items: (weight=3, value=4), (weight=2, value=3), (weight=2, value=3)
Capacity: 4
Greedy (best ratio first): item 1 (w=3, v=4) → 1 बचा → बाकी fit नहीं → value = 4
Optimal: items 2+3 (w=4, v=6) → value = 6 ✗ Greedy fail!
0/1 knapsack में greedy approach fail क्यों करता है?
✅ काम करता है जब greedy choice property prove हो सके:
❌ Fail जब local optima global optima न बनाएँ:
Standard technique: मान लो एक optimal solution है जो greedy choice नहीं इस्तेमाल करता। दिखाओ कि उसकी एक choice को greedy choice से exchange करने पर solution बुरा नहीं होता। इससे साबित होता है greedy कम से कम उतना ही अच्छा है।
'Exchange argument' किसलिए इस्तेमाल होता है?
| पहलू | Greedy | Dynamic Programming | |-------|--------|---------------------| | Choices | हर step पर एक best choice | सभी sub-problems explore करो | | Past revisit? | कभी नहीं | हाँ - sub-results store करता है | | Speed | आमतौर पर तेज़ | State space पर निर्भर | | Correctness | सिर्फ अगर prove हो | हमेशा (सही formulation पर) |
Doubt हो तो greedy से शुरू करो - prove नहीं कर पाए तो DP पर switch करो।