-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathvalidate-stack-sequences.md
36 lines (28 loc) · 1.4 KB
/
validate-stack-sequences.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<p>Given two sequences <code>pushed</code> and <code>popped</code> <strong>with distinct values</strong>, return <code>true</code> if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.</p>
<p> </p>
<div>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>pushed = <span id="example-input-1-1">[1,2,3,4,5]</span>, popped = <span id="example-input-1-2">[4,5,3,2,1]</span>
<strong>Output: </strong><span id="example-output-1">true</span>
<strong>Explanation: </strong>We might do the following sequence:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong>pushed = <span id="example-input-2-1">[1,2,3,4,5]</span>, popped = <span id="example-input-2-2">[4,3,5,1,2]</span>
<strong>Output: </strong><span id="example-output-2">false</span>
<strong>Explanation: </strong>1 cannot be popped before 2.
</pre>
<p> </p>
<p><strong>Note:</strong></p>
<ol>
<li><code>0 <= pushed.length == popped.length <= 1000</code></li>
<li><code>0 <= pushed[i], popped[i] < 1000</code></li>
<li><code>pushed</code> is a permutation of <code>popped</code>.</li>
<li><code>pushed</code> and <code>popped</code> have distinct values.</li>
</ol>
</div>
</div>