leetcode_c++:Divide and Conquer:Count of Range Sum(327)

原创 2016年08月28日 15:48:49

Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.
Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive.

Note:
A naive algorithm of O(n2) is trivial. You MUST do better than that.

Example:
Given nums = [-2, 5, -1], lower = -2, upper = 2,
Return 3.
The three ranges are : [0, 0], [2, 2], [0, 2] and their respective sums are: -2, -1, 2.


typedef long long LL;  
struct SegmentTreeNode {  
    LL L, R;  
    int cnt;  
    SegmentTreeNode *left, *right;  
    SegmentTreeNode(LL L, LL R) :L(L), R(R), cnt(0), left(NULL), right(NULL) {}  
};  

class SegmentTree {  
    SegmentTreeNode * root;  
    SegmentTreeNode * buildTree(vector<LL> &nums, int L, int R) {  
        if (L > R) return NULL;  
        SegmentTreeNode * root = new SegmentTreeNode(nums[L], nums[R]);  
        if (L == R) return root;  
        int mid = (L + R) >> 1;  
        root->left = buildTree(nums, L, mid);  
        root->right = buildTree(nums, mid + 1, R);  
        return root;  
    }  

    void update(SegmentTreeNode * root, LL val) {  
        if (root && root->L <= val &&  val <= root->R) {  
            root->cnt++;  
            update(root->left, val);  
            update(root->right, val);  
        }  
    }  

    int sum(SegmentTreeNode * root, LL L, LL R) {  
        if (!root || root->R < L ||  R < root->L ) return 0;  
        if (L <= root->L  && root->R <= R) return root->cnt;  
        return sum(root->left, L, R) + sum(root->right, L, R);  
    }  

public:  
    SegmentTree(vector<LL> &nums, int L, int R) { root = buildTree(nums, L, R); }  

    int sum(LL L, LL R) {  
        return sum(root, L, R);  
    }  

    void update(LL val) {  
        update(root, val);  
    }  
};  

class Solution {  
public:  
    int countRangeSum(vector<int>& nums, int lower, int upper) {  
        if (nums.size() == 0) return 0;  
        vector<LL> sum_array (nums.size(),0);  
        sum_array[0] = nums[0];  
        for (int i = 1; i < sum_array.size(); i++) {  
            sum_array[i] = nums[i] + sum_array[i - 1];  
        }  
        LL sum = sum_array[sum_array.size() - 1];  
        sort(sum_array.begin(), sum_array.end());  
        auto t = unique(sum_array.begin(), sum_array.end());  
        SegmentTree tree(sum_array, 0, t - sum_array.begin() - 1);  
        int ans = 0;  
        for (int i = nums.size() - 1; i >= 0; i--) {  
            tree.update(sum);  
            sum -= nums[i];  
            ans += tree.sum(lower + sum,upper + sum);  
        }  
        return ans;  
    }  
};  
版权声明:本文为博主原创文章,未经博主允许不得转载。

[leetcode] 327. Count of Range Sum 解题报告

题目链接: https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the...

LeetCode 327. Count of Range Sum(区间和计数)

原题网址:https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the num...
  • jmspan
  • jmspan
  • 2016年04月28日 00:32
  • 845

Leetcode 327. Count of Range Sum[hard]

进入hard模式。题目: Given an integer array nums, return the number of range sums that lie in [lower, upper...

LeetCode 327 Count of Range Sum

Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive....

[Leetcode] 327. Count of Range Sum 解题报告

题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] i...

<LeetCode OJ> 327. Count of Range Sum

327. Count of Range Sum My Submissions Question Total Accepted: 93 Total Submissions: 316 Diffic...

算法Week03.01 - LeetCode 327. Count of Range Sum

算法Week03.01 - LeetCode 327. Count of Range Sum

327. Count of Range Sum 满足某个范围数内的区间个数

Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive....

327_Count of Range Sum

Leetcode 327 题意理解及解题思路分享。

LeetCode Count of Range Sum

Description: Given an integer array nums, return the number of range sums that lie in [lower, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode_c++:Divide and Conquer:Count of Range Sum(327)
举报原因:
原因补充:

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