Skip to content

Commit 14673b7

Browse files
committed
heap
1 parent 5051c5f commit 14673b7

32 files changed

+3374
-2636
lines changed

Java/HashHeap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
H
22
1532965421
3-
tags: HashHeap
3+
tags: HashHeap, Heap
44

55
非题.是从九章找来的HashHeap implementation.
66

Java/Heapify.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
R
2-
tags: Heap
2+
tags: Heap, MinHeap
33

44
Turn unsorted array into a min-heap array, where for each A[i],
55

Java/Kth Largest Element in an Array.java

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
M
2-
1531896299
3-
tags: Divide and Conquer, Heap
2+
1533137926
3+
tags: Divide and Conquer, Heap, PriorityQueue, MinHeap, Quick Sort
4+
5+
kth largest in array
6+
7+
#### PriorityQueue, MinHeap
8+
- Need to maintain k large elements, where the smallest will be compared and dropped if applicable:
9+
- Maintain k elements with min value: consider using minHeap
10+
- add k base elements first
11+
- Maintain MinHeap: only allow larger elements (which will squzze out the min value)
12+
- Remove peek() of queue if over size
13+
- O(nlogk)
14+
15+
16+
#### Quick Sort
17+
- 用Quick Sort 里面partion的一部分
18+
- sort结束后是ascending的, 那么 n - k 就是第k大.
19+
- partion的结果是那个low, 去找 low==nums.size() - k也就是倒数第K个
20+
- 没找到继续partion recursively.
21+
- sort的过程是排一个从小到大的list. (同样的代码还可以好xth smallestmid变成x就好)
22+
- Steps:
23+
- 每个iteration, 找一个pivot,然后从low,和high都和pivot作比较
24+
- 找到一个low>pivot, high<pivot, 也就可以swap了
25+
- 得到的low就是当下的partion point了
26+
- Overall O(nlogN), average O(n) for this problem.
427

528
```
629
/**
@@ -38,4 +61,120 @@ public int findKthLargest(int[] nums, int k) {
3861
return queue.poll();
3962
}
4063
}
64+
65+
66+
// Quick sort/ partition
67+
// Partition to return the `low` index, which should match targetIndex.
68+
class Solution {
69+
public int findKthLargest(int[] nums, int k) {
70+
if (nums == null || nums.length == 0) return -1;
71+
int n = nums.length;
72+
return partition(nums, 0, n - 1, n - k);
73+
}
74+
75+
private int partition (int[] nums, int start, int end, int targetIndex) {
76+
// define low/high
77+
int pivot = end;
78+
int low = start, high = end, num = nums[pivot];
79+
80+
// move pointer and swap
81+
while (low < high) {
82+
while (low < high && nums[low] < num) {
83+
low++;
84+
}
85+
while (low < high && nums[high] >= num) {
86+
high--;
87+
}
88+
swap(nums, low, high);
89+
}
90+
swap(nums, low, pivot);
91+
92+
// compare if low == targetIndex; or recursively partition to find targetIndex
93+
if (low == targetIndex) {
94+
return nums[low];
95+
} else if (low < targetIndex) {
96+
return partition(nums, low + 1, end, targetIndex);
97+
} else {
98+
return partition(nums, start, low - 1, targetIndex);
99+
}
100+
}
101+
102+
private void swap(int[] nums, int x, int y) {
103+
int temp = nums[x];
104+
nums[x] = nums[y];
105+
nums[y] = temp;
106+
}
107+
}
108+
109+
/*
110+
LintCode
111+
Find K-th largest element in an array.
112+
113+
Example
114+
In array [9,3,2,4,8], the 3rd largest element is 4
115+
116+
In array [1,2,3,4,5], the 1st largest element is 5,
117+
2nd largest element is 4, 3rd largest element is 3 and etc.
118+
119+
Note
120+
You can swap elements in the array
121+
122+
Challenge
123+
O(n) time, O(1) space
124+
125+
Tags Expand
126+
Quick Sort Sort
127+
128+
*/
129+
130+
/*
131+
132+
Thoughts:
133+
Almost the same as the Median problem:
134+
the only difference is, this one is not looking for the middle point, but for the last kth element.
135+
Using the same quick sort code with minor modifications, and we can solve this problem.
136+
*/
137+
138+
class Solution {
139+
//param k : description of k
140+
//param numbers : array of numbers
141+
//return: description of return
142+
public int kthLargestElement(int k, ArrayList<Integer> nums) {
143+
if (nums == null || nums.size() == 0) {
144+
return 0;
145+
}
146+
return helper(nums, 0, nums.size() - 1, nums.size() - k);
147+
}
148+
149+
public void swap( ArrayList<Integer>nums, int x, int y){
150+
int temp = nums.get(x);
151+
nums.set(x, nums.get(y));
152+
nums.set(y, temp);
153+
}
154+
155+
public int helper( ArrayList<Integer> nums, int start, int end, int mid) {
156+
int pivot = end;
157+
int num = nums.get(pivot);
158+
int low = start;
159+
int high = end;
160+
while (low < high) {
161+
while(low < high && nums.get(low) < num) {
162+
low++;
163+
}
164+
while(low < high && nums.get(high) >= num) {
165+
high--;
166+
}
167+
swap(nums, low, high);
168+
}
169+
swap(nums, low, pivot);
170+
if (low == mid) {
171+
return nums.get(low);
172+
} else if (low < mid) {
173+
return helper(nums, low + 1, end, mid);
174+
} else {
175+
return helper(nums, start, low - 1, mid);
176+
}
177+
}
178+
};
179+
41180
```

