Skip to content

Commit ec269a0

Browse files
committed
Add solution #1381
1 parent 045aa2b commit ec269a0

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,265 LeetCode solutions in JavaScript
1+
# 1,266 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1052,6 +1052,7 @@
10521052
1377|[Frog Position After T Seconds](./solutions/1377-frog-position-after-t-seconds.js)|Hard|
10531053
1379|[Find a Corresponding Node of a Binary Tree in a Clone of That Tree](./solutions/1379-find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree.js)|Easy|
10541054
1380|[Lucky Numbers in a Matrix](./solutions/1380-lucky-numbers-in-a-matrix.js)|Easy|
1055+
1381|[Design a Stack With Increment Operation](./solutions/1381-design-a-stack-with-increment-operation.js)|Medium|
10551056
1389|[Create Target Array in the Given Order](./solutions/1389-create-target-array-in-the-given-order.js)|Easy|
10561057
1400|[Construct K Palindrome Strings](./solutions/1400-construct-k-palindrome-strings.js)|Medium|
10571058
1402|[Reducing Dishes](./solutions/1402-reducing-dishes.js)|Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* 1381. Design a Stack With Increment Operation
3+
* https://leetcode.com/problems/design-a-stack-with-increment-operation/
4+
* Difficulty: Medium
5+
*
6+
* Design a stack that supports increment operations on its elements.
7+
*
8+
* Implement the CustomStack class:
9+
* - CustomStack(int maxSize) Initializes the object with maxSize which is the maximum number
10+
* of elements in the stack.
11+
* - void push(int x) Adds x to the top of the stack if the stack has not reached the maxSize.
12+
* - int pop() Pops and returns the top of the stack or -1 if the stack is empty.
13+
* - void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are
14+
* less than k elements in the stack, increment all the elements in the stack.
15+
*/
16+
17+
/**
18+
* @param {number} maxSize
19+
*/
20+
var CustomStack = function(maxSize) {
21+
this.elements = [];
22+
this.capacity = maxSize;
23+
};
24+
25+
/**
26+
* @param {number} x
27+
* @return {void}
28+
*/
29+
CustomStack.prototype.push = function(x) {
30+
if (this.elements.length < this.capacity) {
31+
this.elements.push(x);
32+
}
33+
};
34+
35+
/**
36+
* @return {number}
37+
*/
38+
CustomStack.prototype.pop = function() {
39+
return this.elements.length ? this.elements.pop() : -1;
40+
};
41+
42+
/**
43+
* @param {number} k
44+
* @param {number} val
45+
* @return {void}
46+
*/
47+
CustomStack.prototype.increment = function(k, val) {
48+
const limit = Math.min(k, this.elements.length);
49+
for (let i = 0; i < limit; i++) {
50+
this.elements[i] += val;
51+
}
52+
};

0 commit comments

Comments
 (0)