{"id":6497,"date":"2020-03-15T01:30:22","date_gmt":"2020-03-15T08:30:22","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6497"},"modified":"2020-03-15T01:37:17","modified_gmt":"2020-03-15T08:37:17","slug":"leetcode-1381-design-a-stack-with-increment-operation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-1381-design-a-stack-with-increment-operation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1381. Design a Stack With Increment Operation"},"content":{"rendered":"\n<p>Design a stack which supports the following operations.<\/p>\n\n\n\n<p>Implement the&nbsp;<code>CustomStack<\/code>&nbsp;class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>CustomStack(int maxSize)<\/code>&nbsp;Initializes the object with&nbsp;<code>maxSize<\/code>&nbsp;which is the maximum number of elements in the stack or do nothing if the stack reached the&nbsp;<code>maxSize<\/code>.<\/li><li><code>void push(int x)<\/code>&nbsp;Adds&nbsp;<code>x<\/code>&nbsp;to the top of the stack if the stack hasn&#8217;t reached the&nbsp;<code>maxSize<\/code>.<\/li><li><code>int pop()<\/code>&nbsp;Pops and returns the top of stack or&nbsp;<strong>-1<\/strong>&nbsp;if the stack is empty.<\/li><li><code>void inc(int k, int val)<\/code>&nbsp;Increments the bottom&nbsp;<code>k<\/code>&nbsp;elements of the stack by&nbsp;<code>val<\/code>. If there are less than&nbsp;<code>k<\/code>&nbsp;elements in the stack, just increment all the elements in the stack.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted wp-block-preformatted;crayon:false\"><strong>Input<\/strong>\n[\"CustomStack\",\"push\",\"push\",\"pop\",\"push\",\"push\",\"push\",\"increment\",\"increment\",\"pop\",\"pop\",\"pop\",\"pop\"]\n[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]\n<strong>Output<\/strong>\n[null,null,null,2,null,null,null,null,null,103,202,201,-1]\n<strong>Explanation<\/strong>\nCustomStack customStack = new CustomStack(3); \/\/ Stack is Empty []\ncustomStack.push(1);                          \/\/ stack becomes [1]\ncustomStack.push(2);                          \/\/ stack becomes [1, 2]\ncustomStack.pop();                            \/\/ return 2 --&gt; Return top of the stack 2, stack becomes [1]\ncustomStack.push(2);                          \/\/ stack becomes [1, 2]\ncustomStack.push(3);                          \/\/ stack becomes [1, 2, 3]\ncustomStack.push(4);                          \/\/ stack still [1, 2, 3], Don't add another elements as size is 4\ncustomStack.increment(5, 100);                \/\/ stack becomes [101, 102, 103]\ncustomStack.increment(2, 100);                \/\/ stack becomes [201, 202, 103]\ncustomStack.pop();                            \/\/ return 103 --&gt; Return top of the stack 103, stack becomes [201, 202]\ncustomStack.pop();                            \/\/ return 202 --&gt; Return top of the stack 102, stack becomes [201]\ncustomStack.pop();                            \/\/ return 201 --&gt; Return top of the stack 101, stack becomes []\ncustomStack.pop();                            \/\/ return -1 --&gt; Stack is empty return -1.\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: <br>init: O(1)<br>pop: O(1)<br>push: O(1)<br>inc: O(k)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass CustomStack {\npublic:\n  CustomStack(int maxSize): max_size_(maxSize) {}\n\n  void push(int x) {\n    if (data_.size() == max_size_) return;\n    data_.push_back(x);\n  }\n\n  int pop() {\n    if (data_.empty()) return -1;\n    int val = data_.back();\n    data_.pop_back();\n    return val;\n  }\n\n  void increment(int k, int val) {\n    for (int i = 0; i < min(static_cast<size_t>(k), \n                            data_.size()); ++i)\n      data_[i] += val;\n  }\nprivate:\n  int max_size_;\n  vector<int> data_;\n};\n\n\/**\n * Your CustomStack object will be instantiated and called as such:\n * CustomStack* obj = new CustomStack(maxSize);\n * obj->push(x);\n * int param_2 = obj->pop();\n * obj->increment(k,val);\n *\/\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Design a stack which supports the following operations. Implement the&nbsp;CustomStack&nbsp;class: CustomStack(int maxSize)&nbsp;Initializes the object with&nbsp;maxSize&nbsp;which is the maximum number of elements in the stack or&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[291,330,177,180],"class_list":["post-6497","post","type-post","status-publish","format-standard","hentry","category-stack","tag-data-structure","tag-desgin","tag-medium","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6497"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6497\/revisions"}],"predecessor-version":[{"id":6499,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6497\/revisions\/6499"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}