Can ANYONE HELP ME WHATS UP WITH THIS SOLUTION? it cannot pass the last test!


  • 0
    H
    public List<Integer> countSmaller(int[] nums) {
        List<Integer> list = new ArrayList<>();
        List<Integer> res = new ArrayList<>();
        for (int i = nums.length - 1; i >= 0; i --) {
            int index = Collections.binarySearch(list, nums[i]);
            if (index >= 0) {
                while (index - 1 >= 0 && list.get(index - 1) == list.get(index)) {
                    index --;
                }
                list.add(index, nums[i]);
            } else {
                index = - index - 1;
                if (index < list.size()) {
                    list.add(index, nums[i]);
                } else {
                    list.add(nums[i]);
                }
            }
            res.add(0, index);
        }
        return res;
    }

Log in to reply
 

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