Posts

Showing posts from September, 2024

1381. Design a Stack With Increment Operation

Image
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 the stack, increment all the elements in the stack.   Example 1: Input ["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"] [[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]] Output [null,null,null,2,null,null,null,null,null,103,202,201,-1] Explanation CustomStack stk = new CustomStack(3); ...

432 - All O`one Data Structure

Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts. Implement the  AllOne  class: AllOne()  Initializes the object of the data structure. inc(String key)  Increments the count of the string  key  by  1 . If  key  does not exist in the data structure, insert it with count  1 . dec(String key)  Decrements the count of the string  key  by  1 . If the count of  key  is  0  after the decrement, remove it from the data structure. It is guaranteed that  key  exists in the data structure before the decrement. getMaxKey()  Returns one of the keys with the maximal count. If no element exists, return an empty string  "" . getMinKey()  Returns one of the keys with the minimum count. If no element exists, return an empty string  "" . Note  that each function must run in  O(1)  average time comple...