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 :: Java Access superclass attribute 
Java :: java integer object to char 
Java :: custom dialog in android 
Java :: remove duplicates from list java 
Java :: string to char array 
Java :: stringbuilder in java 
Java :: lcm of two number in java 
Java :: java casting to int 
Java :: @restcontroller 
Java :: java for loop syntax 
Java :: java binary exponentiation 
Java :: can we override the overloaded method in java 
Java :: java return pair 
Java :: java startswith regex 
Java :: java comment 
Java :: void * to int 
Java :: throw exception in spring boot with message and geeter se 
Java :: java returns null 
Java :: package javafx.fxml does not exist 
Java :: java check if array element is null 
Java :: what does system.out.println(y + " ") result 
Java :: stream reduce stringbuilder 
Java :: search and delete class files from jars 
Java :: pvector maximum dimension 
Java :: Printing Hexadecimal Code 
Java :: power of a number in java 
Java :: open google maps cycling navigation intent 
Java :: Java public no-arg constructors 
Java :: how to install openjdk 16 
Java :: .throwFor in java 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =