-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbrickWall.js
More file actions
38 lines (31 loc) · 726 Bytes
/
brickWall.js
File metadata and controls
38 lines (31 loc) · 726 Bytes
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
/**
* @param {number[][]} wall
* @return {number}
*/
var leastBricks = function(wall) {
let counts = {};
let width = parseInt(wall[0].reduce((item, accumulator) => {
accumulator += item;
return accumulator;
}, 0));
let height = parseInt(wall.length);
for (let layer of wall) {
let width = 0;
for (let brick of layer) {
width += brick;
if (width in counts) {
counts[width]++;
} else {
counts[width] = 1;
}
}
}
let result = Infinity;
console.log(counts);
for (let count in counts) {
if (parseInt(count) !== width) {
result = Math.min(result, height - counts[count]);
}
}
return (result !== Infinity) ? result : height;
};