堆
| #include <vector> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| void adjustHeap(vector<int> &nums, int len, int curIndex) { |
| int leftChild = 2 * curIndex + 1; |
| int temp = nums[curIndex]; |
| while (leftChild <= len - 1) { |
| |
| if (leftChild < len - 1 |
| && nums[leftChild + 1] > nums[leftChild]) |
| leftChild++; |
| if (nums[leftChild] <= temp) break; |
| nums[curIndex] = nums[leftChild]; |
| curIndex = leftChild; |
| leftChild = 2 * curIndex + 1; |
| } |
| nums[curIndex] = temp; |
| } |
| |
| |
| void bottomUp(vector<int> &nums) { |
| int n = nums.size(); |
| |
| for (int i = n / 2 - 1; i >= 0; i--) |
| adjustHeap(nums, n, i); |
| } |
| |
| |
| void heapInsert(vector<int> &nums, int len, int val) { |
| nums[len++] = val; |
| int curIndex = len - 1; |
| int parent = (curIndex - 1) / 2; |
| while (nums[curIndex] > nums[parent]) { |
| swap(nums[curIndex], nums[parent]); |
| curIndex = parent; |
| parent = (curIndex - 1) / 2; |
| } |
| } |
| |
| |
| void topDown(vector<int> &nums) { |
| int n = nums.size(); |
| for (int i = 0; i < n; ++i) |
| heapInsert(nums, i, nums[i]); |
| } |
| |
| void heapSort(vector<int> &nums) { |
| int n = nums.size(); |
| |
| int len = nums.size(); |
| |
| for (int i = 1; i < n; ++i) { |
| swap(nums[0], nums[--len]); |
| adjustHeap(nums, len, 0); |
| } |
| } |
| |
| vector<int> sortArray(vector<int> &nums) { |
| bottomUp(nums); |
| |
| heapSort(nums); |
| return nums; |
| } |
| }; |
| class Solution { |
| public: |
| |
| ListNode *mergeKLists(vector<ListNode *> &lists) { |
| |
| if (lists.empty()) return nullptr; |
| ListNode *dummyHead = new ListNode(0, nullptr); |
| ListNode *pre = dummyHead; |
| int len = lists.size(); |
| |
| while (true) { |
| |
| int minHeadIndex = -1; |
| |
| for (int i = 0, finished = 0; i < len; ++i) { |
| if ((lists[i] != nullptr) && |
| (minHeadIndex == -1 || (lists[i]->val < lists[minHeadIndex]->val))) |
| minHeadIndex = i; |
| } |
| |
| if (minHeadIndex == -1) return dummyHead->next; |
| |
| |
| ListNode *node = lists[minHeadIndex]; |
| lists[minHeadIndex] = lists[minHeadIndex]->next; |
| node->next = nullptr; |
| pre->next = node; |
| pre = pre->next; |
| |
| |
| int finished = 0; |
| for (ListNode *curHead: lists) |
| if (curHead == nullptr) finished++; |
| if (finished >= len - 1) break; |
| } |
| |
| |
| for (ListNode *curHead: lists) { |
| if (curHead != nullptr) { |
| pre->next = curHead; |
| return dummyHead->next; |
| } |
| } |
| |
| return dummyHead->next; |
| } |
| }; |
| class Solution { |
| public: |
| |
| ListNode *mergeTwoLists(ListNode *a, ListNode *b) { |
| if ((!a) || (!b)) return a ? a : b; |
| ListNode *dummyHead = new ListNode(0, nullptr); |
| ListNode *pre = dummyHead; |
| ListNode *p = a, *q = b; |
| while (p && q) { |
| if (p->val < q->val) { |
| pre->next = p; |
| p = p->next; |
| } else { |
| pre->next = q; |
| q = q->next; |
| } |
| pre = pre->next; |
| } |
| pre->next = (p ? p : q); |
| return dummyHead->next; |
| } |
| |
| |
| ListNode *mergeKLists(vector<ListNode *> &lists) { |
| ListNode *res = nullptr; |
| for (int i = 0; i < lists.size(); ++i) |
| res = mergeTwoLists(res, lists[i]); |
| return res; |
| } |
| }; |
| class Solution { |
| public: |
| |
| ListNode *mergeTwoLists(ListNode *a, ListNode *b) { |
| if ((!a) || (!b)) return a ? a : b; |
| ListNode *dummyHead = new ListNode(0, nullptr); |
| ListNode *pre = dummyHead; |
| ListNode *p = a, *q = b; |
| while (p && q) { |
| if (p->val < q->val) { |
| pre->next = p; |
| p = p->next; |
| } else { |
| pre->next = q; |
| q = q->next; |
| } |
| pre = pre->next; |
| } |
| pre->next = (p ? p : q); |
| return dummyHead->next; |
| } |
| |
| ListNode *merge(vector<ListNode *> &lists, int left, int right) { |
| if (left > right) return nullptr; |
| if (left == right) return lists[left]; |
| int mid = left + ((right - left) >> 1); |
| return mergeTwoLists(merge(lists, left, mid), merge(lists, mid + 1, right)); |
| } |
| |
| |
| ListNode *mergeKLists(vector<ListNode *> &lists) { |
| return merge(lists, 0, lists.size() - 1); |
| } |
| }; |
| class Solution { |
| public: |
| struct cmp { |
| bool operator()(ListNode *a, ListNode *b) { |
| return (*a).val > (*b).val; |
| } |
| }; |
| |
| |
| ListNode *mergeKLists(vector<ListNode *> &lists) { |
| |
| if (lists.empty()) return nullptr; |
| ListNode *dummyHead = new ListNode(0, nullptr); |
| ListNode *pre = dummyHead; |
| |
| |
| priority_queue<ListNode *, vector<ListNode *>, cmp> heap; |
| |
| for (ListNode *curHead: lists) { |
| if (curHead == nullptr) continue; |
| heap.push(curHead); |
| curHead = curHead->next; |
| } |
| |
| while (!heap.empty()) { |
| |
| ListNode *node = heap.top(); |
| heap.pop(); |
| pre->next = node; |
| pre = pre->next; |
| |
| |
| if (node->next != nullptr) |
| heap.push(node->next); |
| } |
| return dummyHead->next; |
| } |
| }; |
| #include <vector> |
| #include <queue> |
| #include <iostream> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| int main() { |
| int n; |
| cin >> n; |
| vector<vector<int>> lines(n, vector<int>(2, 0)); |
| for (int i = 0; i < n; ++i) |
| cin >> lines[i][0] >> lines[i][1]; |
| |
| |
| sort(lines.begin(), lines.end(), |
| [](vector<int> &v1, vector<int> &v2) { return v1[0] < v2[0]; }); |
| |
| priority_queue<int, vector<int>, greater<int>> heap; |
| |
| int res = 0; |
| |
| |
| |
| |
| |
| for (int i = 0; i < n; ++i) { |
| |
| |
| while (!heap.empty() && heap.top() <= lines[i][0]) |
| heap.pop(); |
| heap.emplace(lines[i][1]); |
| res = max(res, (int) heap.size()); |
| } |
| cout << res; |
| } |
| #include <vector> |
| #include <queue> |
| #include <numeric> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| int halveArray(vector<int> &nums) { |
| |
| priority_queue<double, vector<double>, less<double>> heap(begin(nums), end(nums)); |
| |
| double sum = accumulate(begin(nums), end(nums), (double) 0); |
| |
| sum /= 2; |
| int res = 0; |
| |
| for (double minus = 0, half; minus < sum; res++, minus += half) { |
| half = heap.top() / 2; |
| heap.pop(); |
| heap.emplace(half); |
| } |
| return res; |
| } |
| }; |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/18435407
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步