목록Heap (2)
N
https://leetcode.com/problems/k-closest-points-to-origin/ K Closest Points to Origin - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com array 배열과 answer 배열을 선언. points 배열을 순회하면서 array에 유클리드 거리와 인덱스를 넣어준다. 유클리드 거리와 인덱스가 저장된 array 배열을 거리의 오름차순으로 정렬. k개 만큼 answer에 답을 넣어주면 된다. /** * @pa..

처음 문제를 풀었을 때는 벡터를 이용해서 문제를 풀었다..! 하지만 효율성 테스트에서 시간 초과로 통과하지 못했다.. 그래서 priority_queue를 이용하여 문제를 풀었다. priority_queue는 우선순위 큐로 greater, less 함수를 이용하여 오름차순, 내림차순으로 정렬을 할 수 있다..! 시간 복잡도는 O(logn)이기 때문에 벡터를 이용해 sort 하는 것보다 빠르다..! 코드는 어렵지 않으니 천천히 보면 따라갈 수 있다..! #include #include #include #include using namespace std; int solution(vector scoville, int K) { int answer = 0; priority_queue pq; for(int i = 0..