堆
1
wdt_
即使上线也匆忙,新版本,愁断肠,orz...
展开
-
【堆】A016_LC_第 K 个最小的素数分数(小根堆 / 浮点数二分)
一个已排序好的表 A,其包含 1 和其他一些素数. 当列表中的每一个 p<q 时,我们可以构造一个分数 p/q 。那么第 k 个最小的分数是多少呢? 以整数数组的形式返回你的答案, 这里 answer[0] = p 且 answer[1] = q.输入: A = [1, 2, 3, 5], K = 3输出: [2, 5]解释:已构造好的分数,排序后如下所示:1/5, 1/3, 2/5, 1/2, 3/5, 2/3.很明显第三个最小的分数是 2/5.输入: A = [1, 7],原创 2020-08-13 09:28:02 · 145 阅读 · 0 评论 -
【堆】A016_LC_IPO(排序+大根堆)
假设公司 A 即将开始其 IPO。为了以更高的价格将股票卖给风险投资公司,A 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 k 个不同的项目。帮助 A 设计完成最多 k 个不同项目后得到最大总资本的方式。给定若干个项目。对于每个项目 i,它都有一个纯利润 Pi,并且需要最小的资本 Ci 来启动相应的项目。最初,你有 W 资本。当你完成一个项目时,你将获得纯利润,且利润将被添加到你的总资本中。总而言之,从给定项目中选择最多 k 个不同项目的列表,以最大化最终原创 2020-08-12 23:06:18 · 111 阅读 · 0 评论 -
【堆】B014_LC_重构字符串(频次排序+隔空插入)
给定一个字符串S,检查是否能重新排布其中的字母,使得两相邻的字符不同。若可行,输出任意可行的结果。若不可行,返回空字符串。示例 1:输入: S = "aab"输出: "aba"示例 2:输入: S = "aaab"输出: ""注意: S 只包含小写字母并且长度在[1, 500]区间内方法一:贪心思路对于字符个数超多的字符 x,我们应该用个数较少的放在 x 的中间,对于次多的字符也采取同样的措施;这样做的前提是出现次数最多的字符的个数 ≤ n/2class Solution {原创 2020-08-12 21:34:59 · 158 阅读 · 0 评论 -
【堆】A013_LC_数据流的中位数(抽象两个堆为数据流的左右部分)
中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。例如,[2,3,4] 的中位数是 3[2,3] 的中位数是 (2 + 3) / 2 = 2.5设计一个支持以下两种操作的数据结构:void addNum(int num) - 从数据流中添加一个整数到数据结构中。double findMedian() - 返回目前所有元素的中位数。addNum(1)addNum(2)findMedian() -> 1.5addNum(3) findMedian() -&原创 2020-08-12 20:38:52 · 110 阅读 · 0 评论 -
【堆】A012_LC_K个逆序对数组(k 指针 / 堆优化)
一、Problem你有 k 个升序排列的整数数组。找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中。我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < c,则区间 [a,b] 比 [c,d] 小。输入:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]输出: [20,24]解释: 列表 1:[4, 10, 15, 24, 26],24 在区间 [20,24] 中。列表 2:[0, 9, 12, 20]原创 2020-08-02 09:08:28 · 121 阅读 · 0 评论 -
【堆】B010_LC_最接近原点的 K 个点(3 行排序 / 快速选择)
一、Problem我们有一个由平面上的点组成的列表 points。需要从中找出 K 个距离原点 (0, 0) 最近的点。(这里,平面上两点之间的距离是欧几里德距离。)你可以按任何顺序返回答案。除了点坐标的顺序之外,答案确保是唯一的。输入:points = [[1,3],[-2,2]], K = 1输出:[[-2,2]]解释: (1, 3) 和原点之间的距离为 sqrt(10),(-2, 2) 和原点之间的距离为 sqrt(8),由于 sqrt(8) < sqrt(10),(-2, 2原创 2020-07-07 11:19:09 · 156 阅读 · 0 评论 -
【堆】B009_LC_距离相等的条形码(大顶堆 + 选位置)
一、Problem在一个仓库里,有一排条形码,其中第 i 个条形码为 barcodes[i]。请你重新排列这些条形码,使其中两个相邻的条形码 不能 相等。 你可以返回任何满足该要求的答案,此题保证存在答案。示例 1:输入:[1,1,1,2,2,2]输出:[2,1,2,1,2,1]示例 2:输入:[1,1,2]输出:[1,2,1]提示:1 <= barcodes.length <= 100001 <= barcodes[i] <= 10000二、Soluti原创 2020-06-27 18:26:29 · 175 阅读 · 0 评论 -
【栈】A012_LC_柱状图中最大的矩形(暴力 / 单调栈)
一、ProblemGiven n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Example:Input: [2,1,5,6,2,3]Output: 10二、Solution方法一:暴力(超时)对于位置 iii,要想和位置 iii 的柱形拼原创 2020-05-30 22:17:54 · 221 阅读 · 0 评论 -
【堆】A008_LC_最大的团队表现值(小顶堆)
一、题目描述There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return t...原创 2020-03-15 16:12:17 · 181 阅读 · 0 评论 -
【堆】B007_查找和最小的 K 对数字(最小堆 | 剪枝 | 双剪枝)
一、题目描述You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.Define a pair (u,v) which consists of one element from the first array and one element from the sec...原创 2020-02-21 22:38:18 · 216 阅读 · 0 评论 -
【堆】B006_前 K 个高频单词(排序 | maxHeap| minHeap)
一、题目描述Given a non-empty list of words, return the k most frequent elements.Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word wi...原创 2020-02-21 11:41:58 · 276 阅读 · 0 评论 -
【堆】B005_LC_有序矩阵中第 K 小的元素(归并思想 / 二分优化)
一、题目描述Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted orde...原创 2020-02-20 23:55:22 · 187 阅读 · 0 评论 -
【堆】B004_数组中的第 K 个最大元素(PQ / minHeapify / 快速选择)
一、题目描述Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Input: [3,2,1,5,6,4] and k = 2Output: 5二、题解方法...原创 2020-02-20 12:06:11 · 195 阅读 · 0 评论 -
【堆】B003_LC_前 K 个高频元素(排序 | PQ)
一、题目描述Given a non-empty array of integers, return the k most frequent elements.Input: nums = [1,1,1,2,2,3], k = 2Output: [1,2]二、题解方法一:排序使用排序算法对元素按照出现频率降序排列,最后取出前 kkk 个元素。/** * @date: 2/19/202...原创 2020-02-19 23:15:44 · 167 阅读 · 0 评论 -
【堆】堆的各点总结
把一个元素添加到二叉堆时,默认把元素放在末尾,然后调整它的位置后元素。nnn 个结点的完全二叉树(堆是完全二叉树)的深度为 hhh(根的深度为1),则 h 和 n 之间满足 2h−2<n<=2h2^{h-2} < n <= 2^h2h−2<n<=2h,hhh 近似等于 log(n)+1log(n)+1log(n)+1。每次插入/删除一个结点需要 log(n)...原创 2020-02-19 17:16:17 · 425 阅读 · 0 评论 -
【堆】B002_根据字符出现频率排序(枚举 | PQ)
一、题目描述Given a string, sort it in decreasing order based on the frequency of characters.Input:"tree"Output:"eert"/"eetr"Input:"Aabb"Output:"bbAa"/"bbaA"二、题解方法一:队列构建大根堆算法定义 counter 数组统计字...原创 2020-02-19 12:07:54 · 251 阅读 · 0 评论 -
【堆】C001_数据流中的第 K 大元素(PQ | TS)
一、题目描述Design a class to find the kth largest element in a stream. Note that it is the k_th largest element in the sorted order, not the kth distinct element.Your KthLargest class will have a constru...原创 2020-02-19 11:43:20 · 214 阅读 · 0 评论 -
【堆】C000_最后一块石头的重量(大顶堆 | 数组模拟大顶堆)
一、题目描述有一堆石头,每块石头的重量都是正整数。每一回合,从中选出两块最重的石头,然后将它们一起粉碎。假设石头的重量分别为 x 和 y,且 x <= y。那么粉碎的可能结果如下: 如果 x == y,那么两块石头都会被完全粉碎; 如果 x != y,那么重量为 x 的石头将会完全粉碎,而重量为 y 的石头新重量为 y-x。最后,最多只会剩下一块石头。返回此石头的重量。如果没有石...原创 2020-02-18 11:50:07 · 179 阅读 · 0 评论