|
1 |
| -# My whiteboard coding interview operating system !!! |
| 1 | +# Fight towards 1000 commits |
2 | 2 | <!-- MarkdownTOC -->
|
3 | 3 |
|
4 | 4 | - [Typical whiteboard coding workflow](#typical-whiteboard-coding-workflow)
|
|
80 | 80 | - [Parentheses](#parentheses)
|
81 | 81 | - [Queue](#queue)
|
82 | 82 | - [Interfaces](#interfaces)
|
83 |
| - - [PriorityQueue](#priorityqueue) |
| 83 | + - [Heap](#heap) |
84 | 84 | - [Heapify](#heapify)
|
85 | 85 | - [Sift up/down](#sift-updown)
|
| 86 | + - [PriorityQueue](#priorityqueue) |
86 | 87 | - [Improve built-in remove O\(n\)](#improve-built-in-remove-on)
|
87 | 88 | - [Lambda expression as comparator](#lambda-expression-as-comparator)
|
88 | 89 | - [Top K problems](#top-k-problems)
|
@@ -1555,9 +1556,61 @@ boolean isValid( String s )
|
1555 | 1556 | | examine | E element() | E peek() |
|
1556 | 1557 |
|
1557 | 1558 |
|
1558 |
| -#### PriorityQueue |
| 1559 | +#### Heap |
| 1560 | +* Heap is a data structure, which usually have three methods: push, pop and top. where "push" add a new element the heap, "pop" delete the minimum/maximum element in the heap, "top" return the minimum/maximum element. |
| 1561 | + |
1559 | 1562 | ##### Heapify
|
| 1563 | +* Def: Convert an unordered integer array into a heap array. If it is min-heap, for each element A[i], we will get A[i * 2 + 1] >= A[i] and A[i * 2 + 2] >= A[i]. |
| 1564 | +* Heapify is an O(n) operation. The reason is as follows: |
| 1565 | + - At the bottommost level, there are 2^(h)nodes, but we do not call heapify on any of these, so the work is 0. At the next to level there are 2^(h − 1) nodes, and each might move down by 1 level. At the 3rd level from the bottom, there are 2^(h − 2) nodes, and each might move down by 2 levels. |
| 1566 | + |
1560 | 1567 | ##### Sift up/down
|
| 1568 | + |
| 1569 | +```java |
| 1570 | +private void siftdown( int[] A, int k ) |
| 1571 | +{ |
| 1572 | + while ( k < A.length ) |
| 1573 | + { |
| 1574 | + int smallest = k; |
| 1575 | + if ( k * 2 + 1 < A.length && A[k * 2 + 1] < A[smallest] ) |
| 1576 | + { |
| 1577 | + smallest = k * 2 + 1; |
| 1578 | + } |
| 1579 | + if ( k * 2 + 2 < A.length && A[k * 2 + 2] < A[smallest] ) |
| 1580 | + { |
| 1581 | + smallest = k * 2 + 2; |
| 1582 | + } |
| 1583 | + |
| 1584 | + if ( smallest == k ) |
| 1585 | + { |
| 1586 | + break; |
| 1587 | + } |
| 1588 | + else |
| 1589 | + { |
| 1590 | + swap( A, smallest, k ); |
| 1591 | + k = smallest; |
| 1592 | + } |
| 1593 | + } |
| 1594 | +} |
| 1595 | + |
| 1596 | +private void swap( int[] A, int pos1, int pos2 ) |
| 1597 | +{ |
| 1598 | + if ( pos1 != pos2 ) |
| 1599 | + { |
| 1600 | + int temp = A[smallest]; |
| 1601 | + A[smallest] = A[k]; |
| 1602 | + A[k] = temp; |
| 1603 | + } |
| 1604 | +} |
| 1605 | + |
| 1606 | +public void heapify(int[] A) { |
| 1607 | + for (int i = A.length / 2; i >= 0; i--) { |
| 1608 | + siftdown(A, i); |
| 1609 | + } |
| 1610 | +} |
| 1611 | +``` |
| 1612 | + |
| 1613 | +#### PriorityQueue |
1561 | 1614 | ##### Improve built-in remove O(n)
|
1562 | 1615 | * Built-in implementation remove() method for priorityqueue has O(n) time complexity.
|
1563 | 1616 | - O(n) time is spent on looping through entire queue to find the element to be removed. O(logn) is used to remove the element
|
|
0 commit comments