JSFiddle - React, Tailwind, and code Playground
by Vasilii Chugunov
JavaScript
function drawLine (wall) {
let options = {};
for(let i = 0; i < wall.length; i++) {
let counter = 0;
for (let j = 0; j < wall[i].length - 1; j++) {
let brick = wall[i][j];
counter += brick;
if (options[counter]) {
options[counter]++
} else {
options[counter] = 1;
}
}
}
console.log(options)
let max = 0;
Object.keys(options).forEach((key) => {
if (options[key] > max) {
max = options[key];
}
});
console.log(max)
let crossed = 0;
for(let i = 0; i < wall.length; i++) {
let counter = 0;
wall[i].forEach((brick) => {
if (counter < max && counter + brick > max) {
crossed++;
}
counter += brick;
})
}
return crossed;
}
console.log(drawLine([[1,2,2,1],
[6],
[1,3,2],
[2,4],
[3,1,2],
[1,3,1,1]]))