You are given an integer array nums and you have to return a new counts array. The countsarray has the property where counts[i] is the number of smaller elements to the right of nums[i].

Example:

Input: [5,2,6,1]
Output: [2,1,1,0]
Explanation:

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.

这道题给定了一个数组,让我们计算每个数字右边所有小于这个数字的个数,目测不能用 brute force,OJ 肯定不答应,那么为了提高运算效率,首先可以使用用二分搜索法,思路是将给定数组从最后一个开始,用二分法插入到一个新的数组,这样新数组就是有序的,那么此时该数字在新数组中的坐标就是原数组中其右边所有较小数字的个数,参见代码如下:

解法一:

// Binary Search
class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
vector<int> t, res(nums.size());
for (int i = nums.size() - ; i >= ; --i) {
int left = , right = t.size();
while (left < right) {
int mid = left + (right - left) / ;
if (t[mid] >= nums[i]) right = mid;
else left = mid + ;
}
res[i] = right;
t.insert(t.begin() + right, nums[i]);
}
return res;
}
};

上面使用二分搜索法是一种插入排序的做法,我们还可以用 C++ 中的 STL 的一些自带的函数,比如求距离 distance,或是求第一个不小于当前数字的函数 lower_bound(),这里利用这两个函数代替了上一种方法中的二分搜索的部分,两种方法的核心思想都是相同的,构造有序数组,找出新加进来的数组在有序数组中对应的位置存入结果中即可,参见代码如下:

解法二:

// Insert Sort
class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
vector<int> t, res(nums.size());
for (int i = nums.size() - ; i >= ; --i) {
int d = distance(t.begin(), lower_bound(t.begin(), t.end(), nums[i]));
res[i] = d;
t.insert(t.begin() + d, nums[i]);
}
return res;
}
};
再来看一种利用二分搜索树来解的方法,构造一棵二分搜索树,稍有不同的地方是需要加一个变量 smaller 来记录比当前结点值小的所有结点的个数,每插入一个结点,会判断其和根结点的大小,如果新的结点值小于根结点值,则其会插入到左子树中,此时要增加根结点的 smaller,并继续递归调用左子结点的 insert。如果结点值大于根结点值,则需要递归调用右子结点的 insert 并加上根结点的 smaller,并加1,参见代码如下:
解法三:
// Binary Search Tree
class Solution {
public:
struct Node {
int val, smaller;
Node *left, *right;
Node(int v, int s) : val(v), smaller(s), left(NULL), right(NULL) {}
};
int insert(Node*& root, int val) {
if (!root) return (root = new Node(val, )), ;
if (root->val > val) return root->smaller++, insert(root->left, val);
return insert(root->right, val) + root->smaller + (root->val < val ? : );
}
vector<int> countSmaller(vector<int>& nums) {
vector<int> res(nums.size());
Node *root = NULL;
for (int i = nums.size() - ; i >= ; --i) {
res[i] = insert(root, nums[i]);
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/315

类似题目:

Count of Range Sum

Queue Reconstruction by Height

Reverse Pairs

参考资料:

https://leetcode.com/problems/count-of-smaller-numbers-after-self/

https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76576/My-simple-AC-Java-Binary-Search-code

https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/138154/The-C%2B%2B-merge-sort-template-for-pairs-'i'-'j'-problem

https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76611/Short-Java-Binary-Index-Tree-BEAT-97.33-With-Detailed-Explanation

https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76657/3-ways-(Segment-Tree-Binary-Indexed-Tree-Binary-Search-Tree)-clean-python-code

https://leetcode.com/problems/count-of-smaller-numbers-after-self/discuss/76607/C%2B%2B-O(nlogn)-Time-O(n)-Space-MergeSort-Solution-with-Detail-Explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

最新文章

  1. 集成TBS(腾讯浏览服务)x5内核的webView
  2. Luogu题目集合[6/未完待续]
  3. c_test
  4. 2016HUAS_ACM暑假集训2D - 敌兵布阵
  5. C# 延迟处理类 Lazy
  6. ***Redis hash是一个string类型的field和value的映射表.它的添加、删除操作都是O(1)(平均)。hash特别适合用于存储对象
  7. 03_线程的创建和启动_实现Runnable接口方式
  8. hdu1860
  9. EasyUI - 操作 Tree 控件
  10. 1988: Sn 爆long long 的处理方法
  11. Java中Properties类
  12. C#设计模式(0)-认识设计模式
  13. PCL 1.60 +windows+vs2010 安装与配置
  14. python监控微信报警
  15. firemonkey ListView DynamicAppearance
  16. 安卓自定义View实现图片上传进度显示(仿QQ)
  17. 数据分析---《Python for Data Analysis》学习笔记【01】
  18. 洛谷P5289 皮配
  19. cocos中FPS数值的含义
  20. [转]DB2中需要REORG操作的几种情况

热门文章

  1. Effective前端4:尽可能地使用伪元素
  2. 让我们再为C#异步编程Async正名
  3. [python] CSV read and write using module xlrd and xlwt
  4. Java 单例模式详解
  5. C++ STL简述
  6. ASP.NET MVC搭建项目后台UI框架—1、后台主框架
  7. 在SharePoint 2013 之中使用JS从Add-in程序中读取用户配置文件的属性
  8. RadioButton与CheckBox
  9. iOS App 不支持http协议 App Transport Security has blocked a cleartext HTTP (http://)
  10. Spring 4 创建REST API