-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson7.js
91 lines (74 loc) · 2 KB
/
lesson7.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
//check if string is perfect with ([{
function solution(S) {
var map = {')': '(', ']': '[', '}': '{', '(': '', '[': '', '{': ''};
var string = S.split('');
var temp = [string.shift()];
if (S === '') return 1;
string.map(function(value) {
if (map[value] === temp[temp.length - 1]) {
temp.pop();
} else {
temp.push(value);
}
});
if (temp.length === 0) return 1;
return 0;
}
//fishs going down or up the stream eating itself
function solution(A, B) {
var map = A.map(function(value, index) {
return { wei: value, dir: B[index] }
});
var temp = [map.pop()];
while (map.length > 0) {
if (map[map.length-1].dir === temp[0].dir) {
temp.unshift(map.pop());
} else {
if (map[map.length-1].dir === 1) {
if (map[map.length-1].wei < temp[0].wei) {
map.pop();
} else {
temp.shift();
if (temp.length === 0) temp.unshift(map.pop());
}
} else {
temp.unshift(map.pop());
}
}
}
return temp.length;
}
//(()) strings
function solution(S) {
var string = S.split('');
var temp = [string.shift()];
if (S === '') return 1;
string.map(function(value) {
if (value === ')' && temp[temp.length - 1] === '(') {
temp.pop();
} else {
temp.push(value);
}
});
if (temp.length === 0) return 1;
return 0;
}
//mount blocks on top of each other, minimal blocks
function solution(H) {
var blocks = 0;
var blocked = new Array(H.length).fill(0);
for (i = 0; i < H.length; i++) {
if (blocked[i] !== H[i]) {
blocks++;
}
blocked[i] = H[i];
for (x = i; x <= H.length; x++) {
if (H[x] >= H[i]) {
blocked[x] = H[i]
} else {
break;
}
}
}
return blocks;
}