Skip to content

Commit ed1758a

Browse files
committed
Create README - LeetHub
1 parent 729bc50 commit ed1758a

File tree

1 file changed

+66
-0
lines changed
  • 1552-build-an-array-with-stack-operations

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<h2><a href="https://leetcode.com/problems/build-an-array-with-stack-operations">1552. Build an Array With Stack Operations</a></h2><h3>Medium</h3><hr><p>You are given an integer array <code>target</code> and an integer <code>n</code>.</p>
2+
3+
<p>You have an empty stack with the two following operations:</p>
4+
5+
<ul>
6+
<li><strong><code>&quot;Push&quot;</code></strong>: pushes an integer to the top of the stack.</li>
7+
<li><strong><code>&quot;Pop&quot;</code></strong>: removes the integer on the top of the stack.</li>
8+
</ul>
9+
10+
<p>You also have a stream of the integers in the range <code>[1, n]</code>.</p>
11+
12+
<p>Use the two stack operations to make the numbers in the stack (from the bottom to the top) equal to <code>target</code>. You should follow the following rules:</p>
13+
14+
<ul>
15+
<li>If the stream of the integers is not empty, pick the next integer from the stream and push it to the top of the stack.</li>
16+
<li>If the stack is not empty, pop the integer at the top of the stack.</li>
17+
<li>If, at any moment, the elements in the stack (from the bottom to the top) are equal to <code>target</code>, do not read new integers from the stream and do not do more operations on the stack.</li>
18+
</ul>
19+
20+
<p>Return <em>the stack operations needed to build </em><code>target</code> following the mentioned rules. If there are multiple valid answers, return <strong>any of them</strong>.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong class="example">Example 1:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> target = [1,3], n = 3
27+
<strong>Output:</strong> [&quot;Push&quot;,&quot;Push&quot;,&quot;Pop&quot;,&quot;Push&quot;]
28+
<strong>Explanation:</strong> Initially the stack s is empty. The last element is the top of the stack.
29+
Read 1 from the stream and push it to the stack. s = [1].
30+
Read 2 from the stream and push it to the stack. s = [1,2].
31+
Pop the integer on the top of the stack. s = [1].
32+
Read 3 from the stream and push it to the stack. s = [1,3].
33+
</pre>
34+
35+
<p><strong class="example">Example 2:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> target = [1,2,3], n = 3
39+
<strong>Output:</strong> [&quot;Push&quot;,&quot;Push&quot;,&quot;Push&quot;]
40+
<strong>Explanation:</strong> Initially the stack s is empty. The last element is the top of the stack.
41+
Read 1 from the stream and push it to the stack. s = [1].
42+
Read 2 from the stream and push it to the stack. s = [1,2].
43+
Read 3 from the stream and push it to the stack. s = [1,2,3].
44+
</pre>
45+
46+
<p><strong class="example">Example 3:</strong></p>
47+
48+
<pre>
49+
<strong>Input:</strong> target = [1,2], n = 4
50+
<strong>Output:</strong> [&quot;Push&quot;,&quot;Push&quot;]
51+
<strong>Explanation:</strong> Initially the stack s is empty. The last element is the top of the stack.
52+
Read 1 from the stream and push it to the stack. s = [1].
53+
Read 2 from the stream and push it to the stack. s = [1,2].
54+
Since the stack (from the bottom to the top) is equal to target, we stop the stack operations.
55+
The answers that read integer 3 from the stream are not accepted.
56+
</pre>
57+
58+
<p>&nbsp;</p>
59+
<p><strong>Constraints:</strong></p>
60+
61+
<ul>
62+
<li><code>1 &lt;= target.length &lt;= 100</code></li>
63+
<li><code>1 &lt;= n &lt;= 100</code></li>
64+
<li><code>1 &lt;= target[i] &lt;= n</code></li>
65+
<li><code>target</code> is strictly increasing.</li>
66+
</ul>

0 commit comments

Comments
 (0)