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