landscapemi的博客

###### leetcode_c++:Divide and Conquer：Count of Smaller Numbers After Self（315）

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

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

http://blog.csdn.net/murmured/article/details/50201147

struct Node {
int val;
int index;
int cnt;
Node(int val, int index) : val(val), index(index), cnt(0) {}
bool operator <= (const Node &node2)const {
return val <= node2.val;
}
};

class Solution {
public:
void combine(vector<Node> &nums, int Lpos, int Lend, int Rend, vector<Node> &temp) {
int Rpos = Lend + 1;
int Tpos = Lpos;
int n = Rend - Lpos + 1;
int t = Rpos;
while (Lpos <= Lend && Rpos <= Rend) {
if (nums[Lpos] <= nums[Rpos]) {
temp[Tpos] = nums[Lpos];
temp[Tpos].cnt += Rpos - t ;
Tpos++; Lpos++;
}
else {
temp[Tpos++] = nums[Rpos++];
}
}

while (Lpos <= Lend) {
temp[Tpos] = nums[Lpos];
temp[Tpos].cnt += Rpos - t;
Tpos++; Lpos++;
}

while (Rpos <= Rend)
temp[Tpos++] = nums[Rpos++];

for (int i = 0; i< n; i++, Rend--)
nums[Rend] = temp[Rend];
}

void merge_sort(vector<Node> & nums, int L, int R, vector<Node> &temp) {
if (L < R) {
int m = (L + R) >> 1;
merge_sort(nums, L, m, temp);
merge_sort(nums, m + 1, R, temp);
combine(nums, L, m, R, temp);
}
}

vector<int> countSmaller(vector<int>& nums) {
vector<Node> mynums;
vector<Node> temp(nums.size(), Node(0, 0));
for (int i = 0; i < nums.size(); i++)
mynums.push_back(Node(nums[i], i));

vector<int> ans(nums.size(), 0);
merge_sort(mynums, 0, nums.size() - 1, temp);

for (int i = 0; i < nums.size(); i++)
ans[mynums[i].index] = mynums[i].cnt;

return ans;
}
};  

#### [leetcode] 315. Count of Smaller Numbers After Self 解题报告

2016-05-05 10:43:51

#### LeetCode 315. Count of Smaller Numbers After Self （逆序数对）

2016-04-22 13:21:32

#### leetcode 315. Count of Smaller Numbers After Self

2016-08-18 23:10:44

#### LeetCode 315. Count of Smaller Numbers After Self

2016-05-06 23:26:47

#### leetcode -- Count of Smaller Numbers After Self -- 经典求逆序数

2015-12-25 19:46:16

#### 【Leetcode】Count of Smaller Numbers After Self

2016-05-29 21:45:13

#### Google 2016 面试题6 | Count of Smaller Numbers After Self(数组计数)

2016-03-24 17:22:13

#### leetcode ：315. Count of Smaller Numbers After Self ：归并排序应用

2016-03-29 21:11:26

#### 线段树&315 LeetCode：Count of Smaller Numbers After Self

2016-08-30 04:58:47

#### 315. Count of Smaller Numbers After Self

2016-05-05 22:06:28

## 不良信息举报

leetcode_c++:Divide and Conquer：Count of Smaller Numbers After Self（315）