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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more