문제


풀이
은찬
const kClosest = (points, k) => { const array = []; const answer = []; for(let i = 0; i < points.length; i++){ array.push([Math.sqrt(points[i][0] ** 2 + points[i][1] ** 2), i]); } array.sort((a, b) => a[0] - b[0]); for(let i = 0; i < k; i++){ answer.push(points[array[i][1]]); } return answer; };
효성
var kClosest = function(points, k) { const pointsDistance = [], answer=[]; points.forEach(pos => { const distance = calcEuclideanDistance(pos); pointsDistance.push([pos, distance]); }); pointsDistance.sort((a,b) => a[1]-b[1]); for(let i=0; i<k; i++) { answer.push(pointsDistance[i][0]); } return answer; }; function calcEuclideanDistance(arr) { const [x, y] = arr; return (x*x + y*y); }
한 줄 풀이도 있네요..
var kClosest = function(points, k) { return points.sort((p1, p2) => (p1[0]*p1[0]+p1[1]*p1[1]) - (p2[0]*p2[0]+p2[1]*p2[1])) .slice(0, k); };