Accepted Java Solution


  • 0
    Y
     public int smallestDistancePair(int[] nums, int k) {
            Arrays.sort(nums);
            int max = nums[nums.length - 1] - nums[0];
            int array[] = new int[max + 1];
            for(int i = 0; i < nums.length; i++) {
                for(int j = i + 1; j < nums.length; j++) {
                    int diff = Math.abs(nums[j] - nums[i]);
                    array[diff]++;
                }
            }
            int sum = 0;
            int res = 0;
            for(int i = 0; i <= max; i++) {
                sum += array[i];
                if (sum >= k) {
                    res = i;
                    break;
                }
            }
            return res;
        }
    

  • 0

    @yuryant If max is large, it's gonna consume too much space.


Log in to reply
 

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