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:

  1. Input: nums = [5,2,6,1]
  2. Output: [2,1,1,0]
  3. Explanation:
  4. To the right of 5 there are 2 smaller elements (2 and 1).
  5. To the right of 2 there is only 1 smaller element (1).
  6. To the right of 6 there is 1 smaller element (1).
  7. To the right of 1 there is 0 smaller element.

Constraints:

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

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

  1. res[nums[i].second] += (j-mid-1);
    这个里面坑比较多
  1. class Solution {
  2. public:
  3. //法二:利用归并排序求逆序对数的方法
  4. //https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/submissions/
  5.  
  6. vector<int> countSmaller(vector<int>& nums) {
  7. int n=nums.size();
  8. vector<int> res(n,0);
  9. if(n==0 || n==1) return res;
  10. vector<pair<int,int>> tmp(n,pair<int,int>{0,0});
  11. vector<pair<int,int>> idx;
  12. for(int i=0;i<n;i++){
  13. idx.push_back(make_pair(nums[i],i));
  14. }
  15. mergesort(idx,tmp,0,n-1,res);
  16. return res;
  17. }
  18.  
  19. //merge的过程将left到right有序重新存入nums.归并nums[left,mid],nums[mid+1,right]
  20. void merge(vector<pair<int,int>>& nums,vector<pair<int,int>>& tmp,int left,int mid,int right,vector<int>& res) {
  21. int i=left,j=mid+1,k=left;
  22. for(;i<=mid&&j<=right;){
  23. if(nums[i].first<=nums[j].first){
  24. //不同于算整个数组逆序数
  25. //这里的i不是之前的i。归并后数字的位置被改变了.所以利用pari记录nums[i]原始位置
  26. //res[i] += (j-mid-1);
  27. res[nums[i].second] += (j-mid-1);
  28. tmp[k++] = nums[i++];
  29. }else{
  30. tmp[k++] = nums[j++];
  31. }
  32. }
  33. //还有未归并完成的
  34. while(i<=mid){
  35. //先计算res
  36. res[nums[i].second] += (j-mid-1);
  37. tmp[k++]=nums[i++];
  38. }
  39. while(j<=right){
  40. tmp[k++]=nums[j++];
  41. }
  42. //将tmp重新放入nums,那么nums[left,right]即有序了
  43. for(int i=left;i<=right;i++){
  44. nums[i] = tmp[i];
  45. }
  46. return;
  47. }
  48. //归并排序
  49. void mergesort(vector<pair<int,int>>& nums,vector<pair<int,int>>& tmp,int left,int right,vector<int>& res) {
  50. if(left < right){
  51. int mid = left+(right-left)/2;
  52. mergesort(nums,tmp,left,mid,res);
  53. mergesort(nums,tmp,mid+1,right,res);
  54. //合并nums[left,mid] nums[mid+1,right]
  55. merge(nums,tmp,left,mid,right,res);
  56. }
  57. return;
  58. }
  59.  
  60. };

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一《电子商务概论》本科作业-商A1551

    第1次作业: 1 2017年双十一新营销方案 2 销售额达1682亿元分析组成及了解猫狗大战 3 破亿店铺举例. 第2次作业: 1.你如何来定义和理解电子商务?电子商务对社会经济带了怎样的影响,企业. ...

  2. python数据清洗

    盖帽法 分箱法 简单随机抽和分层抽

  3. k8s- centos7.8搭建

    vmware16.0 centos7.8 1. 使用vmware安装 centos环境  cpu4个 内存4G 网络nat模式 2.配置网络 vim /etc/sysconfig/network-sc ...

  4. spring boot:使用redis cluster集群作为分布式session(redis 6.0.5/spring boot 2.3.1)

    一,为什么要使用分布式session? HpptSession默认使用内存来管理Session,如果将应用横向扩展将会出现Session共享问题, 所以我们在创建web集群时,把session保存到r ...

  5. selenium 提取天猫网页数据

    from time import sleep from selenium import webdriver br = webdriver.Chrome() url = "https://ww ...

  6. laravel重写

    laravel location / { try_files $uri $uri/ /index.php?$query_string; } ci location / { try_files $uri ...

  7. 面经分享:看非科班研究生如何转行斩获 ATM 大厂的 Offer ?

    前言 先介绍一下自己的情况吧,本科和研究生都是通信专业,本科是某 Top2,研究生是香港某大学.了解了通信行业的就业情况和工作内容后,大概今年3月份的时候开始想转互联网. 本人相关的基础情况是:学校学 ...

  8. 《Kafka笔记》3、Kafka高级API

    目录 1 Kafka高级API特性 1.1 Offset的自动控制 1.1.1 消费者offset初始策略 1.1.2 消费者offset自动提交策略 1.2 Acks & Retries(应 ...

  9. spring-boot-route(二十一)quartz实现动态定时任务

    Quartz是一个定时任务的调度框架,涉及到的主要概念有以下几个: Scheduler:调度器,所有的调度都由它控制,所有的任务都由它管理. Job:任务,定义业务逻辑. JobDetail:基于Jo ...

  10. oracle oracle sqldeveloper 12505 创建连接失败

    ref:http://blog.csdn.net/yangwenxue_admin/article/details/45062557