Heap
文章平均质量分 67
再见小小ronnie
这个作者很懒,什么都没留下…
展开
-
Build a min heap for a given a given array
public class Solution { public void heapify(int[] A) { // only heapify the parent nodes which range is [0, A.length-1/2] // from bottom to up, call minHeapify() for (int i=转载 2017-03-08 11:44:00 · 286 阅读 · 0 评论 -
Leetcode 378. Kth Smallest Element in a Sorted Matrix
O(mlogn), m is number of columns and n is number of rows. O(m*n) space. Merge the matrix into an array, return kth element. public class Solution { public int kthSmallest(int[][] matrix, int k)原创 2017-03-10 11:58:48 · 243 阅读 · 0 评论 -
Leetcode 295. Find Median from Data Stream
Two heap to save the smaller half and larger half number respectively. We need to keep heap as balanced while insertion, i.e., the different of two heaps' size must be If we find the insertion wil转载 2017-03-10 06:27:44 · 209 阅读 · 0 评论 -
Leetcode 414. Third Maximum Number
public class Solution { public int thirdMax(int[] nums) { // Hashset to remove all duplicate elements HashSet set = new HashSet(); for (int num : nums) set.add(原创 2017-04-03 00:40:25 · 215 阅读 · 0 评论 -
Leetcode 347. Top K Frequent Elements
Data structure used, hash table and min heap. A hashmap to save every element along with its frequency for the input data. A min heap to save top k most frequent map entry, comparing by map.value th原创 2017-04-06 03:38:05 · 234 阅读 · 0 评论