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 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.
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;
}
}
Count of Smaller Number before itself的更多相关文章
- LeetCode "Count of Smaller Number After Self"
Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...
- 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 ...
- 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 ...
- LintCode "Count of Smaller Number before itself"
Warning: input could be > 10000... Solution by segment tree: struct Node { Node(), left(nullptr), ...
- 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 ...
- [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 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- [LeetCode] 315. Count of Smaller Numbers After Self (Hard)
315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- 修改pip源到国内的镜像源
国内网络原因,经常无法访问一些技术网站,pypi.python.org就是其中一个.所以,使用pip给Python安装软件时,经常出现错误.like this: File "/usr/lib ...
- 字符串使用replaceAll()方法报异常
对字符串使用replaceAll()方法替换 * ? + / | 等字符的时候会报以下异常 Dangling meta character '*' near index 0 这主要是因为这些符号在正则 ...
- FileZilla Server ftp 服务器下通过alias别名设置虚拟目录(多个分区)
最近检查服务器的时候发现磁盘空间不够用了,正好有两个硬盘正好,一个硬盘还空着,正好通过ftp服务器的别名功能实现添加空间了,这样就不用重新弄机器了 说明:FileZilla Server 的虚拟目录设 ...
- vyos User Guide
vyos User Guide 来源 https://wiki.vyos.net/wiki/User_Guide The VyOS User Guide is focused on providing ...
- 查看ubuntu版本号
No.1 cat /etc/issue No.2 cat /proc/version No.3 uname -a No.4 lsb_release -a
- 【ZJOI 2018】线图(树的枚举,hash,dp)
线图 题目描述 九条可怜是一个热爱出题的女孩子. 今天可怜想要出一道和图论相关的题.在一张无向图 $G$ 上,我们可以对它进行一些非常有趣的变换,比如说对偶,又或者说取补.这样的操作往往可以赋予一些传 ...
- BZOJ3672 [Noi2014]购票 【点分治 + 斜率优化】
题目链接 BZOJ3672 题解 如果暂时不管\(l[i]\)的限制,并假使这是一条链 设\(f[i]\)表示\(i\)节点的最优答案,我们容易得到\(dp\)方程 \[f[i] = min\{f[j ...
- Vulkan vs OpenGL ES
Vulkan 简介 Vulkan是一个免费开放的.跨平台的.底层的图形API,在一定程度上比AMD Mantle.微软DirectX 12.苹果Metal更值得开发者关注. Vulkan的最大任务不是 ...
- 图解HTTP(六)HTTP首部
一.HTTP报文的结构: 二.4种首部字段: 1. 通用首部字段 请求报文和响应报文都会使用的首部. 首部字段名 说明 Cache-Control 控制缓存行为 Connection 逐跳首部.连接的 ...
- 【题解】Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths Codeforces 741D DSU on Tree
Prelude 很好的模板题. 传送到Codeforces:(* ̄3 ̄)╭ Solution 首先要会DSU on Tree,不会的看这里:(❤ ω ❤). 众所周知DSU on Tree是可以用来处 ...