목록우선순위 큐 (2)
N
https://leetcode.com/problems/furthest-building-you-can-reach/ Furthest Building You Can Reach - 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 최소힙을 이용한 문제풀이. 벽돌과 사다리를 이용해 가장 멀리 갈 수 있는 빌딩의 인덱스를 리턴하면 된다. pq라는 이름으로 우선순위 큐(최소 힙)을 선언한다. for문으로 1부터 heights의 길이만큼 순회. 현재 빌딩의 높이 - 이전 빌딩의 ..

처음 문제를 풀었을 때는 벡터를 이용해서 문제를 풀었다..! 하지만 효율성 테스트에서 시간 초과로 통과하지 못했다.. 그래서 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..