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 the property where counts[i]
is the number of smaller elements to the right of nums[i]
.
Example:
Given nums = [5, 2, 6, 1] 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.
Return the array [2, 1, 1, 0]
.
public class Solution {
public List<Integer> countSmaller(int[] nums) {
// 求得nums在按序排列的数组中的脚标位置
int[] temp = nums.clone();
Arrays.sort(temp);
for (int i = 0; i < temp.length; i++) {
nums[i] = Arrays.binarySearch(temp, nums[i]);
}
// 这里用Integer是为了使用Arrays.asList
Integer[] ans = new Integer[temp.length];
int[] bit = new int[temp.length];
// 遍历的时候使用逆序是因为位于数组最后面的数,逆序程度是最低的
for (int i = temp.length-1; i >= 0; i--) {
/**
* 用bit数组的前num[i]项和作为逆序程度
*
* 最后一位的逆序永远是0
* 次高位的逆序要么是1要么是0,最大值只能是1
* query方法正好保证了这点。
* 它查询bit数组中前nums[i]项的和
* 如果最高位比次高位要小,那么计算次高位项的和就会加1,相反就不回家
*/
ans[i] = query(bit,nums[i]);
/**
* 修改那条链上的数据+1
*/
add(bit,nums[i]+1,1);
} return Arrays.asList(ans);
} /**
* 功能:
* 修改bit数组脚标i对应的链上的数据
* (因为求前n项和,是根据那几个数来求的,所以改动一个数要同时改动那几个数)
* @param bit 树状数组
* @param i 数组脚标
* @param val 脚标所对应的树枝要修改的值
*/
private void add(int[] bit, int i, int val) {
for (;i<bit.length;i+=lowbit(i)) {
bit[i]+=val;
}
} /**
* 查询bit数组中前i项的和
* @param bit 树状数组
* @param i 数组脚标
* @return
*/
private Integer query(int[] bit, int i) {
int ans = 0;
for (;i>0;i-=lowbit(i)) {
ans += bit[i];
}
return ans;
}
/**
* 求二进制的i中第一个1对应的十进制值
* @param i
* @return i转化为二进制之后第一个1所对应的值
*/
private int lowbit(int i) {
return i&(-i);
}
}
简单的解释一下ans数组的产生:
只要后面一位比前面一位要小,那么前面一位求前n项和的时候就会多加一个1.所以加了多少个1,逆序数就有几个。
前n项和的值中每一个1代表了一个逆序数。
分别对:896859 896853的分析
树状数组图
315. Count of Smaller Numbers After Self的更多相关文章
- [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 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(Fenwick Tree)
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(二分或者算法导论中的归并求逆序数对)
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) 我们可以直接套用主席树的模板. 主席树 ...
随机推荐
- 使用Junit对Spring进行单元测试实战小结
Demo代码: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:/ ...
- Java生成XML文件
我们在数据库中的数据可以将其提取出来生成XML文件,方便传输.例如数据库中有Admin这张表: 我们写一个java类表示admin数据: package xmlDom.vo; import java. ...
- JAVA下载文件中文乱码问题
http://blog.itpub.net/92037/viewspace-788900/ 最后的中文乱码没有解决 现在我在系统中用到了两个组件,smartupload,一个支持中文,一个不支持.但是 ...
- Codeforces 730I [费用流]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给两行n个数,要求从第一行选取a个数,第二行选取b个数使得这些数加起来和最大. 限制条件是第一行选取了某个数的条件下,第二行不能选取对应位置的数. ...
- U-Boot GOT表分析和u-boot.lds解读
转自:http://blog.sina.com.cn/s/blog_70dd16910100zab6.html u-boot-2010.09/arch/powerpc/cpu/mpc86xx/star ...
- 简约的ASP.NET 系统框架,提供源码。
好东西,好东西就要分享. 其实谈不上什么好东西,但很实用,应用起来也很简单,我自己用它构建了多套ASP .NET系统,原理也很简单: 1.用XML文件编辑系统的菜单和Toolbar: 2.读取XML节 ...
- C语言获得文件一行
C语言获得一行的数据还是比较麻烦的,这里讲一下几种曾经用过的方法. 第一种,是最笨的方法,就是一个一个字符的读取,也是最容易想到的方法.具体实现如下:void read_line(char l ...
- How to Take Control of Your Line Height in Outlook.com
Reference to: http://www.emailonacid.com/blog/details/C13/line_height_and_outlook.com
- @override的意思
@Override是Java5的元数据,自动加上去的一个标志,告诉你说下面这个方法是从父类/接口 继承过来的,需要你重写一次,这样就可以方便你阅读,也不怕会忘记 @Override是伪代码,表示重写( ...
- Media Queries详解
Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码: <link href="css/reset.css" rel ...