| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.DesignStackWithIncrementOperation; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class DesignStackWithIncrementOperationArray : IDesignStackWithIncrementOperation |
| | 16 | | { |
| | 17 | | private readonly int[] _stackArray; |
| 1 | 18 | | private int _topIndex = -1; |
| | 19 | |
|
| 1 | 20 | | public DesignStackWithIncrementOperationArray(int maxSize) |
| 1 | 21 | | { |
| 1 | 22 | | _stackArray = new int[maxSize]; |
| 1 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Time complexity - O(1) |
| | 27 | | /// Space complexity - O(1) |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="x"></param> |
| | 30 | | public void Push(int x) |
| 5 | 31 | | { |
| 5 | 32 | | if (_topIndex >= _stackArray.Length - 1) |
| 1 | 33 | | { |
| 1 | 34 | | return; |
| | 35 | | } |
| | 36 | |
|
| 4 | 37 | | _topIndex++; |
| | 38 | |
|
| 4 | 39 | | _stackArray[_topIndex] = x; |
| 5 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Time complexity - O(1) |
| | 44 | | /// Space complexity - O(1) |
| | 45 | | /// </summary> |
| | 46 | | /// <returns></returns> |
| | 47 | | public int Pop() |
| 5 | 48 | | { |
| 5 | 49 | | if (_topIndex < 0) |
| 1 | 50 | | { |
| 1 | 51 | | return -1; |
| | 52 | | } |
| | 53 | |
|
| 4 | 54 | | return _stackArray[_topIndex--]; |
| 5 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Time complexity - O(min(k,n)) |
| | 59 | | /// Space complexity - O(1) |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="k"></param> |
| | 62 | | /// <param name="val"></param> |
| | 63 | | public void Increment(int k, int val) |
| 2 | 64 | | { |
| 14 | 65 | | for (var i = 0; i < Math.Min(k, _topIndex + 1); i++) |
| 5 | 66 | | { |
| 5 | 67 | | _stackArray[i] += val; |
| 5 | 68 | | } |
| 2 | 69 | | } |
| | 70 | | } |