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

 1 class Solution {
 2 public:
 3  struct Node
 4 {
 5     int index;
 6     int num;
 7     Node(int a,int b)
 8     {
 9         index =a;
10         num =b;
11     }
12 };
13
14 void mergesort(vector<Node>& nums,int start,int end,vector<int>&res)
15 {
16
17     if(start==end)
18         return;
19     if(start+1==end)
20     {
21         if(nums[start].num>nums[end].num)
22         {
23             Node tmp(1,1) ;
24             res[nums[start].index]++;
25             tmp = nums[start];
26             nums[start] = nums[end];
27             nums[end] = tmp;
28             return;
29         }
30     }
31
32     int middle = (start+end)/2;
33     mergesort(nums,start,middle,res);
34     mergesort(nums,middle+1,end,res);
35
36     int s1 = start;
37     int s2 = middle+1;
38     vector<int>right;
39     vector<Node> pac;
40     while(s1<=middle || s2<=end)
41     {
42
43         if(s1<=middle &&s2<=end)
44         {
45             if(nums[s1].num<=nums[s2].num)
46             {
47                 pac.push_back(nums[s1]);
48
49                 res[nums[s1].index]+=right.size();
50                 s1++;
51             }
52             else
53             {
54                 pac.push_back(nums[s2]);
55                 right.push_back(nums[s2].num);
56                 s2++;
57             }
58         }
59         else if(s1<=middle)
60         {
61             pac.push_back(nums[s1]);
62
63                 res[nums[s1].index]+=right.size();
64             s1++;
65         }
66         else if(s2<=end)
67         {
68             pac.push_back(nums[s2]);
69             right.push_back(nums[s2].num);
70             s2++;
71         }
72     }
73     for(int i=start;i<=end;i++)
74     {
75         nums[i]=pac[i-start];
76     }
77
78 }
79  vector<int> countSmaller(vector<int>& nums) {
80
81      int n = nums.size();
82      vector<int>res;
83      vector<Node>nodes;
84      if(n==0)
85          return res;
86      res.resize(n,0);
87      for(int i=0;i<n;i++)
88      {
89          nodes.push_back(Node(i,nums[i]));
90      }
91       mergesort(nodes,0,n-1,res);
92       return res;
93
94
95  }
96
97
98 };
时间: 2024-08-24 18:18:51

leetcode 315. Count of Smaller Numbers After Self的相关文章

剑指Offer 面试题36:数组中的逆序对及其变形(Leetcode 315. Count of Smaller Numbers After Self)题解

剑指Offer 面试题36:数组中的逆序对 题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 例如, 在数组{7,5,6,4}中,一共存在5个逆序对,分别是(7,6),(7,5),(7,4),(6,4)和(5,4),输出5. 提交网址: http://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5?tpId=13&tqId=11188 或 htt

第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树

Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的树. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树的功能是什么呢? 其实就是一句话. 原序列a的子序列a[l,r]在a排序后的序列b的子序列[L,R]中的个数. 显然本题只用到了主席树的一小部分功能. const int N = 100000 + 5; int a[N], b[N], rt[N * 20], ls[N * 20], rs[N * 2

[LeetCode][JavaScript]Count of Smaller Numbers After Self

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,

【Leetcode】Count of Smaller Numbers After Self

题目链接:https://leetcode.com/problems/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 righ

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

leetcode Count of Smaller Numbers After Self

题目连接 https://leetcode.com/problems/count-of-smaller-numbers-after-self/ Count of Smaller Numbers After Self Description 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]$

Count of Smaller Numbers After Self -- LeetCode

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

[email&#160;protected] [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)

https://leetcode.com/problems/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

[LeetCode] 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