Skip to content

Commit d58cd4b

Browse files
author
hieu
committed
Solve Problem 946 - Validate Stack Sequence
1 parent 883e964 commit d58cd4b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/contest/ReadMe.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public class ReadMe {
1111
// backtrack: 1307[!] 1239[v] 1219[v] 1291[~] 1286[v]
1212
// dp: 1349[!] 1340[v] 1367[v] 1388[!] 1406[!]
1313
// divide & conquer: 932[!] 514 903[v] 327[x] 315[!]
14-
// greedy: 1354[!] 1353[x] 1338[v] 1383[!] 1403
15-
// stack: 1190[!] 975[x] 1381[v] 901[v] 946
14+
// greedy: 1354[!] 1353[x] 1338[v] 1383[!] 1403[v]
15+
// stack: 1190[!] 975[x] 1381[v] 901[v] 946[v]
1616
// binary search: 1351[v] 1337[v] 1292[!] 1235[!] 1237
1717
// 2 pointers: 1248[*] 1234[v] 986[v] 826[x] 977
1818
// sliding window: 1208[v] 1040[!] 995[!] 567[v] 978[v]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package stack;
2+
3+
public class Problem946_ValidateStackSequences {
4+
5+
public boolean validateStackSequences(int[] pushed, int[] popped) {
6+
int i = -1, j = 0;
7+
for (int x : pushed) {
8+
pushed[++i] = x;
9+
while (i >= 0 && pushed[i] == popped[j]) {
10+
j++;
11+
i--;
12+
}
13+
}
14+
return j == popped.length;
15+
}
16+
}

0 commit comments

Comments
 (0)