Copyright © 直通硅谷
http://www.zhitongguigu.com/
How to manage the processes running in the operating system.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Have to sort every time a new task arrives.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Very fast for getting the lowest/highest priority element.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
100
/ \
19 36
/ \ / \
17 3 25 1
2
/ \
19 3
/ \ / \
20 31 5 8
max-heap
min-heap
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
0
/ \
1 2
/ \ / \
3 4 5 6
/\ / \ / \ / \
7 8 9 10 11 12 13 14
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
90
/ \
70 50
/ \ / \
65 44 30 20
/ \ /
35 21 8
80
90
/ \
70 50
/ \ / \
65 44 30 20
/ \ / \
35 21 8 80
90
/ \
80 50
/ \ / \
65 70 30 20
/ \ / \
35 21 8 44
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
90
/ \
70 50
/ \ / \
65 44 30 20
/ \ /
35 21 8
90
/ \
8 50
/ \ / \
65 44 30 20
/ \
35 21
90
/ \
65 50
/ \ / \
35 44 30 20
/ \
8 21
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
26
Copyright © 直通硅谷
http://www.zhitongguigu.com/
26
/
45
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/
26
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/ \
26 21
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/ \
26 21
/
37
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/ \
37 21
/
26
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/ \
37 21
/ \
26 89
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/ \
89 21
/ \
26 37
Copyright © 直通硅谷
http://www.zhitongguigu.com/
89
/ \
45 21
/ \
26 37
Copyright © 直通硅谷
http://www.zhitongguigu.com/
89
/ \
45 21
/ \ /
26 37 12
Copyright © 直通硅谷
http://www.zhitongguigu.com/
89
/ \
45 21
/ \ / \
26 37 12 9
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/ \
36 18
/ \ / \
53 72 30 48
/ \ /
93 15 35
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/ \
36 18
/ \ / \
53 72 30 48
/ \ /
93 15 35
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/ \
36 18
/ \ / \
53 72 30 48
/ \ /
93 15 35
45
/ \
36 18
/ \ / \
93 72 30 48
/ \ /
53 15 35
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/ \
36 18
/ \ / \
93 72 30 48
/ \ /
53 15 35
45
/ \
36 48
/ \ / \
93 72 30 18
/ \ /
53 15 35
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/ \
36 48
/ \ / \
93 72 30 18
/ \ /
53 15 35
45
/ \
93 48
/ \ / \
36 72 30 18
/ \ /
53 15 35
45
/ \
93 48
/ \ / \
53 72 30 18
/ \ /
36 15 35
Copyright © 直通硅谷
http://www.zhitongguigu.com/
45
/ \
93 48
/ \ / \
53 72 30 18
/ \ /
36 15 35
93
/ \
45 48
/ \ / \
53 72 30 18
/ \ /
36 15 35
93
/ \
72 48
/ \ / \
53 45 30 18
/ \ /
36 15 35
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Comparable
Comparator
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
class MyClass implements Comparable<MyClass> {
@Override
public int compareTo(final MyClass o) {
return this.val - o.val; // increasing (minHeap)
}
}
PriorityQueue<MyClass> heap = new PriorityQueue<>(capacity);
Copyright © 直通硅谷
http://www.zhitongguigu.com/
class MyComparator implements Comparator<MyClass> {
@Override
public int compare(MyClass a, MyClass b) {
return a.val - b.val; // increasing (minHeap)
}
}
MyComparator myComparator = new MyComparator();
PriorityQueue<MyClass> heap
= new PriorityQueue<>(capacity, myComparator);
Copyright © 直通硅谷
http://www.zhitongguigu.com/
PriorityQueue<MyClass> heap =
new PriorityQueue<MyClass>(cap, new Comparator<MyClass>() {
@Override
public int compare(MyClass a, MyClass b) {
return a.val - b.val; // increasing (minHeap)
}
});
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
public ListNode merge(ListNode[] lists);
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Insert all head nodes into minHeap.
while (minHeap is not empty)
root = minHeap.pop();
Add root into result
Insert root.next into minHeap.
return result
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0)
return null;
PriorityQueue<ListNode> minHeap = new PriorityQueue<ListNode>(
lists.length,
new Comparator<ListNode>() {
@Override
public int compare(ListNode node1, ListNode node2) {
return node1.val - node2.val;
}
});
for (int i = 0; i < lists.length; i++) {
if (lists[i] != null) {
minHeap.add(lists[i]);
}
}
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (!minHeap.isEmpty()) {
cur.next = minHeap.poll();
cur = cur.next;
if (cur.next != null)
minHeap.add(cur.next);
}
return dummy.next;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.
Return [3,3,5,5,6,7].
Note:
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return nums;
}
int[] result = new int[nums.length - k + 1];
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(k,
new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return b - a;
}
});
for (int i = 0; i < nums.length; i++) {
if (i >= k) {
maxHeap.remove(nums[i-k]);
}
maxHeap.offer(nums[i]);
if (i >= k - 1) {
result[i - k + 1] = maxHeap.peek();
}
}
return result;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return nums;
}
int[] result = new int[nums.length - k + 1];
PriorityQueue<Integer> minHeap = new PriorityQueue<>(k);
for (int i = 0; i < nums.length; i++) {
if (i >= k) {
minHeap.remove(-nums[i-k]);
}
minHeap.offer(-nums[i]);
if (i >= k - 1) {
result[i - k + 1] = -minHeap.peek();
}
}
return result;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return nums;
}
Deque<Integer> deque = new LinkedList<>();
int[] result = new int[nums.length - k + 1];
for (int i = 0; i < nums.length; i++) {
if (!deque.isEmpty() && deque.peekFirst() == i - k) {
deque.poll();
}
while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {
deque.pollLast();
}
deque.offer(i);
if (i >= k - 1) {
result[i - k + 1] = nums[deque.peek()];
}
}
return result;
}
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.
For example,
Given [3,2,1,5,6,4] and k = 2, return 5.
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
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.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
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.
public int findKthLargest(int[] nums, int k) {
Comparator<Integer> comparator = new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return a - b;
}
};
PriorityQueue<Integer> minHeap = new PriorityQueue(k, comparator);
for (int i = 0; i < k; i++) {
minHeap.add(nums[i]);
}
for (int i = k; i < nums.length; i++) {
if (nums[i] >= minHeap.peek()) {
minHeap.poll();
minHeap.add(nums[i]);
}
}
return minHeap.poll();
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
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.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Examples:
[2,3,4] , the median is 3
[2,3], the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
- void addNum(int num) - Add a integer number from the data stream to the data structure.
- double findMedian() - Return the median of all elements so far.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
class MedianFinder {
public void addNum(int num);
public double findMedian();
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
minHeap
(big nums)
maxHeap
(small nums)
Median
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
minHeap
(big nums)
maxHeap
(small nums)
Median
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
minHeap
(big nums)
maxHeap
(small nums)
Median
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Median
Copyright © 直通硅谷
http://www.zhitongguigu.com/
4
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
4
2
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Median
Copyright © 直通硅谷
http://www.zhitongguigu.com/
4
2
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Median
Copyright © 直通硅谷
http://www.zhitongguigu.com/
4
3
2
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
4
3
2
1
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Median
Copyright © 直通硅谷
http://www.zhitongguigu.com/
4
3
2
1
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Median
Copyright © 直通硅谷
http://www.zhitongguigu.com/
4
3
2
1
5
class MedianFinder {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
// Adds a number into the data structure.
public void addNum(int num) {
if (!minHeap.isEmpty() && num > minHeap.peek()) {
minHeap.offer(num);
} else {
maxHeap.offer(-num);
}
if (minHeap.size() - maxHeap.size() == 2) {
maxHeap.offer(-minHeap.poll());
} else if (maxHeap.size() - minHeap.size() == 2) {
minHeap.offer(-maxHeap.poll());
}
}
// Returns the median of current data stream
public double findMedian() {
if (minHeap.size() > maxHeap.size()) {
return minHeap.peek();
}
if (minHeap.size() < maxHeap.size()) {
return -maxHeap.peek();
}
return (double)(minHeap.peek() - maxHeap.peek()) / 2.0;
}
};
Copyright © 直通硅谷
http://www.zhitongguigu.com/
class MedianFinder {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
// Adds a number into the data structure.
public void addNum(int num) {
minHeap.offer(num);
maxHeap.offer(-minHeap.poll());
if (maxHeap.size() - minHeap.size() > 1) {
minHeap.offer(-maxHeap.poll());
}
}
// Returns the median of current data stream
public double findMedian() {
if (minHeap.size() == maxHeap.size()) {
return (double)(minHeap.peek() - maxHeap.peek()) / 2.0;
}
return -maxHeap.peek();
}
};
Copyright © 直通硅谷
http://www.zhitongguigu.com/
class MedianFinder {
PriorityQueue<Integer> min = new PriorityQueue<>();
PriorityQueue<Integer> max = new PriorityQueue<>(100, Collections.reverseOrder());
// Adds a number into the data structure.
public void addNum(int num) {
min.offer(num);
max.offer(min.poll());
if (min.size() < max.size()) {
min.offer(max.poll());
}
}
// Returns the median of current data stream
public double findMedian() {
return (min.size() == max.size()) ? min.peek()/2.0 + max.peek()/2.0 : min.peek();
}
};
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/