Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 783c526

Browse files
add solution in JS for 1381_design_a_stack_with_increment_operation.js
1 parent ffd627c commit 783c526

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @description Initializes stack as an array limited by max size
3+
* @param {Number} maxSize
4+
*/
5+
const CustomStack = function (maxSize) {
6+
this.stack = [];
7+
this.maxSize = maxSize;
8+
};
9+
10+
/**
11+
* @description Adds value to the top of the stack
12+
* @param {Number} value
13+
* @return {Void}
14+
*/
15+
CustomStack.prototype.push = function (value) {
16+
if (this.stack.length < this.maxSize) {
17+
this.stack.push(value);
18+
}
19+
};
20+
21+
/**
22+
* @description Returns the top of the stack and removes it
23+
* @return {Number}
24+
*/
25+
CustomStack.prototype.pop = function () {
26+
// Забираем элемент из стека
27+
if (this.stack.length === 0) {
28+
return -1;
29+
}
30+
const takenElement = this.stack.pop();
31+
return takenElement;
32+
};
33+
34+
/**
35+
* @description Increments the bottom number of elements by value
36+
* @param {Number} number
37+
* @param {Number} value
38+
* @return {Void}
39+
*/
40+
CustomStack.prototype.increment = function (number, value) {
41+
// Сначала сравниваем размер стека с k
42+
if (this.stack.length < number) {
43+
this.stack = this.stack.map((elem) => elem + value);
44+
} else {
45+
for (let i = 0; i < number; i += 1) {
46+
const elem = this.stack[i];
47+
this.stack[i] = elem + value;
48+
}
49+
}
50+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /