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. javascript基础部分

    javascript基础部分 1  数据类型: 基础数据类型(通过typeof来检测):Number,string,undefined,null,boolean,function typeof只能检测 ...

  2. R语言-数据高级管理

    数学函数 abs() 绝对值 sqrt() 平方 ceiling() 向上取整 floor() 向下取整 trunc() 截取整数部分 round(x,digits = n) 保留几位小数 统计函数 ...

  3. css不常用重要属性

    超出省略号:display:block;white-space:norwrap;overflow:hidden;text-overflow:ellipsis; white-space:norwrap/ ...

  4. 编写更好的jQuery代码

    这是一篇关于jQuery的文章,写到这里给初学者一些建议. 现在已经有很多文章讨论jQuery和JavaScript的性能问题,然而,在这篇文章中我计划总结一些提升速度的技巧和一些我自己的建议来改善你 ...

  5. System.Drawing.Color

    System.Drawing.Color color=Color.Red; System.Drawing.Color color=Color.FromArgb(,,); System.Drawing. ...

  6. highcharts去掉右下角highchart.com和右上角的图标(三个小横杆)

    去除右下角highchart.com credits: {                enabled:false            } 去除右上角图标 exporting: {         ...

  7. New Begin--工作一年所思所感小记

    有段日子没有更新Blog了,这段时间过得有些散漫,今天看完了两本书,感触颇多,让我繁杂的心绪又重归平静,想写一些什么,遂有了这篇文章. 所谓的一年是从13年7月9日我正是入职算起,到现在为止一年有余, ...

  8. 【ios 7】 之后的设置系统的状态栏隐藏的方法分享

    由于在做视频播放的的项目,一直困扰的是,视频全屏幕播放的时候,系统的状态栏会隐藏不掉,虽然可以设置为透明的状态来显示,但是电池的状态一直隐藏不掉,查看网上的说法也就是说,要么来控制,他的hidden ...

  9. [转]解决a different object with the same identifier value was already associated with the session错误

    1.a different object with the same identifier value was already associated with the session. 错误原因:在h ...

  10. 在CentOS或RHEL防火墙上开启端口

    转载自:https://linux.cn/article-4243-1.html 如果希望在服务器上提供服务,诸如CentOS或RHEL的企业级Linux发行版包含内置的强大防火墙,它们默认的防火墙规 ...