Design a Stack with Increment operation
Problem Statement: Design a stack that supports increment operations on its elements. Implement the `CustomStack` class with the following functionalities: - `push(int x)`: Adds `x` to the top of the stack if it hasn't reached its maximum size. - `pop()`: Removes and returns the top element of the stack or returns `-1` if the stack is empty. - `increment(int k, int val)`: Increments the bottom `k` elements of the stack by `val`. If the stack has fewer than `k` elements, increment all of them. Example: Given the following operations: 1. `push(1)` 2. `push(2)` 3. `pop()` ➔ Returns `2` 4. `increment(2, 100)` ➔ The stack becomes `[101, 102]` 5. `pop()` ➔ Returns `101` Implementation: - Use an array to represent the stack. - Maintain a variable to track the current size of the stack. - Ensure all methods operate efficiently within the constraints. Time and Space Complexity: - Push Operation: O(1) time complexity, O(n) space complexity in the worst case (if the stack reaches max size). - Pop Operation: O(1) time complexity, O(1) space complexity. - Increment Operation: O(k) time complexity, where k is the number of elements to increment. Space complexity is O(1) as no additional space is used apart from the input parameters. 👉 Don’t just watch—get involved! Drop your solutions and questions in the comments. Let’s solve problems together! 💬 Got a specific question? Comment below, and I’ll cover it in a future video! Like 👍 and Subscribe to stay updated with daily coding challenges! #coding #potd #java #datastructures #algorithms #dailyquestion #leetcode #leetcodejava #javaprogramming #programming #developer #tech #softwareengineering #codingchallenge #codinglife #javadeveloper #stack #data #problemSolving #codenewbie #codewars #java8 #javacoding #programmer #coder #devcommunity #algorithmsanddatastructures #coders #codelife #javalove #learnprogramming #techcommunity #softwaredeveloper #programminglife #javaclasses #javaexample #tutorial #codewithme #devlife #computer #computerscience #codingisfun #software #education #techworld #programmingtutorial #codingforall #dataanalysis #javaapplication #codelessons #stackoverflow #code #python #javascript #webdevelopment #javaweb #mobiledevelopment #softwaretesting #techblog #softwarearchitect #datascience #javafx #bigdata #cloudcomputing #devops #it #javascriptdeveloper #javaengineer #machinelearning #csharp #html #css #sql #json #codequality #openSource #computing #softwareengineeringlife #technology #api #devlife #softwareengineeringstudent #techstartup #programmingtips #educational #javacodingchallenge #programming101 #dataengineering #javaapplications #technews #techupdates #coderlife #algorithms101 #softwareengineeringjourney #dailytips #javafun #funwithcode #codewars #developercommunity #hacker #coderchallenge --- Feel free to adjust any part of it or add more specifics!
Download
0 formatsNo download links available.