Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 10000) . For each element Ai in the array, count the number of element before this elementAi is smaller than it and return count number array.

Example

For array [1,2,7,8,5], return [0,1,2,3,2]

Analysis:

Create a segement tree in which start, end, and count refer to the total numbers from start to end. And at the beginning, we set the count to 0. However, every time when we query from the tree, say, we query 10, we first call query(root, 0, 9), and then update the count of 10 in the tree.

  1. public class Solution {
  2. /**
  3. * @param A: An integer array
  4. * @return: Count the number of element before this element 'ai' is
  5. * smaller than it and return count number array
  6. */
  7. SegmentTreeNode root;
  8.  
  9. class SegmentTreeNode {
  10. // here, start and end refer to the actual number
  11. public int start, end;
  12. public int count;
  13. public SegmentTreeNode left, right;
  14. public SegmentTreeNode(int start, int end, int count) {
  15. this.start = start;
  16. this.end = end;
  17. this.count = count;
  18. this.left = this.right = null;
  19. }
  20. }
  21. // here, start and end refer to the actual number
  22. public SegmentTreeNode build(int start, int end) {
  23. if(start > end) return null;
  24.  
  25. SegmentTreeNode root = new SegmentTreeNode(start, end, );
  26.  
  27. if(start != end) {
  28. int mid = (start + end) / ;
  29. root.left = build(start, mid);
  30. root.right = build(mid + , end);
  31. }
  32. return root;
  33. }
  34. public int querySegmentTree(SegmentTreeNode root, int start, int end) {
  35. if(start == root.start && root.end == end) {
  36. return root.count;
  37. }
  38. int mid = (root.start + root.end) / ;
  39.  
  40. if (start > mid) {
  41. return querySegmentTree(root.right, start, end);
  42. } else if (end <= mid) {
  43. return querySegmentTree(root.left, start, end);
  44. } else {
  45. return querySegmentTree(root.left, start, mid) + querySegmentTree(root.right, mid + , end);
  46. }
  47. }
  48. public void modifySegmentTree(SegmentTreeNode root, int index, int value) {
  49. if(root.start == index && root.end == index) {
  50. root.count += value;
  51. return;
  52. }
  53. if (index < root.start || index > root.end) return;
  54.  
  55. // 查询
  56. int mid = (root.start + root.end) / ;
  57. if(index <= mid) {
  58. modifySegmentTree(root.left, index, value);
  59. } else {
  60. modifySegmentTree(root.right, index, value);
  61. }
  62. //更新
  63. root.count = root.left.count + root.right.count;
  64. }
  65. public ArrayList<Integer> countOfSmallerNumberII(int[] A) {
  66. // write your code here
  67. root = build(, );
  68. ArrayList<Integer> ans = new ArrayList<Integer>();
  69. for(int i = ; i < A.length; i++) {
  70. int res = ;
  71. if (A[i] > ) { // if A[i] == 0, we don't need to query
  72. res = querySegmentTree(root, , A[i]-);
  73. }
  74. modifySegmentTree(root, A[i], );
  75. ans.add(res);
  76. }
  77. return ans;
  78. }
  79. }

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

  1. LeetCode "Count of Smaller Number After Self"

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

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

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

  4. LintCode "Count of Smaller Number before itself"

    Warning: input could be > 10000... Solution by segment tree: struct Node { Node(), left(nullptr), ...

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

  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. 【Java并发编程】之一:可重入内置锁

    每个Java对象都可以用做一个实现同步的锁,这些锁被称为内置锁或监视器锁.线程在进入同步代码块之前会自动获取锁,并且在退出同步代码块时会自动释放锁.获得内置锁的唯一途径就是进入由这个锁保护的同步代码块 ...

  2. P3223 [HNOI2012]排队

    题目描述 某中学有 n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人都是不同的) 输入输 ...

  3. Digits of Factorial LightOJ - 1045(数学题?)

    原文地址: https://blog.csdn.net/fenghoumilin/article/details/52293910 题意:求 n 的阶乘在 base 进制下的位数,这里有一个简单的方法 ...

  4. Oracle 修改dmp的表空间

    1.百度下载  UltraEdit 并安装 2.打开程序,文件-->打开-->找到dmp  文件太大会提示,选择第一个默认,确定 3.按CTRL+H 转成十六进制编辑 4.例如:dmp里面 ...

  5. MT【134】待定系数

    已知\(a,b>0\)且\(ab(a+b)=4\),求\(2a+b\)的最小值______. 解答:\(\sqrt{3}(2a+b)\ge(\sqrt{3}+1)a+b+(\sqrt{3}-1) ...

  6. 《Linux内核设计与实现》第18章读书笔记

    第十八章 调试 一.调试开始前的准备 1.准备开始 bug 藏匿bug的版本 相关内核代码的知识 成功调试的关键在于能否将错误重现 2.内核中的bug 其产生原因无数,表象变化也多种多样.从隐藏在源代 ...

  7. linux内核设计与实现一书阅读整理 之第五章

    CHAPTER 5 系统调用 5.1 与内核通信 系统调用在用户空间进程和硬件设备之间添加了一个中间层,该层主要作用有三个: 为用户空间提供了一种硬件的抽象接口 系统调用保证了系统的稳定和安全 每个进 ...

  8. 项目管理---git----遇到问题------.gitignore不起作用

    情况 在管理一个版本库时,有时候不想要管理某些文件,这个时候我就把这个问价写到.gitignore文件中,这样应该就可以将这个文件忽略,不再进行·版本管理了,但是经常出现的情况是:将这些文件名写到其中 ...

  9. 解题:POI 2006 PRO-Professor Szu

    题面 这个题是比较套路的做法啦,建反图后缩点+拓扑排序嘛,对于所有处在$size>=2$的SCC中的点都是无限解(可以一直绕) 然后注意统计的时候的小细节,因为无限解/大解也要输出,所以我们把这 ...

  10. cpplint

    Cpplint是一个Python脚本,作为一款开源免费的代码静态检测工具,Google也使用它作为自己的C++代码检测工具,也就是说,只要你的代码遵从Google C++代码规范,那么Cpplint将 ...