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的更多相关文章

  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 My Submissions Question

    You are given an integer array nums and you have to return a new counts array. Thecounts array has t ...

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

  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. 深入详解DataTable

    在学习DataTable知识之前,我们有必要了解下ADO.NET.以下摘自MSDN: ADO.NET 对 Microsoft SQL Server 和 XML 等数据源以及通过 OLE DB 和 XM ...

  2. VS2010中App_Code文件夹的问题

    在VS2010中新建一个Web Application,然后新建一个app_Code文件夹, 在app_code文件夹下建一个ClassHelper类. 然后在index页面中使用ClassHelpe ...

  3. Ubuntu16.10 主题flatabulous安装

    以前通过apt直接安装的flatabulous主题16.10上安装会出现找不到包的情况,可以采用源码安装 原来的apt-get方式: 1:主题 sudo add-apt-repository ppa: ...

  4. 80、Android Support v4、v7、v13的区别以及应用场景

    一.简介 在 Android 开发中,为了使用高版本API的新特性,需要添加额外的包来使用这些新特性,这就是 Android Support 包 二.分类 Android Support v4: 这个 ...

  5. dedecms 图片集上传时提示错误信息“(FILEID:1|2|3..)“的解决

    网上看到很多朋友遇到使用织梦程序一段时间后,发现上传图集时候老是失败,提示"提示FILEID:X错误,缩略图显示为红色Error"下面截图错误: 这问题今天也让我头疼了半天,好好的 ...

  6. Python 多线程

    一.线程的使用 需导入模块: from threading import Thread 二.基本使用 def fun1(arg1, v): print(arg1) print('before') t1 ...

  7. SQL Server安全概念简析

    I. 登录名与用户名 登录名: 访问数据库服务器的账户.登录名可以登录到服务器,但不能直接访问数据库内容.数据库连接串中的用户名应配置为登录名.每个登录名的定义存放在master数据库的syslogi ...

  8. 修改.gitignore后让其生效

    在使用git的时候我们有时候需要忽略一些文件或者文件夹.我们一般在仓库的根目录创建.gitignore文件在提交之前,修改.gitignore文件,添加需要忽略的文件.然后再做add commit p ...

  9. Android Studio in OSX 提高工作效率的快捷键

    前言 本篇文章参考了<倍数提高工作效率的Android Studio>一文,快捷键基于OS X系统. OS X Yosemite 10.10.5 Android Studio 1.3.1 ...

  10. QTP操作txt文档

    QTP可以在txt文件(文本文件中读取数据) 首先创造一个文档对象 set fso = createObject("scripting.filesystemobject") 然后用 ...