HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
👻
개발 기록
/
코딩테스트 스터디
코딩테스트 스터디
/
K Closest Points to Origin

K Closest Points to Origin

Link
https://leetcode.com/problems/k-closest-points-to-origin/
Deadline
Nov 27, 2021
Status
Archived
Type
heap

문제

notion image
notion image

풀이

은찬
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); };
 
희진