【发布时间】:2019-11-07 13:59:13
【问题描述】:
我有一个工作面试测试,问题是在数据集中找到 3 个点的所有共线数据点。
wolframalpha.com/input/?i=collinear+lines
我没有通过测试,但我还是自己完成了,找不到任何我能理解的解决方案。
这是数据集
[[0, 0], [1, 1], [2, 2], [3, 3], [3, 2], [4, 2], [5, 1]]
解决方案应该返回 6 它的作用,但我只是想知道我是否得到了最佳解决方案。
我的解决方案:
/**
* Will return the amount of lines of 3 points or can made with 3 unique points
* @param {number} dotsInLine
*
* @returns {number} of lines of 3 points found
*/
function countLineOf3(dotsInLine) {
if (dotsInLine === 3) return 1;
if (dotsInLine > 3) return (dotsInLine - 3) * dotsInLine;
return 0;
}
/**
* will go trough the array of found points on 1 axes and return the count of 3 point lines found
* @param {array} foundInArr
* @param {object} matrix
*
* @returns {number}
*/
function foundInAddToCount(foundInArr, matrix) {
let count = 0;
for (let i = 0; i < foundInArr.length; i++) {
count += countLineOf3(matrix[foundInArr[i]]);
}
return count;
}
function solution(A) {
let count = 0;
let maxX = 0;
let maxY = 0;
const xStrait = {};
const xStraitFound = [];
const yStrait = {};
const yStraitFound = [];
const lineUp = {};
const lineUpFound = [];
const lineDown = {};
const lineDownFound = [];
for (let i = 0; i < A.length; i++) {
if (maxX < A[i][0]) maxX = A[i][0];
if (maxY < A[i][1]) maxY = A[i][1];
// find strait vertical points
if (!xStrait[A[i][0]]) xStrait[A[i][0]] = 0;
xStrait[A[i][0]]++;
if (xStrait[A[i][0]] === 3) xStraitFound.push(A[i][0]);
// find strait horizontal points
if (!yStrait[A[i][1]]) yStrait[A[i][1]] = 0;
yStrait[A[i][1]]++;
if (yStrait[A[i][1]] === 3) yStraitFound.push(A[i][1]);
// find points bottom left to top right
const x = A[i][0];
const y = A[i][1];
const xMinY = x - y;
const yMinx = y - x;
//middle line
let key;
if (xMinY >= 0 && yMinx >= 0) {
key = `${xMinY}${yMinx}`;
} else if (xMinY >= 0 && yMinx < 0) {
// y zero UP
key = `${xMinY}0`;
} else if (xMinY < 0 && yMinx >= 0) {
// x zero UP
key = `0${yMinx}`;
}
if (key) {
if (!lineUp[key]) lineUp[key] = 0;
lineUp[key]++;
if (lineUp[key] === 3) lineUpFound.push(key);
}
const xPlusY = x + y;
//find points top left to bottom right
if (!lineDown[`0${xPlusY}`]) lineDown[`0${xPlusY}`] = 0;
lineDown[`0${xPlusY}`]++;
if (lineDown[`0${xPlusY}`] === 3) {
lineDownFound.push(`0${xPlusY}`);
}
}
count += foundInAddToCount(xStraitFound, xStrait);
count += foundInAddToCount(yStraitFound, yStrait);
count += foundInAddToCount(lineUpFound, lineUp);
count += foundInAddToCount(lineDownFound, lineDown);
return count;
}
console.log(
solution([[0, 0], [1, 1], [2, 2], [3, 3], [3, 2], [4, 2], [5, 1]])
);
我基本上对所有水平线、垂直线和交叉线进行排序(抱歉我找不到正确的英文单词)并数数并将它们相加。
所以如果有人有任何建议,请告诉我。
【问题讨论】:
-
请定义问题。 为什么解决方案是
6?你说你想找到所有共线的数据点,但你的答案是一个标量。 -
另外你的代码似乎并不认为 [[0,0],[2,1],[4,2]] 是共线的
-
如果你看这张图i.imgur.com/TxvLlux.gif然后在3个点之间画一条直线然后你可以画6条线[2,1]不在数据集中
-
wolframalpha.com/input/?i=collinear+lines FYI @Prune
-
@MattTimmermans stackoverflow.com/questions/58750359/… 抱歉无法编辑评论
标签: javascript node.js algorithm