Heaping up with the inqueries Challenge: 


  Given three query types simulate heap operations.


Specifications:


  1. "1 v" - Add an element v to the heap.
    2. "2 v" - Delete the element v from the heap.
    3. Print the minimum of all the elements in the
      heap.  
  
  It is guaranteed that the element to be deleted will exist in the heap.
  Only distinct elements will be in the heap.  
  
  The first line contains the number of queries, Q.
  Each of the next Q lines contains a single query of any one of the 3 above mentioned types.


Implementation:

import java.util.Set;

public class QHeap1 {
    int minimum = Integer.MAX_VALUE;
    Set&lt;Integer&gt; set = new HashSet&lt;&gt;();

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        QHeap1 qHeap = new QHeap1();

        for (int i = Integer.parseInt(input.nextLine()); i &gt; 0; i--) {
            String query = input.nextLine();
            if (query.startsWith("1")) {
                qHeap.add(Integer.valueOf(query.split("\\s")[1]));
            } else if (query.startsWith("2")) {
                qHeap.remove(Integer.valueOf(query.split("\\s")[1]));
            } else {
                System.out.println(qHeap.minimum);
            }
        }
    }

    private void add(Integer value) {
        set.add(value);
        if (value &lt; minimum) {
            minimum = value;
        }
    }

    private void remove(Integer value) {
        set.remove(value);
        if (minimum == value) {
            minimum = Integer.MAX_VALUE;
            for (Integer newMin : set) {
                if (newMin &lt; minimum) {
                    minimum = newMin;
                }
            }
        }
    }
}


This is a challenge found on Hacker Rank. I passed all test cases, but I'm wondering if there's a way to further increase the performance of this algorithm. Especially concerning the minimum tracking. Interestingly, SortedSet implementations timeout hence the current strategy.

I considered also tracking the second minimum, but that seems a slippery slope to also tracking the second or third, but it could conceivably eliminated having to find the minimum, I'm unsure if it makes sense, generally. What do you think?
I had no problems using a SortedSet, which is both more clear and more efficient than your implementation. You should use Scanner#nextInt() instead of parsing strings yourself. 

I'm currently on a Java 6 machine. Other changes I'd suggest in Java 7+:
You should also close your scanner (use a try-with-resources block). You should prefer a PriorityQueue to a TreeSet, because as @JerryCoffin++ has already pointed out, a heap is probably the correct data structure to be using (depending on the implementation details and frequency of removes, in some cases the TreeSet may perform better).

import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;

public class QHeap1 {

    public static void main(final String[] args) {
        final Scanner input = new Scanner(System.in);
        final int numberOfCommands = input.nextInt();
        final SortedSet&lt;Integer&gt; set = new TreeSet&lt;Integer&gt;();

        for (int i = 0; i &lt; numberOfCommands; i++) {
            final int command = input.nextInt();
            switch (command) {
            case 1: 
                set.add(input.nextInt());
                break;
            case 2:
                set.remove(input.nextInt());
                break;
            case 3:
                System.out.println(set.first());
                break;
            default:
                throw new IllegalStateException();
            }
        }
    }
}