Skip to content

Commit 3700f1c

Browse files
committed
Add solution #1441
1 parent 336c346 commit 3700f1c

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,304 LeetCode solutions in JavaScript
1+
# 1,305 LeetCode solutions in JavaScript
22

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

@@ -1099,6 +1099,7 @@
10991099
1437|[Check If All 1's Are at Least Length K Places Away](./solutions/1437-check-if-all-1s-are-at-least-length-k-places-away.js)|Easy|
11001100
1438|[Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](./solutions/1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.js)|Medium|
11011101
1439|[Find the Kth Smallest Sum of a Matrix With Sorted Rows](./solutions/1439-find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows.js)|Hard|
1102+
1441|[Build an Array With Stack Operations](./solutions/1441-build-an-array-with-stack-operations.js)|Medium|
11021103
1443|[Minimum Time to Collect All Apples in a Tree](./solutions/1443-minimum-time-to-collect-all-apples-in-a-tree.js)|Medium|
11031104
1446|[Consecutive Characters](./solutions/1446-consecutive-characters.js)|Easy|
11041105
1447|[Simplified Fractions](./solutions/1447-simplified-fractions.js)|Medium|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1441. Build an Array With Stack Operations
3+
* https://leetcode.com/problems/build-an-array-with-stack-operations/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array target and an integer n.
7+
*
8+
* You have an empty stack with the two following operations:
9+
* - "Push": pushes an integer to the top of the stack.
10+
* - "Pop": removes the integer on the top of the stack.
11+
*
12+
* You also have a stream of the integers in the range [1, n].
13+
*
14+
* Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal
15+
* to target. You should follow the following rules:
16+
* - If the stream of the integers is not empty, pick the next integer from the stream and push it
17+
* to the top of the stack.
18+
* - If the stack is not empty, pop the integer at the top of the stack.
19+
* - If, at any moment, the elements in the stack (from the bottom to the top) are equal to target,
20+
* do not read new integers from the stream and do not do more operations on the stack.
21+
*
22+
* Return the stack operations needed to build target following the mentioned rules. If there are
23+
* multiple valid answers, return any of them.
24+
*/
25+
26+
/**
27+
* @param {number[]} target
28+
* @param {number} n
29+
* @return {string[]}
30+
*/
31+
var buildArray = function(target, n) {
32+
const result = [];
33+
let current = 1;
34+
35+
for (const num of target) {
36+
while (current < num) {
37+
result.push('Push');
38+
result.push('Pop');
39+
current++;
40+
}
41+
result.push('Push');
42+
current++;
43+
}
44+
45+
return result;
46+
};

0 commit comments

Comments
 (0)