File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ function solution ( ingredient ) {
2+ let stack = [ ] ;
3+ let count = 0 ;
4+ let i = 0 ;
5+
6+ while ( i < ingredient . length ) {
7+ stack . push ( ingredient [ i ] ) ;
8+
9+ if ( stack . length >= 4 ) {
10+ const len = stack . length ;
11+ if ( stack [ len - 4 ] === 1 &&
12+ stack [ len - 3 ] === 2 &&
13+ stack [ len - 2 ] === 3 &&
14+ stack [ len - 1 ] === 1 ) {
15+ stack . splice ( - 4 ) ;
16+ count ++ ;
17+ }
18+ }
19+ i ++ ;
20+ }
21+
22+ return count ;
23+ }
24+
25+ // function solution(ingredient) {
26+ // let queue = [...ingredient];
27+ // let stack = [];
28+ // let count = 0;
29+
30+ // while(queue.length > 0) {
31+ // stack.push(queue.shift());
32+
33+ // if(stack.length >= 4) {
34+ // const len = stack.length;
35+ // if(stack[len-4] === 1 &&
36+ // stack[len-3] === 2 &&
37+ // stack[len-2] === 3 &&
38+ // stack[len-1] === 1) {
39+ // stack.splice(-4);
40+ // count++;
41+ // }
42+ // }
43+ // }
44+
45+ // return count;
46+ // }
You can’t perform that action at this time.
0 commit comments