Find if Array contains Duplicate Number - Leet Code Solution
Problem Statement Given an array of integers, find if the array contains any…
July 03, 2019
Its a tree based data structure which is a complete binary tree(all nodes have 2 nodes, leaf-child may not have all 2), and satisfies a heap property. But, the amazing thing is that it uses array as its underlying data structure.
The heap is the implementation of the Priority Queue, where top most priority (or lowest priority) element is required to be at the top.
In this tree representation, every root node is always greater than both of its child. Note: right child can be lesser than left child.
As reverse of the max heap. Every root element is always smaller than both of its child elements.
Some basic operations on heap are:
As I said earlier, they are based on array, not on any pointers stuff. Consider an array:
224,131,23,45,52,287,304,122,460,438
The first element will be the root of tree, 2nd and 3rd are the child of it. 4th and 5th will be child of 2nd node. And so on.
224
/ \
131 23
/ \ / \
45 52 287 304
/ \ /
122 460 438
Note: Above is just tree representation from array. It does not satisfy any max-heap or min-heap property.
leftChildIndex = rootIndex * 2 + 1
rightChildIndex = rootIndex * 2 + 2
Lets assume we have to build a max heap. We would want every root node to be greater than its child nodes.
void max_heapify(int[] arr, int index, int heapsize) {
int l = 2 * index + 1;
int r = 2 * index + 2;
int indexOfLargest = index;
if (l < heapsize && arr[l] > arr[index]) {
indexOfLargest = l;
}
if (r < heapsize && arr[r] > arr[indexOfLargest]) {
indexOfLargest = r;
}
if (indexOfLargest != index) {
swap(arr, index, indexOfLargest);
max_heapify(indexOfLargest);
}
}
This operation takes O(log n)
Here for the index passed, we are comparing it with its child elements. If any child is greater than child, it will be swapped. Since, we have put a newer element to its kid. That sub-tree also has to satisfy max-heap property. And, we go in a natural recursive algorithm.
void buildMaxHeap(int[] arr, int heapsize) {
for (int i=heapsize/2; i>=0; i--) {
max_heapify(arr, i, heapsize);
}
}
This operation takes O(n log n) (including time complexity of max_heapify() as well)
In above example of input, after buildMaxHeap, it will look like:
473,445,295,310,404,199,257,25,21,47
473
/ \
445 295
/ \ / \
310 404 199 257
/ \ /
25 21 47
Heap data structure has many usage, and is very useful data structure.
Problem Statement Given an array of integers, find if the array contains any…
Problem Statement You are given two non-empty linked lists representing two non…
A Binary tree is a data structure which has two children nodes attached to it…
Counting sort runs on relatively smaller set of input. Counting sort calculates…
This algorithm is very useful for large input. And, is quite efficient one. It…
Problem Statement Say you have an array prices for which the ith element is the…
Introduction In this post we will see following: How to schedule a job on cron…
Introduction There are some cases, where I need another git repository while…
Introduction In this post, we will see how to fetch multiple credentials and…
Introduction I have an automation script, that I want to run on different…
Introduction I had to write a CICD system for one of our project. I had to…
Introduction Java log4j has many ways to initialize and append the desired…