Description
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:
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.
分析
题目的意思是:给你一个数组,然后求出数组中每个数,其右边比当前数小的个数,如果不明白的话,看一下Example就知道了。
- 这是一道leetcode hard难度的题目,暴力破解肯定会有问题。
- 首先可以使用用二分搜索法,思路是将给定数组从最后一个开始,用二分法插入到一个新的数组,这样新数组就是有序的,那么此时该数字在新数组中的坐标就是原数组中其右边所有较小数字的个数。
- 思路很奇妙,我也做不出来,学习一下。
代码
class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
vector<int> res(nums.size(),0);
vector<int> t;
for(int i=nums.size()-1;i>=0;i--){
int left=0;
int right=t.size();
while(left<right){
int mid=(left+right)/2;
if(nums[i]>t[mid]){
left=mid+1;
}else{
right=mid;
}
}
res[i]=right;
t.insert(t.begin()+right,nums[i]);
}
return res;
}
};