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 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
随机推荐
- BZOJ5100 POI2018Plan metra(构造)
容易发现要么1和n直接相连,要么两点距离即为所有dx,1+dx,n的最小值.若为前者,需要满足所有|d1-dn|都相等,挂两棵菊花即可.若为后者,将所有满足dx,1+dx,n=d1,n的挂成一条链,其 ...
- Alternate Task UVA - 11728 (暴力。。分解质因子)
题意: 输入一个正整数S,(S <= 1000)求一个最大的正整数N,使得N的所有正因子之和为S. 解析: ..求1000以内的所有数的正因子和 ...输出.. #include <io ...
- 洛谷 P2598 [ZJOI2009]狼和羊的故事 解题报告
P2598 [ZJOI2009]狼和羊的故事 题目描述 "狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......" \(Orez\)听到这首歌, ...
- docker attach 和 docker exec
docker attach docker attach -- Attach to a running container. 常用选项: --sig-proxy=true:Proxy all recei ...
- Android listview与adapter用法(BaseAdapter + getView)
Android listview与adapter用法http://www.cnblogs.com/zhengbeibei/archive/2013/05/14/3078805.html package ...
- 《剑指offer》— JavaScript(23)二叉搜索树的后序遍历序列
二叉搜索树的后序遍历序列 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 相关知识 二叉查找树(B ...
- etcd3集群管理
目录 在什么情况下需要集群的运行时更改 集群运行时更改的操作 更新一个节点 删除一个节点 增加一个节点 节点迁移和灾难恢复 迁移节点 灾难恢复 在什么情况下需要集群的运行时更改 维护和升级多个机器 如 ...
- Java基础-StringBuffer类与StringBuilder类简介
Java基础-StringBuffer类与StringBuilder类简介 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.StringBuffer类 在学习过String类之后 ...
- day12 继承
设计原则:开闭原则:对于拓展open,对于修改close. 类与类的关系:1.is a(继承关系) 2.has a(组合关系) 继承的优点:1.代码的可重用性 2.父类的属性和方法用于子类 3.子类可 ...
- Vue组件穿透
1. element ui怎么修改单独一个label的颜色 用 >>> 进行组件的穿透