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 1:

Input: nums = [5,2,6,1]
Output: [2,1,1,0]
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.

Constraints:

  • 0 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
 
class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
vector<int> res(nums.size(),0);
//从右向左,将数组有序插入tmp.利用二分查找确定当前数右边比它小的数的个数
vector<int> tmp;
for(int i=nums.size()-1;i>=0;i--){
int left = 0,right=tmp.size()-1;
//找第一个大于等于当前数的位置。插入其中
while(left <= right){
int mid = left+(right-left)/2;
if(tmp[mid] < nums[i]) left = mid+1;
else right = mid-1;
}
//最后返回的位置是left
res[i]=left;
//插入nums[i]
tmp.insert(tmp.begin()+left,nums[i]);
}
return res;
}
};

//归并:先引入逆序数;不同于逆序数对:

res[nums[i].second] += (j-mid-1);
这个里面坑比较多
class Solution {
public:
//法二:利用归并排序求逆序对数的方法
//https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/submissions/ vector<int> countSmaller(vector<int>& nums) {
int n=nums.size();
vector<int> res(n,0);
if(n==0 || n==1) return res;
vector<pair<int,int>> tmp(n,pair<int,int>{0,0});
vector<pair<int,int>> idx;
for(int i=0;i<n;i++){
idx.push_back(make_pair(nums[i],i));
}
mergesort(idx,tmp,0,n-1,res);
return res;
} //merge的过程将left到right有序重新存入nums.归并nums[left,mid],nums[mid+1,right]
void merge(vector<pair<int,int>>& nums,vector<pair<int,int>>& tmp,int left,int mid,int right,vector<int>& res) {
int i=left,j=mid+1,k=left;
for(;i<=mid&&j<=right;){
if(nums[i].first<=nums[j].first){
//不同于算整个数组逆序数
//这里的i不是之前的i。归并后数字的位置被改变了.所以利用pari记录nums[i]原始位置
//res[i] += (j-mid-1);
res[nums[i].second] += (j-mid-1);
tmp[k++] = nums[i++];
}else{
tmp[k++] = nums[j++];
}
}
//还有未归并完成的
while(i<=mid){
//先计算res
res[nums[i].second] += (j-mid-1);
tmp[k++]=nums[i++];
}
while(j<=right){
tmp[k++]=nums[j++];
}
//将tmp重新放入nums,那么nums[left,right]即有序了
for(int i=left;i<=right;i++){
nums[i] = tmp[i];
}
return;
}
//归并排序
void mergesort(vector<pair<int,int>>& nums,vector<pair<int,int>>& tmp,int left,int right,vector<int>& res) {
if(left < right){
int mid = left+(right-left)/2;
mergesort(nums,tmp,left,mid,res);
mergesort(nums,tmp,mid+1,right,res);
//合并nums[left,mid] nums[mid+1,right]
merge(nums,tmp,left,mid,right,res);
}
return;
} };

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

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

  6. LeetCode 315. Count of Smaller Numbers After Self

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

  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(Fenwick Tree)

    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. 2017-18一《电子商务概论》专科作业--经管B1601/2、经管B1631

    第1次作业: 1.你如何来定义和理解电子商务?电子商务对社会经济带了怎样的影响,企业.消费者的反应如何?你知道哪些电子商务企业,他们都属于什么类型? 2.请详细阐述应该如何关注哪些事项才能在淘宝网成功 ...

  2. CSS元素的显示与隐藏

    CSS元素的显示与隐藏 我们访问某些网站的时候,经常可以看到一些广告弹出来,点击关闭就不见了,但是重新刷新页面后,广告又会重新弹出来.这就是元素的显示和隐藏的一个应用. 1. display属性 di ...

  3. 【UR #13】Yist

    UOJ小清新题表 题目摘要 UOJ链接 给出一个排列 \(A\) 以及它的一个非空子序列 \(B\),给出一个 \(x\) 并进行若干次操作,每一次操作需要在 \(A\) 中选择一个长度恰好为 \(x ...

  4. Privileged Permission开机授权时序图 SourceCode android-10.0.0_r36

    Privileged Permission开机授权时序图 | SourceCode:android-10.0.0_r36 | Author:秋城 | v1.1SystemServerSystemSer ...

  5. go 继承

    package main import "fmt" type Animal struct { Color string } // 继承动物结构体 type Dog struct { ...

  6. centos6.8 Mysql-5.7.20 升级 mysql-8.0.14-1

    Mysql-5.7.20 升级 mysql-8.0.14-1   操作前建议先查阅以下网页初步了解Mysql版本升级信息  https://blog.csdn.net/u012946310/artic ...

  7. 面试不再慌,看完这篇保证让你写HashMap跟玩一样

    今天这篇文章给大家讲讲hashmap,这个号称是所有Java工程师都会的数据结构.为什么说是所有Java工程师都会呢,因为很简单,他们不会这个找不到工作.几乎所有面试都会问,基本上已经成了标配了. 在 ...

  8. CF1430 D. String Deletion(div 2)

    题目链接:http://codeforces.com/contest/1430/problem/D 题意:有一个长度为n(n<=2*10^5)的01字符串,每轮操作有两步: 第一步是删去字符串中 ...

  9. 【Azure Developer】使用.Net Core解析JSON的笔记

    在C#中解析JSON的一些历史代码记录,分别记录针对各种情况的解析方式. DLL的引用 using Newtonsoft.Json; using Newtonsoft.Json.Linq; 需要使用的 ...

  10. 自定义View(进度条)

    继承View重写带两个参数的构造方法,一个参数的不行不会加载视图,构造方法中初始化画笔这样不用没次刷新都要初始化浪费内存,在ondraw方法中绘图,invalidate方法会掉用ondraw方法重新绘 ...