I want to find min in o(1)
and each time min is called min should be deleted
static ArrayList <Integer> numbers = new ArrayList <Integer> ();
static ArrayList <Integer> min = new ArrayList <Integer> ();
public static void add(int num){
numbers.add(num);
if(min.isEmpty() || num < min.get(min.size()-1))
min.add(num);
}
public static void getMin(){
if(!min.isEmpty()){
System.out.println(min.get(min.size() - 1));
numbers.remove(min.get(min.size() - 1));
min.remove(min.size() - 1);
}
but there is problem
add(7)
add(8)
min
min
the output should be
7
8
but it is
7
how do I fix it ?