优先队列-二叉堆
文章平均质量分 83
介绍二叉堆的使用
memcpy0
希望探索文理结合的自由之路。
展开
-
LeetCode 2462. 雇佣 K 位工人的总代价【最小堆,模拟】
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2024-05-05 22:34:39 · 802 阅读 · 0 评论 -
LeetCode 2558. 从数量最多的堆取走礼物【模拟,堆或原地堆化】简单
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-10-28 11:55:33 · 144 阅读 · 0 评论 -
LeetCode 2530. 执行 K 次操作后的最大分数【贪心,原地堆化】1386
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-10-18 10:12:27 · 142 阅读 · 0 评论 -
LeetCode 630. Course Schedule III【反悔贪心,堆,排序】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。为了方便在PC上运行调试、分享代码文件,我还建立了相关的。原创 2023-09-10 12:08:31 · 207 阅读 · 0 评论 -
LeetCode 1029. Two City Scheduling 【贪心,堆,排序,快速选择】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-09 15:28:53 · 767 阅读 · 0 评论 -
LeetCode 407. Trapping Rain Water II【数组/BFS/堆/最短路】困难
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。 为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conqu.原创 2021-11-04 09:56:06 · 199 阅读 · 0 评论 -
LeetCode 面试题 17.14. Smallest K LCCI【堆/排序/分治】中等
Design an algorithm to find the smallest K numbers in an array. Example: Input: arr = [1,3,5,7,2,4,6,8], k = 4 Output: [1,2,3,4] Note: 0 <= len(arr) <= 100000 0 <= k <= min(100000, len(arr)) 题意:设计一个算法,找出数组中最小的 k 个数。以任意顺序返回这 k 个数均可。 ..原创 2021-09-06 17:22:00 · 140 阅读 · 0 评论 -
LeetCode 295. Find Median from Data Stream【堆】困难
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values. For example, for arr = [2,3,4], the median is 3. For example, for arr = [2,3], the原创 2021-09-03 19:09:32 · 188 阅读 · 0 评论 -
LeetCode C++ 1648. Sell Diminishing-Valued Colored Balls【Heap/Greedy/二分】中等
You have an inventory of different colored balls, and there is a customer that wants orders balls of any color. The customer weirdly values the colored balls. Each colored ball's value is the number of balls of that color you currently have in your in原创 2021-05-07 16:41:30 · 210 阅读 · 0 评论 -
LeetCode C++ 1738. Find Kth Largest XOR Coordinate Value【堆/二维前缀和】中等
You are given a 2D matrix of size m x n, consisting of non-negative integers. You are also given an integer k. The value of coordinate (a, b) of the matrix is the XOR of all matrix[i][j] where 0 <= i <= a < m and 0 <= j <= b < n (0-indexe原创 2021-05-07 02:20:46 · 284 阅读 · 0 评论 -
LeetCode C++ 1845. Seat Reservation Manager【Heap/Design】中等
Design a system that manages the reservation state of n seats that are numbered from 1 to n. Implement the SeatManager class: SeatManager(int n) Initializes a SeatManager object that will manage n seats numbered from 1 to n. All seats are initially avail原创 2021-05-04 01:49:38 · 209 阅读 · 0 评论 -
LeetCode C++ 703. Kth Largest Element in a Stream【Heap/Design】简单
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element. Implement KthLargest class: KthLargest(int k, int[] nums) Initializes the object wi原创 2020-12-25 21:43:49 · 204 阅读 · 0 评论 -
LeetCode C++ 1405. Longest Happy String【Greedy/Heap】中等
A string is called happy if it does not have any of the strings 'aaa', 'bbb' or 'ccc' as a substring. Given three integers a, b and c, return any string s, which satisfies following conditions: s is happy and longest possible. s原创 2020-12-09 01:25:12 · 246 阅读 · 0 评论 -
LeetCode C++ 23. Merge k Sorted Lists【排序/堆/分治/链表】困难
You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Example 1: Input: lists = [[1,4,5],[1,3,4],[2,6]] Output: [1,1,2,3,4,4,5,6] Explanation: Th原创 2020-10-08 20:01:08 · 215 阅读 · 0 评论 -
LeetCode C++ 347. Top K Frequent Elements【优先队列】中等
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elemen原创 2020-08-26 00:11:34 · 262 阅读 · 0 评论 -
【数据结构】优先队列专题 左式堆
文章目录1. 左式堆概述2. 左式堆性质3. 左式堆操作和代码实现(1) 合并操作(2) 插入操作(3) 删除最小值操作 1. 左式堆概述 为了有效支持合并操作,即以 o(N)o(N)o(N) 时间进行 Merge ,我们需要使用动态的链接结构或者静态链表。如果只使用数组形式的堆结构,在进行合并时光拷贝一个数组到另一个数组中,就将花费 Θ(N)\Theta(N)Θ(N) 的时间。 左式堆 (leftist tree, leftist heap ),或者说左偏堆、左偏树,是一种优先队列实现方式,属于可并堆(原创 2020-08-10 22:59:44 · 1728 阅读 · 0 评论