일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Python
- data science
- 프로그래머스
- Decorator
- 운영체제
- 파이썬
- iterator
- 시바견
- 109. Convert Sorted List to Binary Search Tree
- 밴픽
- t1
- Regular Expression
- shiba
- 43. Multiply Strings
- Substring with Concatenation of All Words
- DWG
- attribute
- Python Implementation
- Python Code
- Convert Sorted List to Binary Search Tree
- Generator
- concurrency
- 315. Count of Smaller Numbers After Self
- LeetCode
- kaggle
- 컴퓨터의 구조
- 715. Range Module
- 30. Substring with Concatenation of All Words
- Protocol
- Class
Archives
- Today
- Total
Scribbling
[C++] priority queue (pair<int, pair<int, int>>), minheap 본문
Computer Science/C++
[C++] priority queue (pair<int, pair<int, int>>), minheap
focalpoint 2023. 9. 2. 03:50LeetCode: 373. Find K Pairs with Smallest Sums
class Solution {
public:
vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
int m = nums1.size();
int n = nums2.size();
vector<vector<int>> ans;
set<pair<int, int>> visited;
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
greater<pair<int, pair<int, int>>>> minHeap;
minHeap.push({nums1[0] + nums2[0], {0, 0}});
visited.insert({0, 0});
while (k-- && !minHeap.empty()) {
auto top = minHeap.top();
minHeap.pop();
int i = top.second.first;
int j = top.second.second;
ans.push_back({nums1[i], nums2[j]});
if (i + 1 < m && visited.find({i + 1, j}) == visited.end()) {
minHeap.push({nums1[i + 1] + nums2[j], {i + 1, j}});
visited.insert({i + 1, j});
}
if (j + 1 < n && visited.find({i, j + 1}) == visited.end()) {
minHeap.push({nums1[i] + nums2[j + 1], {i, j + 1}});
visited.insert({i, j + 1});
}
}
return ans;
}
};
'Computer Science > C++' 카테고리의 다른 글
[C++] 전문가를 위한 C++ - Chapter 1 (0) | 2023.09.15 |
---|---|
[C++] vector<vector<int>> sorting with comparator (0) | 2023.09.07 |
[C++] vector sum, dp table with vector (0) | 2023.09.02 |
[C++] unordered_map, unordered_set, substr (0) | 2023.08.31 |
[C++] vector sum, vector max (0) | 2023.08.31 |