AI EducademyAIEducademy
🌳

AI की नींव

🌱
AI Seeds

शून्य से शुरू करें

🌿
AI Sprouts

नींव बनाएं

🌳
AI Branches

व्यवहार में लागू करें

🏕️
AI Canopy

गहराई में जाएं

🌲
AI Forest

AI में महारत हासिल करें

🔨

AI में महारत

✏️
AI Sketch

शून्य से शुरू करें

🪨
AI Chisel

नींव बनाएं

⚒️
AI Craft

व्यवहार में लागू करें

💎
AI Polish

गहराई में जाएं

🏆
AI Masterpiece

AI में महारत हासिल करें

🚀

करियर रेडी

🚀
इंटरव्यू लॉन्चपैड

अपनी यात्रा शुरू करें

🌟
व्यवहारिक इंटरव्यू में महारत

सॉफ्ट स्किल्स में महारत

💻
तकनीकी इंटरव्यू

कोडिंग राउंड में सफल हों

🤖
AI और ML इंटरव्यू

ML इंटरव्यू में महारत

🏆
ऑफर और उससे आगे

सबसे अच्छा ऑफर पाएं

सभी कार्यक्रम देखें→

लैब

7 प्रयोग लोड हुए
🧠न्यूरल नेटवर्क प्लेग्राउंड🤖AI या इंसान?💬प्रॉम्प्ट लैब🎨इमेज जनरेटर😊सेंटिमेंट एनालाइज़र💡चैटबॉट बिल्डर⚖️एथिक्स सिमुलेटर
🎯मॉक इंटरव्यूलैब में जाएँ→
nav.journeyब्लॉग
🎯
हमारे बारे में

हर जगह, हर किसी के लिए AI शिक्षा सुलभ बनाना

❓
nav.faq

Common questions answered

✉️
Contact

Get in touch with us

⭐
ओपन सोर्स

GitHub पर सार्वजनिक रूप से निर्मित

सीखना शुरू करें - यह मुफ्त है
AI EducademyAIEducademy

MIT लाइसेंस - ओपन सोर्स

सीखें

  • कार्यक्रम
  • पाठ
  • लैब

समुदाय

  • GitHub
  • योगदान करें
  • आचार संहिता
  • हमारे बारे में
  • सामान्य प्रश्न

सहायता

  • कॉफ़ी खरीदें ☕
  • footer.terms
  • footer.privacy
  • footer.contact
AI और इंजीनियरिंग प्रोग्राम›✏️ AI Sketch›पाठ›Greedy Algorithms
🏃
AI Sketch • मध्यम⏱️ 17 मिनट पढ़ने का समय

Greedy Algorithms

Greedy Algorithm क्या होता है?

Greedy algorithm solution एक step एक बार बनाता है, हमेशा वो option चुनता है जो अभी सबसे अच्छा लगे - बिना पुराने decisions पर दोबारा सोचे। न backtracking, न आगे देखना।

यह तब काम करता है जब दो properties हों:

  1. Greedy choice property - locally optimal choice globally optimal solution तक ले जाए।
  2. Optimal substructure - हर choice के बाद बचा problem उसी problem का छोटा version हो।

Activity Selection - Classic Example

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 के लिए सबसे ज़्यादा जगह बचती है।

Timeline पर earliest end time से activity selection दिखा रहा है
Earliest end time से select करने पर non-overlapping activities की संख्या maximum होती है।

Huffman Coding - Concept

Optimal prefix-free binary code बनाने के लिए बार-बार दो सबसे कम frequency वाले symbols merge करो। यह greedy है: हर step पर सबसे सस्ती pair merge करो। Result एक tree है जहाँ frequent symbols को short codes मिलते हैं।

🤯
Huffman coding JPEG, MP3, और ZIP compression में इस्तेमाल होती है। David Huffman ने इसे 1952 में एक student के रूप में invent किया - यह एक homework assignment था!

Jump Game

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 करता है?

पाठ 9 / 100% पूर्ण
←Recursion और Backtracking

Discussion

Sign in to join the discussion

lessons.suggestEdit

Gas Station

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 उसके बाद से शुरू होगा।

Task Scheduler

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
🤔
Think about it:Task scheduler में सबसे frequent task पहले चुनने से idle slots कम क्यों होते हैं? अगर सबसे कम frequent पहले चुनें तो क्या गलत होगा?

Fractional Knapsack vs 0/1 Knapsack

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 कब काम करता है?

✅ काम करता है जब greedy choice property prove हो सके:

  • Interval scheduling, Huffman coding, minimum spanning trees, Dijkstra's algorithm

❌ Fail जब local optima global optima न बनाएँ:

  • 0/1 knapsack, longest path, travelling salesman

Greedy Correctness - Exchange Argument

Standard technique: मान लो एक optimal solution है जो greedy choice नहीं इस्तेमाल करता। दिखाओ कि उसकी एक choice को greedy choice से exchange करने पर solution बुरा नहीं होता। इससे साबित होता है greedy कम से कम उतना ही अच्छा है।

🧠त्वरित जांच

'Exchange argument' किसलिए इस्तेमाल होता है?

Greedy vs Dynamic Programming

| पहलू | 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 करो।

🤯
Dijkstra's shortest-path algorithm greedy है - हमेशा closest unvisited node process करता है। यह काम करता है क्योंकि edge weights non-negative हैं, जो greedy choice को safe बनाता है।
🤔
Think about it:Coin denominations [1, 3, 4] हैं और 6 के लिए change बनाना है। Greedy 4+1+1=3 coins चुनता है, लेकिन optimal 3+3=2 coins है। इन denominations में greedy choice property क्यों टूटती है?

📚 और पढ़ें

  • NeetCode - Greedy playlist - curated greedy problems with explanations
  • Tech Interview Handbook - Greedy - कब greedy इस्तेमाल करें और common patterns
  • CP-Algorithms - Greedy algorithms - गहरी theory और proofs