-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__E__alidate-stack-sequences.js
123 lines (105 loc) · 4.03 KB
/
__E__alidate-stack-sequences.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// https://leetcode.com/problems/validate-stack-sequences/
const c = console.log.bind(console);
//by me of course -_-
var validateStackSequences = function(pushed, popped) {
// if (popped.length == 0) {
// return "shit ";
// }
let poppedItem = popped[0];
popped.reverse().pop();
let updatedPopped = popped.reverse();
// return updatedPopped
let pushedPopIndex = pushed.indexOf(poppedItem);
let pushedLeft = pushed.slice(0, pushedPopIndex);
let pushedRight = pushed.slice(pushedPopIndex + 1, pushed.length);
// var pushed = [...pushedLeft, ...pushedRight]
// c("pushedLeft :" + pushedLeft);
// c("pushedRight :" + pushedRight);
// c("pushedPopIndex :" + pushedPopIndex);
// c("updatedPopped :" + updatedPopped);
while (true) {
let poppedItem = updatedPopped[0];
// c("#####################################");
// c("poppedItem :" + poppedItem);
// c("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
// c("pushedLeft :" + pushedLeft);
// c("pushedRight :" + pushedRight);
// c("poppedItem :" + poppedItem);
// c("updatedPopped :" + updatedPopped);
// c("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
// c(pushedLeft[pushedLeft.length - 1])
if (typeof poppedItem == "undefined") {
// c("$$$$$$$$$$$$$$$$$$$")
// c("pushedLeft :" + pushedLeft);
// c("pushedRight :" + pushedRight);
// c("pushedPopIndex :" + pushedPopIndex);
// c("updatedPopped :" + updatedPopped);
// c("$$$$$$$$$$$$$$$$$$$")
return pushedLeft.length == 0 && pushedRight.length == 0 ?
"true" :
"false";
} else if (pushedLeft[pushedLeft.length - 1] == poppedItem) {
pushedLeft.pop();
pushedLeft = pushedLeft;
updatedPopped.reverse().pop();
updatedPopped = updatedPopped.reverse();
// c("pushed left")
} else if (pushedRight[0] == poppedItem) {
// c("pushed RIght");
pushedRight.reverse().pop();
pushedRight = pushedRight.reverse();
// c(pushedRight);
updatedPopped.reverse().pop();
updatedPopped = updatedPopped.reverse();
} else {
if (pushedRight.length > 0) {
pushedLeft.push(pushedRight[0]);
pushedRight.reverse().pop();
pushedRight.reverse();
} else {
return false;
}
// return false
// c("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
// c("pushedLeft :" + pushedLeft);
// c("pushedRight :" + pushedRight);
// c("poppedItem :" + poppedItem);
// c("updatedPopped :" + updatedPopped);
// c("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
// return "shit";
}
// c("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
// c("pushedLeft :" + pushedLeft);
// c("pushedRight :" + pushedRight);
// c("poppedItem :" + poppedItem);
// c("updatedPopped :" + updatedPopped);
// c("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
// c("pushedLeft :" + pushedLeft);
// c("pushedRight :" + pushedRight);
// c("pushedPopIndex :" + pushedPopIndex);
// c("updatedPopped :" + updatedPopped);
}
};
//from community
var validateStackSequences = function(pushed, popped) {
let stack = [];
let i = 0;
for (let item of pushed) {
stack.push(item);
while (stack.length && stack[stack.length - 1] === popped[i]) {
i++;
stack.pop();
}
}
return pushed.length == i;
}
c(validateStackSequences([1, 2, 3, 4, 5], [4, 5, 3, 2, 1])); //t
c(validateStackSequences([1, 2, 3, 4, 5], [4, 3, 5, 1, 2])); //f
c(validateStackSequences([0], [0])); //t
c(validateStackSequences([0, 2, 1], [0, 1, 2])); //t
c(validateStackSequences([4, 0, 3, 1, 2], [4, 1, 3, 0, 2])); //t
// c(
// validateStackSequences(
// [3, 1, 2, 9, 5, 6, 8, 4, 7, 0], [2, 6, 4, 0, 7, 8, 5, 9, 1, 3]
// )
// ); //t