[LeetCode 307] Range Sum Query - Mutable

来源:互联网 发布:猎豹浏览器 mac 编辑:程序博客网 时间:2024/05/01 08:35

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

The update(i, val) function modifies nums by updating the element at index i to val.

Example:

Given nums = [1, 3, 5]sumRange(0, 2) -> 9update(1, 2)sumRange(0, 2) -> 8

Note:

  1. The array is only modifiable by the update function.
  2. You may assume the number of calls to update and sumRange function is distributed evenly.
Solution:

assume update and sumRange function is distributed evenly, we need O(lgn) complexity for both functions.

Could use binary tree achieve this. Use segment tree to store, the node has left to right range, and the range sum. For update operation, need to find the node and update all node which range include the position. For sumRange operation, need to find the nodes which range could consist the sumrange you are looking for.


public class NumArray {    class TreeNode {        int start = 0;        int end = 0;        int sum = 0;        TreeNode left = null;        TreeNode right = null;    }    TreeNode root = null;    public void init(int[] nums) {        if(nums == null || nums.length == 0) return;        this.root = buildTree(0, nums.length-1, nums);    }    public TreeNode buildTree(int start, int end, int[] data) {        TreeNode t = new TreeNode();        t.start = start;        t.end = end;        if(start == end) {            t.sum = data[start];            return t;        }                int mid = start + (end-start)/2;        t.left = buildTree(start, mid, data);        t.right = buildTree(mid+1, end, data);        t.sum = t.left.sum + t.right.sum;        return t;    }    public NumArray(int[] nums) {        init(nums);    }    public void updateTree(TreeNode node, int index, int val) {        if(node == null) return;        if(node.start == node.end) {            node.sum = val;            return;        }        int mid = node.start + (node.end - node.start)/2;        if(index<=mid) {            updateTree(node.left, index, val);        }else {            updateTree(node.right, index, val);        }        node.sum = node.left.sum + node.right.sum;    }        void update(int i, int val) {        updateTree(this.root, i, val);    }    public int queryTree(TreeNode node, int left, int right) {        if(node == null) return 0;        if(node.start == left && node.end == right) return node.sum;        int mid = node.start + (node.end - node.start)/2;        if(mid>=right) {            return queryTree(node.left, left, right);        }else if(mid<left) {            return queryTree(node.right, left, right);        }else {            return queryTree(node.left, left, mid) + queryTree(node.right, mid+1, right);        }    }    public int sumRange(int i, int j) {        return queryTree(root, i, j);    }}// Your NumArray object will be instantiated and called as such:// NumArray numArray = new NumArray(nums);// numArray.sumRange(0, 1);// numArray.update(1, 10);// numArray.sumRange(1, 2);




0 0
原创粉丝点击