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 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- [BZOJ4044]Virus synthesis 回文自动机的DP
4044: [Cerc2014] Virus synthesis Time Limit: 20 Sec Memory Limit: 128 MB Description Viruses are us ...
- [提升性选讲] 树形DP进阶:一类非线性的树形DP问题(例题 BZOJ4403 BZOJ3167)
转载请注明原文地址:http://www.cnblogs.com/LadyLex/p/7337179.html 树形DP是一种在树上进行的DP相对比较难的DP题型.由于状态的定义多种多样,因此解法也五 ...
- Udp广播的发送与接收(C#+UdpClient) 上篇
简介: Udp广播消息用在局域网的消息传递很方便.本文使用UdpClient类在WPF下实现Udp广播收发 发送: void MainWindow_Loaded(object sender, Rout ...
- BZOJ 3210: 花神的浇花集会
3210: 花神的浇花集会 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 577 Solved: 299[Submit][Status][Discus ...
- AtCoder Grand Contest 003
AtCoder Grand Contest 003 A - Wanna go back home 翻译 告诉你一个人每天向哪个方向走,你可以自定义他每天走的距离,问它能否在最后一天结束之后回到起点. ...
- 【poj2411】 Mondriaan's Dream
http://poj.org/problem?id=2411 (题目链接) 题意 一个$n*m$的网格,用$1*2$的方块填满有多少种方案. Solution 轮廓线dp板子.按格dp,对上方和左方的 ...
- 面向对象高级编程(2)-使用@property
使用@property 在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合逻 ...
- Redis中国用户组|唯品会Redis cluster大规模生产实践
嘉宾:陈群 很高兴有机会在Redis中国用户组给大家分享redis cluster的生产实践.目前在唯品会主要负责redis/hbase的运维和开发支持工作,也参与工具开发工作 Outline 一.生 ...
- shell 中的操作符
1.算术操作符 2.关系操作符 3.布尔操作符 4.字符串操作符 5.文件相关操作符 算术操作符 bash shell 没有提供任何机制来执行简单的算术运算,不过我们可以借助于一些其他程序,如 exp ...
- 题解【bzoj4587 & bzoj4408 [FJOI2016]神秘数】
Description \(n\) 个数的序列,每次询问一个区间,求最小的一个数使得不能用这个区间中的数之和表示. \(n \leq 10^5, \sum a_i \leq 10^9\) 这两个题一个 ...