Stack & Queue
Bracket matching, monotonic stacks और deques, expression evaluation और queue डिज़ाइन।
DSAमध्यम34 घंटे10 पैटर्न · 54 प्रॉब्लम
0%
आपकी प्रगतिप्रॉब्लम और केस स्टडी के नाम, कोड और तकनीकी कीवर्ड अंग्रेज़ी में ही रखे गए हैं।
मूल विचार
Monotonic stack उम्मीदवारों को sorted क्रम में रखता है; जब नया element क्रम तोड़ता है, तो वह जिन्हें pop करता है उन्हें अपना जवाब मिल जाता है।
कब इस्तेमाल करें
- Brackets या nested structure का मिलान
- अगला / पिछला बड़ा या छोटा element
- Sliding-window maximum (monotonic deque)
- Expression evaluation
टेम्पलेट
def next_greater(nums):
res, stack = [-1] * len(nums), [] # stack of indices
for i, x in enumerate(nums):
while stack and nums[stack[-1]] < x:
res[stack.pop()] = x
stack.append(i)
return resComplexity
O(n) amortised — हर element अधिकतम एक बार push और pop होता है।
आम गलतियाँ
- Indices चाहिए (चौड़ाई/दूरी के लिए) तब values रखना
- Duplicates के साथ strict बनाम non-strict तुलना
- अंत में stack खाली करना भूल जाना
इस कोर्स के पैटर्न
Parentheses / Bracket Matching · 7Stack Simulation / String Processing · 8Expression Evaluation · 4Stack / Queue Design · 8Iterator (Stack-based) · 2Queue Simulation · 5Monotonic Stack – Next / Previous Greater or Smaller · 9Monotonic Stack – Greedy Removal · 2Monotonic Stack – Contribution / Area · 5Monotonic Queue / Deque · 4
पहचान के संकेत
ParenthesesMonotonic StackMonotonic QueueQueue/Stack Design