Java HashMap & HashSet Beats 83%


  • 0
    E
    public int[] nextGreaterElement(int[] little, int[] big) {
            HashSet<Integer> set = new HashSet<>();
            HashMap<Integer, Integer> map = new HashMap<>();
            int result[] = new int[little.length];
            boolean flag;
           
            for(int i : little) set.add(i);
            for(int j = 0; j < big.length; j++)
              if(set.contains(big[j])) map.put(big[j], j);
            
            for(int k = 0; k < little.length; k++){
                int index = map.get(little[k]);
                flag = true;
                for(int l = index; l < big.length; l++){
                  if(big[l] > little[k]){
                     result[k] = big[l]; 
                     flag = false;
                     break;
                  }
                  if(flag) result[k] = - 1;
                }
            }
          return result; 
        }

Log in to reply
 

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