Advertisement

Design a Stack With Increment Operation - LeetCode 1381 Solution

Design a Stack With Increment Operation - Complete Solution Guide

Design a Stack With Increment Operation is LeetCode problem 1381, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Design a stack that supports increment operations on its elements. Implement the CustomStack class: CustomStack(int maxSize) Initializes the object with maxSize which is the maximum number of elements in the stack. void push(int x) Adds x to the top of the stack if the stack has not reached the maxSize . int pop() Pops and returns the top of the stack or -1 if the stack is empty. void inc(int k, int val) Increments the bottom k elements of the stack by val . If there are less than k elements in

Detailed Explanation

The problem requires designing a custom stack data structure with a specific 'increment' operation in addition to the standard 'push' and 'pop' operations. The stack has a fixed maximum size. The 'push' operation adds an element to the top if the stack is not full. The 'pop' operation removes and returns the top element, returning -1 if the stack is empty. The 'increment' operation increases the bottom 'k' elements of the stack by a given value. If the stack contains less than 'k' elements, then all the elements in the stack are incremented.

Solution Approach

The solution uses two arrays: one to store the stack elements and another to store the increment values for each element. The 'push' operation adds a new element to the stack and initializes its increment value to 0. The 'pop' operation removes the top element, adds its increment value to the element below it (if one exists), and returns the sum of the element's value and its increment. The 'increment' operation adds the given value to the increment values of the bottom 'k' elements. The 'min' function is used to handle the case when 'k' is larger than the stack size.

Step-by-Step Algorithm

  1. Step 1: Initialize the CustomStack with a maximum size and two arrays: 'stack' to store the stack elements and 'inc' to store the increments for each element.
  2. Step 2: Implement the 'push' operation: If the stack is not full, add the element to the 'stack' array and initialize its corresponding 'inc' value to 0.
  3. Step 3: Implement the 'pop' operation: If the stack is empty, return -1. Otherwise, get the increment value from the 'inc' array, get the element from the 'stack' array. If there is an element below the current one (not the base of the stack), add its increment to the increment below it. Return the sum of the stack value and increment value.
  4. Step 4: Implement the 'increment' operation: Determine the minimum of 'k' and the stack size. Then, add the given 'val' to the increment values of the bottom 'k' elements in the 'inc' array.

Key Insights

  • Insight 1: The increment operation can be optimized by using an auxiliary array to store the increment values for each element, avoiding repeated updates to the stack itself.
  • Insight 2: The stack and increment array sizes must be managed according to the maximum size limit to avoid exceeding allocated memory.
  • Insight 3: When popping, the increment value for the popped element needs to be propagated to the element below it in the stack, if such an element exists.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(n)

Topics

This problem involves: Array, Stack, Design.

Companies

Asked at: Cloudflare, IMC, Moloco, eBay.