JAVA beats 95% using Array


  • 0
    J

    public class PhoneDirectory {

    int[] nums;
    int count;
    int i=0; // always return the smallest number;
    public PhoneDirectory(int maxNumbers) {
        count=maxNumbers;
        nums=new int[count];
    }
    
    
    public int get() {
        while(i<count&&nums[i]!=0){
            i++;
        }
        if(i==count) return -1;
        nums[i]--;
        return i++;
    }
    
    
    public boolean check(int number) {
        return nums[number]==0;
    }
    
    
    public void release(int number) {
        nums[number]=0;
        i=Math.min(i,number);  // check if the number released is smaller than i
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.