{"id":2164,"date":"2018-03-17T12:51:11","date_gmt":"2018-03-17T19:51:11","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2164"},"modified":"2018-03-17T12:56:34","modified_gmt":"2018-03-17T19:56:34","slug":"leetcode-413-arithmetic-slices","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-413-arithmetic-slices\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 413. Arithmetic Slices"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u8fd4\u56de\u6240\u6709\u5b50\u6570\u7ec4\u4e2d\u7b49\u5dee\u6570\u5217\u7684\u4e2a\u6570\u3002<\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p><a href=\"https:\/\/leetcode.com\/problems\/arithmetic-slices\/description\/\">https:\/\/leetcode.com\/problems\/arithmetic-slices\/description\/<\/a><\/p>\n<p>A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.<\/p>\n<p>For example, these are arithmetic sequence:<\/p>\n<pre class=\"crayon: false\">1, 3, 5, 7, 9\r\n7, 7, 7, 7\r\n3, -1, -5, -9<\/pre>\n<p>The following sequence is not arithmetic.<\/p>\n<pre class=\"crayon: false\">1, 1, 2, 5, 7<\/pre>\n<p>A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 &lt;= P &lt; Q &lt; N.<\/p>\n<p>A slice (P, Q) of array A is called arithmetic if the sequence:<br \/>\nA[P], A[p + 1], &#8230;, A[Q &#8211; 1], A[Q] is arithmetic. In particular, this means that P + 1 &lt; Q.<\/p>\n<p>The function should return the number of arithmetic slices in the array A.<\/p>\n<h2><b>Example:<\/b><\/h2>\n<pre class=\"crayon: false \">A = [1, 2, 3, 4]\r\n\r\nreturn: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.<\/pre>\n<h1><strong>Solution 0: Reduction<\/strong><\/h1>\n<p>Reduce the problem to # of all 1 sub arrays.<\/p>\n<p>B[i &#8211; 2] = is_slice(A[i], A[i+1], A[i+2])<\/p>\n<p>Time Complexity: O(n)<\/p>\n<p>Space Complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int numberOfArithmeticSlices(vector&lt;int&gt;&amp; A) {\r\n    if (A.size() &lt; 3) return 0;\r\n    vector&lt;int&gt; B(A.size() - 2, 0);\r\n    for (int i = 2; i &lt; A.size() ; ++i) {      \r\n      if (A[i - 1] - A[i - 2] == A[i] - A[i - 1])\r\n        B[i - 2] = 1;      \r\n    }\r\n    return all1SubArrays(B);\r\n  }\r\nprivate:\r\n  \/\/ return number of arrays whose values are all ones.\r\n  int all1SubArrays(const vector&lt;int&gt;&amp; A) {\r\n    int sum = 0;\r\n    int curr = 0;\r\n    for (int i = 0; i &lt; A.size(); ++i)\r\n      if (A[i]) sum += ++curr;\r\n      else curr = 0;\r\n    return sum;\r\n  }\r\n};<\/pre>\n<h1><strong>Solution 1: Combined<\/strong><\/h1>\n<p>C++<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int numberOfArithmeticSlices(vector&lt;int&gt;&amp; A) {\r\n    int sum = 0;\r\n    int curr = 0;\r\n    for (int i = 2; i &lt; A.size() ; ++i) {      \r\n      if (A[i - 1] - A[i - 2] == A[i] - A[i - 1]) {\r\n        sum += ++curr;\r\n      } else {\r\n        curr = 0;\r\n      }\r\n    }\r\n    return sum;\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems:<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-795-number-of-subarrays-with-bounded-maximum\/\">\u82b1\u82b1\u9171 LeetCode 795. Number of Subarrays with Bounded Maximum<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u8fd4\u56de\u6240\u6709\u5b50\u6570\u7ec4\u4e2d\u7b49\u5dee\u6570\u5217\u7684\u4e2a\u6570\u3002 Problem https:\/\/leetcode.com\/problems\/arithmetic-slices\/description\/ A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184,46],"tags":[18,177,182,41],"class_list":["post-2164","post","type-post","status-publish","format-standard","hentry","category-array","category-dynamic-programming","tag-dp","tag-medium","tag-reduction","tag-subarray","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2164","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=2164"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2164\/revisions"}],"predecessor-version":[{"id":2169,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2164\/revisions\/2169"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}