Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java priority queue

PriorityQueue<E> pq = new PriorityQueue<E>();
Comment

priority queue java

import java.util.Arrays;

public class PriorityQueue {
  private int[] items = new int[5];
  private int count;

  // O(n)
  public void add(int item) {
    if (isFull())
      throw new IllegalStateException();

    var i = shiftItemsToInsert(item);
    items[i] = item;
    count++;
  }

  public boolean isFull() {
    return count == items.length;
  }

  private int shiftItemsToInsert(int item) {
    int i;
    for (i = count - 1; i >= 0; i--) {
      if (items[i] > item)
        items[i + 1] = items[i];
      else
        break;
    }
    return i + 1;
  }

  // O(1)
  public int remove() {
    if (isEmpty())
      throw new IllegalStateException();

    return items[--count];
  }

  public boolean isEmpty() {
    return count == 0;
  }

  @Override
  public String toString() {
    return Arrays.toString(items);
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: android sha1 key 
Java :: hello world program in java 
Java :: list to map of list java 8 
Java :: java checking the amount of duplicates in array 
Java :: java variables 
Java :: how to find selenium webelement java 
Java :: heap in java 
Java :: java substring after character 
Java :: hide icon android studio 
Java :: what is packages in java 
Java :: java reverse string 
Java :: declaration of list in java 
Java :: quicksort for arraylist 
Java :: java indexof all occurrences 
Java :: java instanceof keyword 
Java :: antialiasing kjava 
Java :: use regex in if statement java 
Java :: add element in the last in double linked list in java 
Java :: what is inflater in android 
Java :: read wss endpoint java 
Java :: richest customer wealth leetcode 
Java :: This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 4.2 or newer. 
Java :: rider find and replace 
Java :: java Overridding example 
Java :: print double without decimal java 
Java :: java append to file 
Java :: inbuild method to sum of an arraylist elements in java 
Java :: how to name loops in java 
Java :: how to find sum of the digit of the numbers in java 
Java :: arraylist 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =