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