| | 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 DesignStackWithIncrementOperationLinkedList : IDesignStackWithIncrementOperation |
| | 16 | | { |
| | 17 | | private readonly int _maxSize; |
| | 18 | | private int _count; |
| | 19 | | private Node? _headNode; |
| | 20 | | private Node? _tailNode; |
| | 21 | |
|
| 1 | 22 | | public DesignStackWithIncrementOperationLinkedList(int maxSize) |
| 1 | 23 | | { |
| 1 | 24 | | _maxSize = maxSize; |
| 1 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Time complexity - O(1) |
| | 29 | | /// Space complexity - O(1) |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="x"></param> |
| | 32 | | public void Push(int x) |
| 5 | 33 | | { |
| 5 | 34 | | if (_count >= _maxSize) |
| 1 | 35 | | { |
| 1 | 36 | | return; |
| | 37 | | } |
| | 38 | |
|
| 4 | 39 | | var newNode = new Node(x, _headNode); |
| | 40 | |
|
| 4 | 41 | | if (_headNode == null) |
| 1 | 42 | | { |
| 1 | 43 | | _tailNode = newNode; |
| 1 | 44 | | } |
| | 45 | | else |
| 3 | 46 | | { |
| 3 | 47 | | _headNode.Previous = newNode; |
| 3 | 48 | | } |
| | 49 | |
|
| 4 | 50 | | _headNode = newNode; |
| | 51 | |
|
| 4 | 52 | | _count++; |
| 5 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Time complexity - O(1) |
| | 57 | | /// Space complexity - O(1) |
| | 58 | | /// </summary> |
| | 59 | | /// <returns></returns> |
| | 60 | | public int Pop() |
| 5 | 61 | | { |
| 5 | 62 | | if (_headNode == null) |
| 1 | 63 | | { |
| 1 | 64 | | return -1; |
| | 65 | | } |
| | 66 | |
|
| 4 | 67 | | var value = _headNode.Value; |
| | 68 | |
|
| 4 | 69 | | _headNode = _headNode.Next; |
| | 70 | |
|
| 4 | 71 | | if (_headNode != null) |
| 3 | 72 | | { |
| 3 | 73 | | _headNode.Previous = null; |
| 3 | 74 | | } |
| | 75 | | else |
| 1 | 76 | | { |
| 1 | 77 | | _tailNode = null; |
| 1 | 78 | | } |
| | 79 | |
|
| 4 | 80 | | _count--; |
| | 81 | |
|
| 4 | 82 | | return value; |
| 5 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Time complexity - O(min(k,n)) |
| | 87 | | /// Space complexity - O(1) |
| | 88 | | /// </summary> |
| | 89 | | /// <param name="k"></param> |
| | 90 | | /// <param name="val"></param> |
| | 91 | | public void Increment(int k, int val) |
| 2 | 92 | | { |
| 2 | 93 | | var i = 0; |
| 2 | 94 | | var currentNode = _tailNode; |
| | 95 | |
|
| 7 | 96 | | while (i < k && currentNode != null) |
| 5 | 97 | | { |
| 5 | 98 | | currentNode.Value += val; |
| | 99 | |
|
| 5 | 100 | | currentNode = currentNode.Previous; |
| | 101 | |
|
| 5 | 102 | | i++; |
| 5 | 103 | | } |
| 2 | 104 | | } |
| | 105 | |
|
| 4 | 106 | | private class Node(int value, Node? next = null, Node? previous = null) |
| | 107 | | { |
| 18 | 108 | | public int Value { get; set; } = value; |
| 8 | 109 | | public Node? Next { get; } = next; |
| 15 | 110 | | public Node? Previous { get; set; } = previous; |
| | 111 | | } |
| | 112 | | } |