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]
.
原文地址(我的新博客) :http://www.hrwhisper.me/leetcode-count-of-smaller-numbers-after-self/
本文由 hrwhisper 原创发布,转载请点名出处
题目地址 leetcode Count of Smaller Numbers After Self
题意:
给定nums数组,求数组中每个元素i的右边比其小的数
思路:
简单的说就是求逆序数。
合并排序
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 &nums, int Lpos, int Lend, int Rend, vector &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 & nums, int L, int R, vector &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 countSmaller(vector& nums) {
vector mynums;
vector temp(nums.size(), Node(0, 0));
for (int i = 0; i < nums.size(); i++)
mynums.push_back(Node(nums[i], i));
vector 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;
}
};
Fenwick 树
class FenwickTree {
vector sum_array;
int n;
inline int lowbit(int x) {
return x & -x;
}
public:
FenwickTree(int n) :n(n), sum_array(n + 1, 0) {}
void add(int x, int val) {
while (x <= n) {
sum_array[x] += val;
x += lowbit(x);
}
}
int sum(int x) {
int res = 0;
while (x > 0) {
res += sum_array[x];
x -= lowbit(x);
}
return res;
}
};
class Solution {
public:
vector countSmaller(vector& nums) {
vector temp_num = nums;
sort(temp_num.begin(), temp_num.end());
unique(temp_num.begin(), temp_num.end());
unordered_map dic;
for (int i = 0; i < temp_num.size(); i++)
dic[temp_num[i]] = i + 1;
FenwickTree tree(nums.size());
vector ans(nums.size(),0);
for (int i = nums.size() - 1; i >= 0; i--) {
ans[i] = tree.sum(dic[nums[i]] - 1);
tree.add(dic[nums[i]],1);
}
return ans;
}
};
您所在位置:细语呢喃 > leetcode Count of Smaller Numbers After Self
本文由 hrwhisper 原创发布,转载请点名出处