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.

 public class Solution {
/**
* @param A: An integer array
* @return: Count the number of element before this element 'ai' is
* smaller than it and return count number array
*/
SegmentTreeNode root; class SegmentTreeNode {
// here, start and end refer to the actual number
public int start, end;
public int count;
public SegmentTreeNode left, right;
public SegmentTreeNode(int start, int end, int count) {
this.start = start;
this.end = end;
this.count = count;
this.left = this.right = null;
}
}
// here, start and end refer to the actual number
public SegmentTreeNode build(int start, int end) {
if(start > end) return null; SegmentTreeNode root = new SegmentTreeNode(start, end, ); if(start != end) {
int mid = (start + end) / ;
root.left = build(start, mid);
root.right = build(mid + , end);
}
return root;
}
public int querySegmentTree(SegmentTreeNode root, int start, int end) {
if(start == root.start && root.end == end) {
return root.count;
}
int mid = (root.start + root.end) / ; if (start > mid) {
return querySegmentTree(root.right, start, end);
} else if (end <= mid) {
return querySegmentTree(root.left, start, end);
} else {
return querySegmentTree(root.left, start, mid) + querySegmentTree(root.right, mid + , end);
}
}
public void modifySegmentTree(SegmentTreeNode root, int index, int value) {
if(root.start == index && root.end == index) {
root.count += value;
return;
}
if (index < root.start || index > root.end) return; // 查询
int mid = (root.start + root.end) / ;
if(index <= mid) {
modifySegmentTree(root.left, index, value);
} else {
modifySegmentTree(root.right, index, value);
}
//更新
root.count = root.left.count + root.right.count;
}
public ArrayList<Integer> countOfSmallerNumberII(int[] A) {
// write your code here
root = build(, );
ArrayList<Integer> ans = new ArrayList<Integer>();
for(int i = ; i < A.length; i++) {
int res = ;
if (A[i] > ) { // if A[i] == 0, we don't need to query
res = querySegmentTree(root, , A[i]-);
}
modifySegmentTree(root, A[i], );
ans.add(res);
}
return ans;
}
}

最新文章

  1. C# HttpWebReqeust和HttpWebResponse发送请求
  2. Java面向对象编程 第一章 面向对象开发方法概述
  3. Vim,极简使用教程,让你瞬间脱离键鼠切换的痛苦
  4. Oracle补习班第七天
  5. angularjs 分页精华代码
  6. 将存储在本地的大量分散的小文件,合并并保存在hdfs文件系统中
  7. Oracle安装后,服务中没有监听器怎么处理?
  8. Python工程文件中的名词解释---Module与Package的区别
  9. 什么是CGI(Common Gateway Interface)?
  10. OpenCV矩阵运算
  11. ZT 将sublime text的tab改为四个空格
  12. 使用LTP套件对Linux系统进行压力测试
  13. 20180322 对DataTable里面的数据进行去重
  14. activiti工作流
  15. mysql 基本语法学习1(数据库、数据表、数据列的操作)
  16. 在主Android Activity中加载Fragment的一般简易方法 ,来模拟一个微信界面。
  17. 在TextBrowser显示中,如何让最新的数据永远出现在第一行或者是在窗口的最后显示信息
  18. caffe官网的部分翻译及NG的教程
  19. linux 安装php7
  20. Python3 模块、包调用&amp;路径

热门文章

  1. PGM学习之四 Factor,Reasoning
  2. python爬虫headers设置后无效解决方案
  3. python2 python3共存解决方案
  4. 洛谷3732:[HAOI2017]供给侧改革——题解
  5. HDU--4607
  6. Qt 模型/视图/委托
  7. python基础3--字符串
  8. linux命令总结dd命令详解
  9. Hadoop生态圈-使用MapReduce处理HBase数据
  10. HTML常用标签-&lt;body&gt;内基本标签(块级标签和内联标签)