Interval Sum I && II

Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the sum number between index start and end in the given array, return the result list.

Example

For array [1,2,7,8,5], and queries [(0,4),(1,2),(2,4)], return [23,9,20].

Analysis:
Use an array to save the sum from 0 to i. Then for query [i, j], we shoud return sum[j] - sum[i - 1].

 1 /**
 2  * Definition of Interval:
 3  * public classs Interval {
 4  *     int start, end;
 5  *     Interval(int start, int end) {
 6  *         this.start = start;
 7  *         this.end = end;
 8  *     }
 9  */
10 public class Solution {
11     /**
12      *@param A, queries: Given an integer array and an query list
13      *@return: The result list
14      */
15     public ArrayList<Long> intervalSum(int[] A,
16                                        ArrayList<Interval> queries) {
17
18         ArrayList<Long> list = new ArrayList<Long>();
19         if (A == null || A.length == 0) return list;
20         if (queries == null || queries.size() == 0) return list;
21
22         long[] sum = new long[A.length];
23
24         for (int i = 0; i < sum.length; i++) {
25             if (i == 0) {
26                 sum[i] = A[i];
27             } else {
28                 sum[i] += sum[i - 1] + A[i];
29             }
30         }
31
32         for (int i = 0; i < queries.size(); i++) {
33             Interval interval = queries.get(i);
34             if (interval.start == 0) {
35                 list.add(sum[interval.end]);
36             } else {
37                 list.add(sum[interval.end] - sum[interval.start - 1]);
38             }
39         }
40         return list;
41     }
42 }

Interval Sum II

Given an integer array in the construct method, implement two methods query(start, end)and modify(index, value):

  • For query(startend), return the sum from index start to index end in the given array.
  • For modify(indexvalue), modify the number in the given index to value

Example

Given array A = [1,2,7,8,5].

  • query(0, 2), return 10.
  • modify(0, 4), change A[0] from 1 to 4.
  • query(0, 1), return 6.
  • modify(2, 1), change A[2] from 7 to 1.
  • query(2, 4), return 14.

Analysis:

As the value in the array may change, so it is better to build a segement tree. If the value in the tree is changed, we also need to update its parent node.

 1 public class Solution {
 2     /* you may need to use some attributes here */
 3
 4     SegmentTreeNode root;
 5
 6     /**
 7      * @param A:
 8      *            An integer array
 9      */
10     public Solution(int[] A) {
11         if (A == null || A.length == 0)
12             return;
13         root = build(A, 0, A.length - 1);
14     }
15
16     private SegmentTreeNode build(int[] A, int start, int end) {
17         if (A == null || start < 0 || end >= A.length)
18             return null;
19         SegmentTreeNode root = new SegmentTreeNode(start, end, 0);
20         if (start == end) {
21             root.sum = A[start];
22         } else {
23             int mid = (end - start) / 2 + start;
24             root.left = build(A, start, mid);
25             root.right = build(A, mid + 1, end);
26             root.sum = root.left.sum + root.right.sum;
27         }
28         return root;
29     }
30
31     public long query(int start, int end) {
32         if (root == null || start > end)
33             return 0;
34         return helper(root, Math.max(0, start), Math.min(end, root.end));
35     }
36
37     public long helper(SegmentTreeNode root, int start, int end) {
38         if (root.start == start && root.end == end) {
39             return root.sum;
40         }
41
42         int mid = (root.start + root.end) / 2;
43         if (start >= mid + 1) {
44             return helper(root.right, start, end);
45         } else if (end <= mid) {
46             return helper(root.left, start, end);
47         } else {
48             return helper(root.left, start, mid) + helper(root.right, mid + 1, end);
49         }
50     }
51
52     public void modify(int index, int value) {
53         if (root == null)
54             return;
55         if (index < root.start && index > root.end)
56             return;
57         modifyHelper(root, index, value);
58     }
59
60     public void modifyHelper(SegmentTreeNode root, int index, int value) {
61         if (root.start == root.end && root.start == index) {
62             root.sum = value;
63             return;
64         }
65
66         int mid = (root.start + root.end) / 2;
67         if (index >= mid + 1) {
68             modifyHelper(root.right, index, value);
69         } else {
70             modifyHelper(root.left, index, value);
71         }
72         root.sum = root.left.sum + root.right.sum;
73
74     }
75 }
76
77 class SegmentTreeNode {
78     public int start, end, sum;
79     public SegmentTreeNode left, right;
80
81     public SegmentTreeNode(int start, int end, int sum) {
82         this.start = start;
83         this.end = end;
84         this.sum = sum;
85         this.left = this.right = null;
86     }
87 }
时间: 2024-11-09 09:33:56

Interval Sum I && II的相关文章

Lintcode207 Interval Sum II solution 题解

[题目描述] Given an integer array in the construct method, implement two methods query(start, end) and modify(index, value): For query(start,end), return the sum from index start to index end in the given array. For modify(index,value), modify the number

Leetcode | Combination Sum I &amp;&amp; II

Combination Sum I Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note:All numbers (includ

leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 r

Lintcode: Interval Sum II

Given an integer array in the construct method, implement two methods query(start, end) and modify(index, value): For query(start, end), return the sum from index start to index end in the given array. For modify(index, value), modify the number in t

Lintcode: Interval Sum

Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the sum number between index start and end in the given array, return the result l

Leetcode 39 40 216 Combination Sum I II III

Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includi

1. Two Sum I &amp; II &amp; III

1. Given an array of integers, return indices of the two numbers such that they add up to a specific target. 问题: 1.数组是否有序 2. 数组排序 // sorting array Arrays.sort(iArr) 方法1:O(n^2) public class Solution { public int[] twoSum(int[] nums, int target) { int[

子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II

引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一篇文章的思路略作改进. 例 1,求元素数量为定值的所有子集 Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n 

Nested List Weight Sum I &amp; II

Nested List Weight Sum I Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Example 1:Given the list