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
 Approach #2: Java. [BST]
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)的更多相关文章

  1. [LeetCode] 315. Count of Smaller Numbers After Self (Hard)

    315. Count of Smaller Numbers After Self class Solution { public: vector<int> countSmaller(vec ...

  2. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  3. leetcode 315. Count of Smaller Numbers After Self 两种思路

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  4. [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 ...

  5. LeetCode 315. Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树

    Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的数. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树 ...

随机推荐

  1. Objective-c中的delegate浅析

    delegate初探 在ios开发中,我们常常会用到类似例如以下的对话框: 因此,例如以下这段代码我们也就非常熟悉了: - (IBAction)showSheet:(id)sender { UIAct ...

  2. SAP流水号

    [转]编号范围对象维护 Tcode: SNRO OYSM   1.Number Range的通用Tcode:SNRO   2.Number Range的通用读取函数:NUMBER_GET_NEXT 3 ...

  3. 浅尝NODE.js

    Node.js是Google公司开发的,安装好必要的环境以后,可以在服务端上跑的js,可以接收和回应http请求,所有方法都支持异步回调,大大提高事务执行效率. 学习地址:http://www.run ...

  4. [IR课程笔记]Query Refinement and Relevance Feedback

    相关反馈的两种类型: “真实”的相关反馈: 1. 系统返回结果 2. 用户提供一些反馈 3. 系统根据这些反馈,返回一些不同的,更好的结果 “假定”的相关反馈 1. 系统得到结果但是并不返回结果 2. ...

  5. 3D 图片播放焦点图插件Adaptor

    在线演示 本地下载

  6. <JAVA图像学习笔记>关于Graphics/Graphics2D以及简单的几何图像制作(一个简单钟表的实现)

    题外话:正好赶上OperatingSystem的作业要做一个模拟线程/进程调度的问题,决定用JAVA实现才发现这些内容和之前学过的GUI制作是两码事儿- -b 通过学习java.swing库的Acti ...

  7. 9.1 NOIP普及组试题精解(1)

    9-1 series1.c #include <stdio.h> int main() { float s=0.0,k; int n; printf("输入数字k(1~15):& ...

  8. matlab之sortrows()函数

    sortrows()函数的格式: sortrows(A,column) A是一个矩阵,如果没有第二个参数column,则默认按照第一列升序排列,如果遇到重复数字,则按照第二列升序排列,依次类推... ...

  9. JS工具函数汇总

    备注:http://phpjs.org/  这个站点把PHP常用的方法用js实现了,推荐一下 1.从数组中随机获取几个不重复项 //从一个给定的数组arr中,随机返回num个不重复项 function ...

  10. JS遍历ChexkBoxList

    var cboxs = $("#cblAuth input[type=checkbox]"); ; ; i < cboxs.length; i++) { if (cboxs[ ...