Rotate Array - Leet Code Solution
Problem Statement Given an array, rotate the array to the right by k steps…
July 10, 2019
Max Priority Queue is a data structure which manage a list of keys(values). And, gives priority to element with maximum value.
It supports following operations:
(Heap data structure)[/coding-interview/what-is-heap-data-structure/] can be used for its implementation.
Lets see each one of them:
public class MaxPriorityQueue {
private int[] heap;
private int maxSize;
private int heapSize;
public MaxPriorityQueue(int maxSize) {
this.maxSize = maxSize;
this.heap = new int[maxSize];
this.heapSize = 0;
}
@Override
public String toString() {
return ArrayUtils.toString(this.heap, this.heapSize);
}
private int getLeftChild(int index) { return 2*index + 1;}
private int getRightChild(int index) { return 2*index + 2;}
private int getParent(int index) {
if (index == 0) {
return -1;
}
return (index-1)/2;
}
private void maxHeapify(int index) {
int largest = index;
int l = this.getLeftChild(index);
int r = this.getRightChild(index);
if (l < this.heapSize && this.heap[l] > this.heap[index]) {
largest = l;
}
if (r < this.heapSize && this.heap[r] > this.heap[largest]) {
largest = r;
}
if (largest != index) {
ArrayUtils.swap(this.heap, largest, index);
this.maxHeapify(largest);
}
}
/**
* Get the max element, do not delete
*/
public int getMax() throws Exception {
if (this.heapSize == 0) {
throw new Exception("Heap underflow");
}
return this.heap[0];
}
/**
* Get the max, and delete from heap
* Runs in O(log n)
*/
public int extractMax() throws Exception {
if (this.heapSize == 0) {
throw new Exception("Heap underflow");
}
int ret = this.heap[0];
this.heap[0] = this.heap[this.heapSize-1];
this.heapSize --;
this.maxHeapify(0);
return ret;
}
/**
* Set the value at index specified to new value specified
*/
public void increment(int index, int newValue) throws Exception {
if (index > this.heapSize-1) {
throw new Exception("Overflow");
}
this.heap[index] = newValue;
while (index > 0 && this.heap[index] > this.heap[this.getParent(index)]) {
ArrayUtils.swap(this.heap, index, this.getParent(index));
index = this.getParent(index);
}
}
public void insert(int val) throws Exception {
if (this.heapSize >= this.maxSize) {
throw new Exception("Overflow");
}
this.heapSize ++;
this.heap[this.heapSize-1] = Integer.MIN_VALUE;
this.increment(this.heapSize-1, val);
}
public static void main(String[] args) throws Exception {
MaxPriorityQueue q = new MaxPriorityQueue(10);
//fresh state
System.out.println(q);
//lets insert 5 elements
q.insert(10); q.insert(5); q.insert(4); q.insert(6); q.insert(20);
System.out.println(q);
System.out.println(q.getMax());
System.out.println("Extracting max: " + q.extractMax());
System.out.println("State: " + q);
System.out.println("Incrementing index-2 to 12");
q.increment(2, 12);
System.out.println("State: " + q);
System.out.println("Extracting max: " + q.extractMax());
System.out.println("State: " + q);
System.out.println("Extracting max: " + q.extractMax());
System.out.println("State: " + q);
}
}
To know about basic Heap data structure, and its heapify operation. See: (Heap data structure)[/coding-interview/what-is-heap-data-structure/] Its an important data structure for interview purpose.
Since we are maintaining max heap. The largest value is always at 0th index. Just return it. Runtime complexity is O(1)
Runtime complexity is O(log n)
Runtime complexity is O(log n)
Runtime complexity is O(log n)
Problem Statement Given an array, rotate the array to the right by k steps…
Young Tableau A a X b matrix is Young Tableau if all rows(from left to right…
Problem Statement Given two strings s and t , write a function to determine if t…
Problem Statement Given a sorted array nums, remove the duplicates in-place such…
Sorting Problems Merge Sort Quick Sort Heap Sort Bubble Sort Selection Sort…
Problem Statement Roman numerals are represented by seven different symbols: I…
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…