Array:Count of Smaller Numbers After Self

You are given an integer array nums and you have to return a new counts array.
The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]

 public List countSmaller(int[] nums) {
        Integer[] arr = new Integer[nums.length];
        ArrayList sorted = new ArrayList();
        for (int i = nums.length-1; i >= 0; i--) {
            int index = getIndex(sorted, nums[i]);
            arr[i] = index;
            sorted.add(index, nums[i]);
        }
        return Arrays.asList(arr);
    }
    private int getIndex(ArrayList sorted,int target) {
        if (sorted.size()==0) {
            return 0;
        }
        int start = 0;
        int end = sorted.size()-1;
        if (sorted.get(end)target) {
            return 0;
        }
        while (start

你可能感兴趣的:(Array:Count of Smaller Numbers After Self)