Published
Edited
Jan 12, 2022
Insert cell
# Blocks to fix
Insert cell
solution("xxx..", "..xxx")
Insert cell
solution("..xxxx.", "x...xx.")
Insert cell
solution("..xxxxx.", "x.x..xx.")
Insert cell
function solution(L1, L2) {
const lane1 = L1.split("");
const lane2 = L2.split("");

const accumulator = (acc, el) => (el === "x" ? ++acc : acc);
const topX = lane1.reduce(accumulator, 0);
const bottomX = lane2.reduce(accumulator, 0);

const lane1Memoi = getMemoi(lane1);
const lane2Memoi = getMemoi(lane2);

let res = Math.max(topX, bottomX);
for (let i = 1; i < lane1.length; i++) {
const topToBottom =
lane1Memoi[i - 1] + (lane2Memoi[lane2Memoi.length - 1] - lane2Memoi[i]);

const bottomToTop =
lane2Memoi[i - 1] + (lane1Memoi[lane1Memoi.length - 1] - lane1Memoi[i]);

// const topToBottom =
// lane1.slice(0, i).reduce(accumulator, 0) +
// lane2.slice(i + 1).reduce(accumulator, 0);
// const bottomToTop =
// lane2.slice(0, i).reduce(accumulator, 0) +
// lane1.slice(i + 1).reduce(accumulator, 0);
res = Math.max(res, topToBottom, bottomToTop);
}

return res;
}
Insert cell
function getMemoi(lane, reverse = false) {
let count = 0;
const memoization = [];

if (reverse) lane.reverse();
for (const el of lane) {
if (el === "x") count++;
memoization.push(count);
}

return memoization;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more