Warning: input could be > 10000...

Solution by segment tree:

  1. struct Node
  2. {
  3. Node(int s, int e) : start(s), end(e), cnt(), left(nullptr), right(nullptr)
  4. {};
  5. int start;
  6. int end;
  7. int cnt;
  8. //
  9. Node *left;
  10. Node *right;
  11. };
  12. class Solution {
  13. Node *pRoot;
  14.  
  15. void update(int v)
  16. {
  17. Node *p = pRoot;
  18.  
  19. while(p)
  20. {
  21. p->cnt ++;
  22. if(p->start == p->end) break;
  23. int mid = (p->start + p->end) / ;
  24. if(v <= mid)
  25. {
  26. if(!p->left)
  27. {
  28. p->left = new Node(p->start, mid);
  29. }
  30. p = p->left;
  31. }
  32. else
  33. {
  34. if(!p->right)
  35. {
  36. p->right = new Node(mid + , p->end);
  37. }
  38. p = p->right;
  39. }
  40. }
  41. }
  42. int query(Node *p, int v)
  43. {
  44. if(!p) return ;
  45.  
  46. int mid = (p->start + p->end) / ;
  47. if(v < mid)
  48. {
  49. return query(p->left, v);
  50. }
  51. else if(v == mid)
  52. {
  53. return p->left ? p->left->cnt : ;
  54. }
  55. // v > mid
  56. return (p->left ? p->left->cnt : ) + query(p->right, v);
  57. }
  58. public:
  59. /**
  60. * @param A: An integer array
  61. * @return: Count the number of element before this element 'ai' is
  62. * smaller than it and return count number array
  63. */
  64. vector<int> countOfSmallerNumberII(vector<int> &A) {
  65.  
  66. pRoot = new Node(, );
  67.  
  68. vector<int> ret;
  69. for(auto v : A)
  70. {
  71. ret.push_back(query(pRoot, v - ));
  72. update(v);
  73. }
  74. return ret;
  75. }
  76. };

LintCode "Count of Smaller Number before itself"的更多相关文章

  1. Lintcode: Count of Smaller Number

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  2. LeetCode "Count of Smaller Number After Self"

    Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...

  3. Lintcode249 Count of Smaller Number before itself solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...

  4. Lintcode248 Count of Smaller Number solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, value from ...

  5. Count of Smaller Number before itself

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  6. [Swift]LeetCode315. 计算右侧小于当前元素的个数 | Count of Smaller Numbers After Self

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

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

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

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

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

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

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

随机推荐

  1. dedecms 按照栏目指定的id排序

    方法: 1.打开include/taglib/channelartlist.lib.php,找到大约78行,把 代码如下(一定要注意表名一致): $dsql->SetQuery("SE ...

  2. css中常用的hack

    <!DOCTYPE html> <html> <head> <title>Css Hack</title> <style> #t ...

  3. selenium执行js报错

    selenium执行js报错 Traceback (most recent call last):    dr.execute_script(js)  File "C:\Python27\l ...

  4. Optimizing shaper — hashing filters (HTB)

    I have a very nice shaper in my linux box :-) How the configurator works — it’s another question, he ...

  5. Linux磁盘文件的命名

    磁盘的常用接口有两种:IDE和SATA接口,目前主流的是SATA接口. IDE接口由IDE扁平电缆线连接,一个电缆可连接两个IDE接口,通常主机又都会提供两个IDE接口,因此最多可以接到四个IDE设备 ...

  6. CSS table-layout 、border-collapse属性

    ( table-layout)设置表格布局算法: 可能的值 值 描述 automatic 默认.列宽度由单元格内容设定. fixed 列宽由表格宽度和列宽度设定. inherit 规定应该从父元素继承 ...

  7. 快速对字符转义,避免跨站攻击XSS

    XSS已经成为非常流行的网站攻击方式,为了安全起见,尽量避免用户的输入.可是有些情况下不仅不避免,反而要求鼓励输入,比如写博客.博客园开放性很高,可以运行手写的JS.之前比较著名的例子就是,凡是看到某 ...

  8. C函数及指针学习1

    1 大段程序注释的方法 #if 0#endif 2三字母词 以两个问号 开始的都要注意 3 字面值(常量) 在整型号字面值后加 字符L (long),U(unsigned)说明字符常量 为长整型 或( ...

  9. Codeforces Round #144 (Div. 2)

    A. Perfect Permutation 奇偶对调. B. Non-square Equation \(s(x)\)不超过200,根据求根公式计算\(x\). C. Cycles 每次新增点时都和 ...

  10. Compiler ,Interpreter, Linker

    https://en.wikipedia.org/wiki/Interpreter_(computing) https://en.wikipedia.org/wiki/Compiler https:/ ...