You are viewing a single comment's thread. Return to all comments →
Here's my solution in TypeScript:
(Notice that the arguments for the queen's position are wrong in the original function.)
type Obstacles = { l: number; r: number; t: number; b: number; tr: number; tl: number; br: number; bl: number; }; function queensAttack(n: number, _k: number, c_q: number, r_q: number, obstacles: number[][]): number { const bounds = { left: r_q - 1, top: n - c_q, right: n - r_q, bottom: c_q - 1, } const limits = obstacles.reduce<Obstacles>((dict, [coordY, coordX]) => { const dX = coordX - r_q; const dY = coordY - c_q; if(dY === 0) { if(dX > 0) { dict.r = Math.min(dX - 1, dict.r); } else { dict.l = Math.min(-dX - 1, dict.l); } } else if (dX === 0) { if(dY > 0) { dict.t = Math.min(dY - 1, dict.t); } else { dict.b = Math.min(-dY - 1, dict.b); } } else if (Math.abs(dX) === Math.abs(dY)) { if(dY > 0) { if(dX > 0) { dict.tr = Math.min(dX - 1, dict.tr); } else { dict.tl = Math.min(-dX - 1, dict.tl); } } else { if(dX > 0){ dict.br = Math.min(dX - 1, dict.br); } else { dict.bl = Math.min(-dX - 1, dict.bl); } } } return dict; }, { t: bounds.top, b: bounds.bottom, l: bounds.left, r: bounds.right, tr: Math.min(bounds.top, bounds.right), tl: Math.min(bounds.top, bounds.left), bl: Math.min(bounds.bottom, bounds.left), br: Math.min(bounds.bottom, bounds.right), }); return Object.values(limits).reduce((acc, curr) => acc + curr, 0); }
Seems like cookies are disabled on this browser, please enable them to open this website
Queen's Attack II
You are viewing a single comment's thread. Return to all comments →
Here's my solution in TypeScript:
(Notice that the arguments for the queen's position are wrong in the original function.)