Count of Smaller Numbers After Self LeetCode solution in Java
1class Solution {2public List<Integer> countSmaller(int[] nums) {3int min = 20001;4int max = -1;5for (int num : nums) {6min = Math.min(min, num);7max = Math.max(max, num);8}910min--;11int[] count = new int[max - min + 1];12Integer[] result = new Integer[nums.length];13for (int i = nums.length - 1; i >= 0; i--) {14int k = nums[i] - min - 1;15int c = 0;16do {17c += count[k];18k -= (-k & k);19} while (k > 0);20result[i] = c;2122k = nums[i] - min;23while (k < count.length) {24count[k]++;25k += (-k & k);26}27}2829return Arrays.asList(result);30}31}