Design a Stack With Increment Operation | Better Approach | O(1) | Leetcode 1381 | codestorywithMIK
Whatsapp Community Link : https://www.whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A
This is the 18th Video of our Playlist "Design Data Struture : Popular Interview Problems" by codestorywithMIK
Design Data Structure Qns Playlist - • Insert Delete GetRandom O(1) | Detail...
Excel Sheet - https://docs.google.com/document/d/1Hqdu5bivW6kHBSr-hGyvO2kYOKkyTtM3qmhmHYH7s7o
In this video we will try to solve a good Design DSA Problem : Design a Stack With Increment Operation | Better Approach | O(1) Time | Leetcode 1381 | codestorywithMIK
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Design a Stack With Increment Operation | Better Approach | O(1) Time | Leetcode 1381 | codestorywithMIK
Company Tags : LinkedIn
My solutions on Github(C++ & JAVA) - https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Design/Design a Stack With Increment Operation.cpp
Leetcode Link : https://leetcode.com/problems/design-a-stack-with-increment-operation
My DP Concepts Playlist : • Roadmap for DP | How to Start DP ? | ...
My Graph Concepts Playlist : • Graph Concepts & Qns - 1 : Graph will...
My Recursion Concepts Playlist : • Introduction | Recursion Concepts And...
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Interview_DS_Algo
Instagram : https://www.instagram.com/codestorywithmik/
Facebook : https://www.facebook.com/people/codestorywithmik/100090524295846/
Twitter : https://twitter.com/CSwithMIK
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
Initialization (CustomStack(int maxSize)):
The constructor initializes a maximum stack size (n) and two vectors:
st: Holds the elements in the stack.
increments: Keeps track of incremental values for each element, which will be added during the pop operation.
Push Operation (void push(int x)):
If the stack has not reached its maximum size (n), the new element x is pushed onto st, and a corresponding 0 is added to increments.
Pop Operation (int pop()):
The pop operation removes and returns the top element from the stack. If the stack is empty, it returns -1.
It adds the current top's increment value to the next-to-top element’s increment, effectively propagating the increment downwards.
Returns the value of the top element adjusted by its corresponding increment.
Increment Operation (void increment(int k, int val)):
This method adds val to the first k elements in the stack (or the entire stack if k is larger than the stack size).
The increment is added to the increments vector at index min(k-1, st.size()-1), meaning the effect is propagated when elements are popped.
Key Design Points:
The increments vector enables efficient management of the increment operation, as it avoids incrementing all elements individually and instead defers the update until a pop occurs.
The time complexity for push, pop, and increment operations is constant (O(1)), making this approach very efficient for stack operations with increments.
✨ Timelines✨
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #coding #programming #100daysofcode #developers #techjobs #datastructures #algorithms #webdevelopment #softwareengineering #computerscience #pythoncoding #codinglife #coderlife #javascript #datascience #leetcode #leetcodesolutions #leetcodedailychallenge #codinginterview #interviewprep #technicalinterview #interviewtips #interviewquestions #codingchallenges #interviewready #dsa #hindi #india #hindicoding #hindiprogramming #hindiexplanation #hindidevelopers #hinditech #hindilearning #helpajobseeker #jobseekers #jobsearchtips #careergoals #careerdevelopment #jobhunt #jobinterview #github #designthinking #learningtogether #growthmindset #digitalcontent #techcontent #socialmediagrowth #contentcreation #instagramreels #videomarketing #codestorywithmik #codestorywithmick #codestorywithmikc #codestorywitmik #codestorywthmik #codstorywithmik #codestorywihmik #codestorywithmiik #codeistorywithmik #codestorywithmk #codestorywitmick #codestorymik #codestorwithmik