315. Count of Smaller Numbers After Self(Fenwick Tree)
You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i]
is the number of smaller elements to the right of nums[i]
.
Example:
Input: [5,2,6,1]
Output:[5,2,6,1]Explanation:
To the right of 5 there are 2 smaller elements (2 and 1).
To the right of 2 there is only 1 smaller element (1).
To the right of 6 there is 1 smaller element (1).
To the right of 1 there is 0 smaller element.
Approach #1: C++. [Fenwick Tree / Binary Indexed Tree]
class FenwickTree {
public:
FenwickTree(int n): sums_(n+1, 0) {} void update(int i, int delta) {
while (i < sums_.size()) {
sums_[i] += delta;
i += lowbit(i);
}
} int query(int i) const {
int sum = 0;
while (i > 0) {
sum += sums_[i];
i -= lowbit(i);
}
return sum;
}
private:
static inline int lowbit(int x) { return x & (-x); }
vector<int> sums_;
}; class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
set<int> sorted(nums.begin(), nums.end());
unordered_map<int, int> ranks; int rank = 0; for (const int num : sorted)
ranks[num] = ++rank; vector<int> ans;
FenwickTree tree(ranks.size()); for (int i = nums.size() - 1; i >= 0; --i) {
ans.push_back(tree.query(ranks[nums[i]] - 1));
tree.update(ranks[nums[i]], 1);
} std::reverse(ans.begin(), ans.end());
return ans;
}
};
Prefix sums of frequencise, convert the number to its rank as in sorted array.
For example:
Input: [5, 2, 6, 1]
sorted: [1, 2, 5, 6]
ranks: [1, 4, 2, 3]
Increase the freq[rank] by 1.
Num | Rank | Freq | Prefix sum / Query(rank - 1) |
- | - | [0, 0, 0, 0, 0] | - |
1 | 1 | [0, 1, 0, 0, 0] | 0 |
6 | 4 | [0, 1, 0, 0, 1] | 1 |
2 | 2 | [0, 1, 1, 0, 1] | 1 |
5 | 3 | [0, 1, 1, 1, 1] | 2 |
class Solution { class Node {
Node left, right;
int val, sum, dup = 1;
public Node(int v, int s) {
val = v;
sum = s;
}
} public List<Integer> countSmaller(int[] nums) {
Integer[] ans = new Integer[nums.length];
Node root = null;
for (int i = nums.length - 1; i >= 0; i--) {
root = insert(nums[i], root, ans, i, 0);
}
return Arrays.asList(ans);
} private Node insert(int num, Node node, Integer[] ans, int i, int preSum) {
if (node == null) {
node = new Node(num, 0);
ans[i] = preSum;
} else if (node.val == num) {
node.dup++;
ans[i] = preSum + node.sum;
} else if (node.val > num) {
node.sum++;
node.left = insert(num, node.left, ans, i, preSum);
} else {
node.right = insert(num, node.right, ans, i, preSum + node.dup + node.sum);
}
return node;
}
}
315. Count of Smaller Numbers After Self(Fenwick Tree)的更多相关文章
- [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 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- [LeetCode] 315. Count of Smaller Numbers After Self 计算后面较小数字的个数
You are given an integer array nums and you have to return a new counts array. The countsarray has t ...
- LeetCode 315. Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
- 315. Count of Smaller Numbers After Self
You are given an integer array nums and you have to return a new counts array. The counts array has ...
- 315.Count of Smaller Numbers After Self My Submissions Question
You are given an integer array nums and you have to return a new counts array. Thecounts array has t ...
- 315. Count of Smaller Numbers After Self(二分或者算法导论中的归并求逆序数对)
You are given an integer array nums and you have to return a new counts array. The counts array has ...
- 第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树
Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的数. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树 ...
随机推荐
- 如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?
目录: 一.概述 最近在做一个新闻类结合社区的APP的时候,需要添加一个侧滑菜单的效果,考虑到可以使用DrawerLayout布局,但是问题是使用了 DrawerLayout布局后,主页内容应该是一个 ...
- vue应用示例
1 . 实现轮播图 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&q ...
- 从动态库的def文件生成lib文件
以sqlite3为例,下载的文件中只有def文件,没有lib文件,想使用静态方式调用dll的情况下,就需要额外的.h文件和.lib文件存在. .h文件可以从官方下载的sqlite-amalgamati ...
- 20145239杜文超 《Java程序设计》第7周学习总结
20145239 <Java程序设计>第7周学习总结 教材学习内容总结 Lambda 认识Lambda语法 Lambda语法概述: Arrays的sort()方法可以用来排序,在使用sor ...
- 高通MSM8255 GPS 调试分析&&Android系统之Broadcom GPS 移植【转】
本文转载自:http://blog.csdn.net/gabbzang/article/details/12063031 http://blog.csdn.NET/dwyane_zhang/artic ...
- hihocoder(第十周)二叉树(前序中序推后续)递推实现
题目 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在参与过了美食节之后,小Hi和小Ho在别的地方又玩耍了一阵子,在这个过程中,小Ho得到了一个非常有意思 ...
- win8+sdk8+vs2012+freeglut+glew开发opengl
写给想要学习opengl的同学们. 刚开始学习opengl的时候,对于整个环境的搭建以及一些概念不太清晰,网上的资料又比较凌乱,因此在此总结一下,方便大家. 首先,是有一个windows系统,我用的是 ...
- PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)
利用Requests+正则表达式爬取猫眼电影top100 目标站点分析 流程框架 爬虫实战 使用requests库获取top100首页: import requests def get_one_pag ...
- 分享知识-快乐自己:java 中的访问修饰符
1):Java中的访问修饰符: Java面向对象的基本思想之一是封装细节并且公开接口.Java语言采用访问控制修饰符来控制类及类的方法和变量的访问权限,从而向使用者暴露接口,但隐藏实现细节. 访问控制 ...
- 《c# 实现p2p文件分享与传输系统》 二、 设计 - 续(NAT穿透)
c#实现P2P文件分享与传输系统 二.设计 - 续(NAT穿透) 首先要抱歉,因为这些日子较忙,没有写文章,这个系列拖了很久,现在开始继续. 上一篇文章介绍了p2p系统Tracker Server和 ...