# LeetCode 题解：315. 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].

Example:

Given nums = [5, 2, 6, 1]

To the right of 5 there are 2 smaller elements (2 and 1).
To the right of 2 there is only 1 smaller element (1).
To the right of 6 there is 1 smaller element (1).
To the right of 1 there is 0 smaller element.


Return the array [2, 1, 1, 0].

class Solution {

public List<Integer> countSmaller(int[] nums) {
List<Integer> list = new ArrayList<>(nums.length);
for (int i = 0; i < nums.length; i++) {
}
return list;
}

private int getCount(int i, int[] nums) {
int j = i + 1, count = 0;
while (j < nums.length) {
if (nums[j++] < nums[i]) count++;
}
return count;
}

}

class Solution {
private List<Integer> list = null;

public List<Integer> countSmaller(int[] nums) {
list = new ArrayList<>(nums.length);
for (int i = 0; i < nums.length; i++) list.add(0);
Node[] nodes = getNodeList(nums);
mergeSortWhileCount(nodes, 0, nodes.length - 1);
return list;
}

private void mergeSortWhileCount(Node[] nodes, int s, int e) {
if (s >= e) return;
int mid = (s + e) / 2;
mergeSortWhileCount(nodes, s, mid);
mergeSortWhileCount(nodes, mid + 1, e);
for (int i = s, j = mid + 1; i <= mid; i++) {
while (j <= e && nodes[j].val < nodes[i].val) j++;
int originIndex = nodes[i].index;
list.set(originIndex, list.get(originIndex) + (j - mid - 1));
}
Arrays.sort(nodes, s, e + 1, Comparator.comparingInt(x -> x.val));
}

private Node[] getNodeList(int[] nums) {
Node[] res = new Node[nums.length];
for (int i = 0; i < nums.length; i++)
res[i] = new Node(nums[i], i);
return res;
}

static class Node {
int val;
int index;

public Node(int val, int index) {
this.val = val;
this.index = index;
}
}
}

class Solution {
public List<Integer> countSmaller(int[] nums) {
Integer[] ans = new Integer[nums.length];
Node root = null;
for (int i = nums.length - 1; i >= 0; i--) {
root = insert(root, new Node(nums[i]), ans, i, 0);
}
return Arrays.asList(ans);
}

private Node insert(Node root, Node node, Integer[] ans, int i, int preSum) {
if (root == null) {
ans[i] = preSum;
return node;
} else if (root.val == node.val) {
ans[i] = preSum + root.sum;
root.dup++;
} else if (node.val < root.val) {
root.sum++;
root.left = insert(root.left, node, ans, i, preSum);
} else {
root.right = insert(root.right, node, ans, i, preSum + root.sum + root.dup);
}
return root;
}

static class Node {
int sum = 0, val, dup = 1;
Node left, right;

public Node(int val) {
this.val = val;
}
}

}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120