LeetCode Count of Smaller Numbers After Self

Posted Dylan_Java_NYC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode Count of Smaller Numbers After Self相关的知识,希望对你有一定的参考价值。

原题链接在这里:https://leetcode.com/problems/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].

题解:

从右向左扫描数组nums, try to find the position of nums[i] in BST.

For each BST node, it contains its left subtree size count, its duplicate count.

When inserting a new node, returns the sum of smaller count. 

Time Complexity: O(n^2). BST不一定balance. Space: O(n).

AC Java:

 1 class Solution {
 2     public List<Integer> countSmaller(int[] nums) {
 3         LinkedList<Integer> res = new LinkedList<>();
 4         if(nums == null || nums.length == 0){
 5             return res;
 6         }
 7         
 8         int n = nums.length;
 9         res.offerFirst(0);
10         TreeNode root = new TreeNode(nums[n - 1]);
11         root.count = 1;
12         
13         for(int i = n - 2; i >= 0; i--){
14             int smallerCount = insert(root, nums[i]);
15             res.offerFirst(smallerCount);
16         }
17         
18         return res;
19     }
20     
21     private int insert(TreeNode root, int num){
22         int smallerCountSum = 0;
23         while(root.val != num){
24             if(root.val > num){
25                 root.leftCount++;
26                 if(root.left == null){
27                     root.left = new TreeNode(num);
28                 }
29                 
30                 root = root.left;
31             }else{
32                 smallerCountSum += root.leftCount + root.count;
33                 if(root.right == null){
34                     root.right = new TreeNode(num);
35                 }
36                 
37                 root = root.right;
38             }
39         }
40         
41         root.count++;
42         return smallerCountSum + root.leftCount;
43     }
44 }
45 
46 class TreeNode{
47     int val;
48     int count;
49     int leftCount;
50     TreeNode left;
51     TreeNode right;
52     
53     public TreeNode(int val){
54         this.val = val;
55         this.count = 0;
56         this.leftCount = 0;
57     }
58 }

类似Reverse Pairs.

以上是关于LeetCode Count of Smaller Numbers After Self的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 315. Count of Smaller Numbers After Self

[LeetCode][JavaScript]Count of Smaller Numbers After Self

Leetcode 315. Count of Smaller Numbers After Self

leetcode No315. Count of Smaller Numbers After Self

leetcode No315. Count of Smaller Numbers After Self

leetcode No315. Count of Smaller Numbers After Self