You are given an integer array nums and you have to return a new counts array. Thecounts 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].

Subscribe to see which companies asked this question

因为需要求某元素右边小于该元素的数组元素的个数,所以从数组末尾开始向前进行处理。数据结构使用树,节点有属性value,smaller,分别表示对应元素的值以及数组中小于该元素值的个数。函数insert既向树种插入新的节点,又能够求得数组右边小于value的元素的个数。因为数组末尾的元素的smaller值首先返回,所以要用到双向队列deque,将每次求得的值插入链表的头部。最终返回整个队列的值即可。

在插入操作中,请注意函数的参数root是指针的引用,因为对该指针的修改必须得到保留。如果root为空(代表数组末尾的那个元素),初始化根节点,返回0.如果根节点的值大于value,需要把这个元素对应的几点向左插入树中,同时,小于根节点的值的元素个数加1.剩余的情况下,节点需要向右插入树中,返回的值应该是比root节点小的元素个数加上树的右半部分比该元素小的元素个数,同时还要区分一下要插入的值是大于还是等于root节点的值。

很精妙的解法,真是只可意会,不可言传啊。

class Solution {
private:
class Node{
public:
int value;
int smaller;
Node *left;
Node *right;
Node(int value,int smaller) {
this ->value = value;
this ->smaller =smaller;
left=right=NULL;//在leetcode中,必须要加上这句,否则超时
}
};
int insert(Node * & root,int value){
if(root ==NULL){
root =new Node(value,);
return ;
}
if(root->value > value){
root->smaller++;
return insert(root->left,value);
}
return root->smaller+insert(root->right,value)+(root->value <value? :);
}
public:
vector<int> countSmaller(vector<int>& nums) {
Node * root=NULL;
deque <int >q;
for(int i=nums.size()-;i>-;i-- ){
int value =insert(root,nums[i]);
cout<<"this is a test! "<<value<<endl;
q.push_front(value);
}
return vector<int>(q.begin(),q.end());
}
};

315.Count of Smaller Numbers After Self My Submissions Question的更多相关文章

  1. [LeetCode] 315. Count of Smaller Numbers After Self (Hard)

    315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...

  2. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  3. leetcode 315. Count of Smaller Numbers After Self 两种思路

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  4. [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 countsarray has t ...

  5. LeetCode 315. Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  6. 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 ...

  7. 315. Count of Smaller Numbers After Self(Fenwick Tree)

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  8. 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 ...

  9. 第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树

    Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的数. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树 ...

随机推荐

  1. css初始化值

    body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,form,textarea,input,p,th,td,tr,table,tbody,thead,tfoot, ...

  2. hadoop1.2.1 MultipleOutputs将结果输出到多个文件或文件夹

    hadoop1.2.1 MultipleOutputs将结果输出到多个文件或文件夹 博客分类:http://tydldd.iteye.com/blog/2053867 hadoop   hadoop1 ...

  3. .net core 13

  4. python之路:进阶 二

        c = collections.Counter(  Counter({ b = collections.Counter(  b.update(c)   Counter({ Counter({  ...

  5. 【HighCharts系列教程】六、去除highCharts版权信息的几种方法

    方法一:单个图表去除版权 设置Credits属性为不可用,也就是credits中enable=false,具体代码如下 <script type="text/javascript&qu ...

  6. Counting Haybales

    Counting Haybales 题目描述 Farmer John is trying to hire contractors to help rearrange his farm, but so ...

  7. NVIDA 提到的 深度框架库

    BidMachBlocksCaffeChainerCNTKcuda-convnetcuda-convnet2Deeplearning4jkaldiKerasLasagneMarvinMatConvNe ...

  8. SSL 通信原理及Tomcat SSL 双向配置

    SSL 通信原理及Tomcat SSL 双向配置 目录1 参考资料 .................................................................. ...

  9. 防止多个UIAlertView重叠弹出

    http://www.jianshu.com/p/7ac398ef4532 项目中可能会遇到这种情况,好几个alertView因为逻辑关系全部弹出,用户需要一个个的点击才能将所有的alertView取 ...

  10. Spring自学教程-ssh整合(六)

    以下是本人原创,如若转载和使用请注明转载地址.本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!博客地址 感谢您支持我的博客,我的动力是您的支持和关注!如若转载和使用请注明转载地址 ...