-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path104hamburger_stack.js
More file actions
59 lines (51 loc) · 1.53 KB
/
104hamburger_stack.js
File metadata and controls
59 lines (51 loc) · 1.53 KB
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
// function solution(ingredient) {
// let answer = 0;
// let isBun = false;
// let isVege = false;
// let isMeat = false;
// for (let i = 0; i < ingredient.length; i++) {
// if (isBun && isVege && ingredient[i] !== 3) {
// isBun = false;
// isVege = false;
// }
// if (ingredient[i] === 1) {
// isBun = true;
// } else if (isBun && ingredient[i] === 2) {
// isVege = true;
// } else if (isBun && isVege && ingredient[i] === 3) {
// isMeat = true;
// } else {
// isBun = false;
// isVege = false;
// isMeat = false;
// }
// if (isBun && isVege && isMeat) {
// answer += 1;
// isBun = false;
// isVege = false;
// isMeat = false;
// }
// }
// return answer;
// }
// 2. hamburger solution with simple array splice
function solution(ingredient) {
let answer = 0;
let count = 0;
while (count < ingredient.length) {
if (count < 0) count = 0;
if (ingredient[count] === 1) {
if (ingredient[count + 1] === 2) {
if (ingredient[count + 2] === 3) {
if (ingredient[count + 3] === 1) {
answer += 1;
ingredient.splice(count, 4);
count -= 3;
}
}
}
}
count += 1;
}
return answer;
}