1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { public int[] smallerNumbersThanCurrent(int[] nums) {
int n = nums.length; int[] ans = new int[n]; Map<Integer, Integer> map = new HashMap<>(); int tmp = 0; int[] nums2= Arrays.copyOf(nums, n); Arrays.sort(nums2); for (int i = 0; i < n; i++) { if (i == 0) { map.put(nums2[i], i); continue; } if (nums2[i] == nums2[i-1]){ map.put(nums2[i], tmp); }else { tmp = i; map.put(nums2[i], i); } }
for (int i = 0; i < n; i++) { ans[i] = map.get(nums[i]); } return ans; } }
|