Java/Kth Largest Element.java

Lines changed: 0 additions & 107 deletions
This file was deleted.

Java/Meeting Rooms II.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
M
22
1521167295
3-
tags: Heap, Greedy, Sort, Sweep Line
3+
tags: Heap, Greedy, Sort, Sweep Line, PriorityQueue
44

55
给一串数字pair, 代表会议的开始/结束时间. 找同时又多少个会议发生(需要多少件房间)
66

@@ -43,25 +43,18 @@
4343
*/
4444
class Solution {
4545
class Point {
46-
int pos;
47-
int flag;
46+
int pos, flag;
4847
public Point(int pos, int flag) {
4948
this.pos = pos;
5049
this.flag = flag;
5150
}
5251
}
5352
public int minMeetingRooms(Interval[] intervals) {
54-
if (intervals == null || intervals.length == 0) {
55-
return 0;
56-
}
57-
int count = 0;
58-
int max = 0;
59-
PriorityQueue<Point> queue = new PriorityQueue<Point>(new Comparator<Point>() {
60-
public int compare(Point a, Point b) {
61-
return a.pos - b.pos;
62-
}
63-
});
64-
53+
if (intervals == null || intervals.length == 0) return 0;
54+
55+
int count = 0, max = 0;
56+
// init
57+
PriorityQueue<Point> queue = new PriorityQueue<>(Comparator.comparing(a -> a.pos));
6558
for (Interval interval: intervals) {
6659
queue.offer(new Point(interval.start, 1));
6760
queue.offer(new Point(interval.end, -1));
@@ -77,7 +70,6 @@ public int compare(Point a, Point b) {
7770

7871
max = Math.max(count, max);
7972
}
80-
8173
return max;
8274
}
8375
}

Java/Merge k Sorted Arrays.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
M
2-
1528048508
3-
tags: Heap, PriorityQueue
2+
1533139060
3+
tags: Heap, PriorityQueue, MinHeap
4+
5+
Same as merge k sorted list, use priorityQueue
46

57
#### Priority Queue
68
- 由Merge k sorted list启发用PriorityQueue,存那k个首发element
79
- PriorityQueue需要存储单位: 自己建一个Class Node 存val, x, y index.
810
- 因为array里没有 'next' pointer只能存x,y来推next element
11+
- Not sure why `new PriorityQueue<>(Comparator.comparing(a -> a.val));` is slower
912

1013
```
1114
/*
1215
Given k sorted integer arrays, merge them into one sorted array.
1316
14-
Have you met this question in a real interview? Yes
1517
Example
1618
Given 3 sorted arrays:
1719
@@ -42,19 +44,20 @@ public Node(int val, int x, int y) {
4244
}
4345

4446
public int[] mergekSortedArrays(int[][] arrays) {
45-
List<Integer> rst = new ArrayList<Integer>();
46-
if (arrays == null || arrays.length == 0) {
47-
return new int[0];
48-
}
47+
List<Integer> rst = new ArrayList<>();
48+
if (arrays == null || arrays.length == 0) return new int[0];
4949

50+
51+
// Faster
52+
// Somehow, slower: PriorityQueue<Node> queue = new PriorityQueue<>(Comparator.comparing(a -> a.val));
5053
PriorityQueue<Node> queue = new PriorityQueue<>(arrays.length,
5154
new Comparator<Node>() {
5255
public int compare(Node a, Node b){
5356
return a.val - b.val;
5457
}
5558
}
5659
);
57-
60+
5861
//init
5962
for (int i = 0; i < arrays.length; i++) {
6063
if (arrays[i].length != 0) {

0 commit comments

Comments
 (0)