{"id":3563,"date":"2018-08-17T01:00:04","date_gmt":"2018-08-17T08:00:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3563"},"modified":"2018-08-17T01:15:45","modified_gmt":"2018-08-17T08:15:45","slug":"leetcode-303-range-sum-query-immutable","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-303-range-sum-query-immutable\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 303. Range Sum Query &#8211; Immutable"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 303. Range Sum Query - Immutable - \u5237\u9898\u627e\u5de5\u4f5c EP10\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/awS9dn_XCmI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>Given an integer array\u00a0<i>nums<\/i>, find the sum of the elements between indices\u00a0<i>i<\/i>\u00a0and\u00a0<i>j<\/i>\u00a0(<i>i<\/i>\u00a0\u2264\u00a0<i>j<\/i>), inclusive.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false\">Given nums = [-2, 0, 3, -5, 2, -1]\r\n\r\nsumRange(0, 2) -&gt; 1\r\nsumRange(2, 5) -&gt; -1\r\nsumRange(0, 5) -&gt; -3\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>You may assume that the array does not change.<\/li>\n<li>There are many calls to\u00a0<i>sumRange<\/i>\u00a0function.<\/li>\n<\/ol>\n<h1><strong>Solution: Prefix sum<\/strong><\/h1>\n<p>sums[i] = nums[0] + nums[1] + &#8230; + nums[i]<\/p>\n<p>sumRange(i, j) = sums[j] &#8211; sums[i &#8211; 1]<\/p>\n<p>Time complexity: pre-compute: O(n), query: O(1)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 112 ms\r\nclass NumArray {\r\npublic:\r\n  NumArray(vector&lt;int&gt; nums): sums_(nums) {      \r\n    if (nums.empty()) return;            \r\n    for (int i = 1; i &lt; nums.size();++i)\r\n      sums_[i] += sums_[i - 1];\r\n  }\r\n\r\n  int sumRange(int i, int j) {\r\n    if (i == 0) return sums_[j];\r\n    return sums_[j] - sums_[i-1];\r\n  }\r\nprivate:    \r\n  vector&lt;int&gt; sums_;\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an integer array\u00a0nums, find the sum of the elements between indices\u00a0i\u00a0and\u00a0j\u00a0(i\u00a0\u2264\u00a0j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[20,18,222,200,98],"class_list":["post-3563","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-array","tag-dp","tag-easy","tag-prefix-sum","tag-range-query","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3563"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3563\/revisions"}],"predecessor-version":[{"id":3567,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3563\/revisions\/3567"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}