forked from geekhub-js-2014/life-homework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcups.js
57 lines (47 loc) · 1.63 KB
/
cups.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
Array.prototype.max = function () {
return Math.max.apply(Math, this);
};
var cups = function (wall) {
var i,
smallSquare,
max = wall.indexOf(wall.max()),
getCups = function (smallWall) {
var out = [{
start: 0,
end: 0,
square: 0
}];
for (i = 0; i < smallWall.length; i++) {
if (out[out.length - 1].square > 0 && smallWall[i] > smallWall[out[out.length - 1].start]) {
out[out.length - 1].end = i;
if (out[out.length - 1].end < out[out.length - 1].start) {
out[out.length - 1].square -= (out[out.length - 1].count * (out[out.length - 1].start - out[out.length - 1].end));
}
out.push({
start: i,
end: i,
square: 0
});
}
if (smallWall[i] > smallWall[out[out.length - 1].start]) {
out[out.length - 1].start = i;
} else {
smallSquare = smallWall[out[out.length - 1].start] - smallWall[i];
out[out.length - 1].square += smallSquare;
}
}
return out;
};
smallSquare = 0;
getCups(wall.slice(0, max)).concat(getCups(wall.slice(max + 1).reverse())).filter(function (val) {
smallSquare += val.square;
return val.square;
});
return smallSquare;
};
var arr = [];
var tt = function () {
var i = [5, 4];
arr.push(i.pop());
console.log(arr, i);
};