Skip to content

Commit da2e739

Browse files
add 946
1 parent 23734a9 commit da2e739

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ _If you like this project, please leave me a star._ ★
143143
|953|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_953.java)| |Easy|
144144
|951|[Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_951.java) | |Medium| Tree, DFS, recursion|
145145
|950|[Reveal Cards In Increasing Order](https://leetcode.com/problems/reveal-cards-in-increasing-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_950.java) | |Medium|
146+
|946|[Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_946.java) | |Medium|Stack
146147
|944|[Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_944.java) | |Easy|
147148
|942|[DI String Match](https://leetcode.com/problems/di-string-match/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_942.java) | |Easy|
148149
|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | |Easy|
@@ -165,6 +166,7 @@ _If you like this project, please leave me a star._ ★
165166
|888|[Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_888.java) | |Easy|
166167
|885|[Spiral Matrix III](https://leetcode.com/problems/spiral-matrix-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_885.java) |[:tv:](https://www.youtube.com/watch?v=0qep3f9cqVs) |Medium|
167168
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | |Easy|
169+
|883|[Projection Area of 3D Shapes](https://leetcode.com/problems/projection-area-of-3d-shapes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_883.java) | |Easy|Math
168170
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_876.java) | |Easy|
169171
|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_872.java) | |Easy| DFS, recursion
170172
|868|[Binary Gap](https://leetcode.com/problems/binary-gap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_868.java) | |Easy|
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 946. Validate Stack Sequences
7+
*
8+
* Given two sequences pushed and popped with distinct values,
9+
* return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.
10+
*
11+
* Example 1:
12+
* Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
13+
* Output: true
14+
* Explanation: We might do the following sequence:
15+
* push(1), push(2), push(3), push(4), pop() -> 4,
16+
* push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
17+
*
18+
* Example 2:
19+
* Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
20+
* Output: false
21+
* Explanation: 1 cannot be popped before 2.
22+
*
23+
* Note:
24+
* 0 <= pushed.length == popped.length <= 1000
25+
* 0 <= pushed[i], popped[i] < 1000
26+
* pushed is a permutation of popped.
27+
* pushed and popped have distinct values.
28+
* */
29+
public class _946 {
30+
public static class Solution1 {
31+
public boolean validateStackSequences(int[] pushed, int[] popped) {
32+
if (pushed == null || popped == null || pushed.length == 0 || popped.length == 0) {
33+
return true;
34+
}
35+
Stack<Integer> stack = new Stack<>();
36+
stack.push(pushed[0]);
37+
int i = 1;
38+
int j = 0;
39+
while (!stack.isEmpty() || j < popped.length) {
40+
if (j < popped.length && !stack.isEmpty() && stack.peek() == popped[j]) {
41+
stack.pop();
42+
j++;
43+
} else {
44+
if (i < pushed.length) {
45+
stack.push(pushed[i++]);
46+
} else {
47+
return stack.isEmpty();
48+
}
49+
}
50+
}
51+
return stack.isEmpty();
52+
}
53+
}
54+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._946;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _946Test {
10+
11+
private static _946.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _946.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(true, solution1.validateStackSequences(new int[]{1, 2, 3, 4, 5}, new int[]{4, 5, 3, 2, 1}));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(false, solution1.validateStackSequences(new int[]{1, 2, 3, 4, 5}, new int[]{4, 3, 5, 1, 2}));
26+
}
27+
28+
@Test
29+
public void test3() {
30+
assertEquals(true, solution1.validateStackSequences(new int[]{}, new int[]{}));
31+
}
32+
33+
@Test
34+
public void test4() {
35+
assertEquals(false, solution1.validateStackSequences(new int[]{4, 0, 1, 2, 3}, new int[]{4, 2, 3, 0, 1}));
36+
}
37+
}

0 commit comments

Comments
 (0)