|
| 1 | +# 315. Count of Smaller Numbers After Self |
| 2 | + |
| 3 | +- Difficulty: Hard. |
| 4 | +- Related Topics: Array, Binary Search, Divide and Conquer, Binary Indexed Tree, Segment Tree, Merge Sort, Ordered Set. |
| 5 | +- Similar Questions: Count of Range Sum, Queue Reconstruction by Height, Reverse Pairs, How Many Numbers Are Smaller Than the Current Number, Count Good Triplets in an Array, Count the Number of K-Big Indices. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +Given an integer array `nums`, return** an integer array **`counts`** where **`counts[i]`** is the number of smaller elements to the right of **`nums[i]`. |
| 10 | + |
| 11 | + |
| 12 | +Example 1: |
| 13 | + |
| 14 | +``` |
| 15 | +Input: nums = [5,2,6,1] |
| 16 | +Output: [2,1,1,0] |
| 17 | +Explanation: |
| 18 | +To the right of 5 there are 2 smaller elements (2 and 1). |
| 19 | +To the right of 2 there is only 1 smaller element (1). |
| 20 | +To the right of 6 there is 1 smaller element (1). |
| 21 | +To the right of 1 there is 0 smaller element. |
| 22 | +``` |
| 23 | + |
| 24 | +Example 2: |
| 25 | + |
| 26 | +``` |
| 27 | +Input: nums = [-1] |
| 28 | +Output: [0] |
| 29 | +``` |
| 30 | + |
| 31 | +Example 3: |
| 32 | + |
| 33 | +``` |
| 34 | +Input: nums = [-1,-1] |
| 35 | +Output: [0,0] |
| 36 | +``` |
| 37 | + |
| 38 | + |
| 39 | +**Constraints:** |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +- `1 <= nums.length <= 105` |
| 44 | + |
| 45 | +- `-104 <= nums[i] <= 104` |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +## Solution |
| 50 | + |
| 51 | +```javascript |
| 52 | +/** |
| 53 | + * @param {number[]} nums |
| 54 | + * @return {number[]} |
| 55 | + */ |
| 56 | +var countSmaller = function(nums) { |
| 57 | + var arr = nums.map((num, i) => [num, i]); |
| 58 | + var res = Array(nums.length).fill(0); |
| 59 | + mergeSort(arr, res); |
| 60 | + return res; |
| 61 | +}; |
| 62 | + |
| 63 | +var mergeSort = function(arr, res) { |
| 64 | + if (arr.length === 1) return arr; |
| 65 | + var mid = Math.floor(arr.length / 2); |
| 66 | + var left = mergeSort(arr.slice(0, mid), res); |
| 67 | + var right = mergeSort(arr.slice(mid), res); |
| 68 | + return merge(left, right, res); |
| 69 | +}; |
| 70 | + |
| 71 | +var merge = function(left, right, res) { |
| 72 | + var arr = []; |
| 73 | + var leftIndex = 0; |
| 74 | + var rightIndex = 0; |
| 75 | + while (leftIndex < left.length || rightIndex < right.length) { |
| 76 | + if (!right[rightIndex] || (left[leftIndex] && left[leftIndex][0] > right[rightIndex][0])) { |
| 77 | + arr.push(left[leftIndex]); |
| 78 | + res[left[leftIndex][1]] += right.length - rightIndex; |
| 79 | + leftIndex += 1; |
| 80 | + } else { |
| 81 | + arr.push(right[rightIndex]); |
| 82 | + rightIndex += 1; |
| 83 | + } |
| 84 | + } |
| 85 | + return arr; |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
| 89 | +**Explain:** |
| 90 | + |
| 91 | +nope. |
| 92 | + |
| 93 | +**Complexity:** |
| 94 | + |
| 95 | +* Time complexity : O(n * log(n)). |
| 96 | +* Space complexity : O(n). |
0 commit comments