leetcode_c++:Divide and Conquer:Count of Smaller Numbers After Self(315)

原创 2016年08月28日 15:46:39

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_c++:Divide and Conquer: The Skyline Problem(218)

题目class Solution { public: vector getSkyline(vector& buildings) { vector< pair > edges; ...

LeetCode #315 Count of Smaller Numbers After Self

LeetCode #315 Count of Smaller Numbers After Self问题描述 给出一个整数序列,求解在这个序列中每个数字右边有多少个数字比当前数字小。 例子: G...

Leetcode 315. Count of Smaller Numbers After Self[hard]

题目: You are given an integer array nums and you have to return a new counts array. The counts array...

LeetCode 315. Count of Smaller Numbers After Self (逆序数对)

原题网址:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer a...
  • jmspan
  • jmspan
  • 2016年04月22日 13:21
  • 1488

LeetCode315—Count of Smaller Numbers After Self—Java版归并算法

这是我在研究leetcode的solution第一个解决算法时,自己做出的理解,并且为了大家能看懂,做出了详细的注释。 此算法算是剑指Offer36的升级版,都使用的归并算法,但是此处的算法,难度更...

leetcode 315. Count of Smaller Numbers After Self

315. Count of Smaller Numbers After Self 线段树

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

剑指Offer 面试题36:数组中的逆序对 题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。 例如, 在数组{7,...

LeetCode 315. Count of Smaller Numbers After Self

又到一周更新时,这次的题目依旧来自LeetCode,由于这周老师还是讲的divide and conquer,按照以往的惯例,我从难度为hard的题目中挑选了一道通过率第二高的题,因为第一高的题上一次...
  • Ardenso
  • Ardenso
  • 2017年03月12日 15:11
  • 176

LeetCode 315 Count of Smaller Numbers After Self(树状数组)

LeetCode 315 Count of Smaller Numbers After Self(树状数组)

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

题目链接: https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode_c++:Divide and Conquer:Count of Smaller Numbers After Self(315)
举报原因:
原因补充:

(最多只允许输入30个字)