{"id":9454,"date":"2022-01-31T23:29:44","date_gmt":"2022-02-01T07:29:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9454"},"modified":"2022-01-31T23:29:55","modified_gmt":"2022-02-01T07:29:55","slug":"leetcode-2145-count-the-hidden-sequences","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2145-count-the-hidden-sequences\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2145. Count the Hidden Sequences"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;array of&nbsp;<code>n<\/code>&nbsp;integers&nbsp;<code>differences<\/code>, which describes the&nbsp;<strong>differences&nbsp;<\/strong>between each pair of&nbsp;<strong>consecutive&nbsp;<\/strong>integers of a&nbsp;<strong>hidden<\/strong>&nbsp;sequence of length&nbsp;<code>(n + 1)<\/code>. More formally, call the hidden sequence&nbsp;<code>hidden<\/code>, then we have that&nbsp;<code>differences[i] = hidden[i + 1] - hidden[i]<\/code>.<\/p>\n\n\n\n<p>You are further given two integers&nbsp;<code>lower<\/code>&nbsp;and&nbsp;<code>upper<\/code>&nbsp;that describe the&nbsp;<strong>inclusive<\/strong>&nbsp;range of values&nbsp;<code>[lower, upper]<\/code>&nbsp;that the hidden sequence can contain.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, given&nbsp;<code>differences = [1, -3, 4]<\/code>,&nbsp;<code>lower = 1<\/code>,&nbsp;<code>upper = 6<\/code>, the hidden sequence is a sequence of length&nbsp;<code>4<\/code>&nbsp;whose elements are in between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>6<\/code>&nbsp;(<strong>inclusive<\/strong>).<ul><li><code>[3, 4, 1, 5]<\/code>&nbsp;and&nbsp;<code>[4, 5, 2, 6]<\/code>&nbsp;are possible hidden sequences.<\/li><li><code>[5, 6, 3, 7]<\/code>&nbsp;is not possible since it contains an element greater than&nbsp;<code>6<\/code>.<\/li><li><code>[1, 2, 3, 4]<\/code>&nbsp;is not possible since the differences are not correct.<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the number of&nbsp;<strong>possible<\/strong>&nbsp;hidden sequences there are.<\/em>&nbsp;If there are no possible sequences, return&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> differences = [1,-3,4], lower = 1, upper = 6\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The possible hidden sequences are:\n- [3, 4, 1, 5]\n- [4, 5, 2, 6]\nThus, we return 2.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> differences = [3,-4,5,1,-2], lower = -4, upper = 5\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The possible hidden sequences are:\n- [-3, 0, -4, 1, 2, 0]\n- [-2, 1, -3, 2, 3, 1]\n- [-1, 2, -2, 3, 4, 2]\n- [0, 3, -1, 4, 5, 3]\nThus, we return 4.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> differences = [4,-7,2], lower = 3, upper = 6\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no possible hidden sequences. Thus, we return 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == differences.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>-10<sup>5<\/sup>&nbsp;&lt;= differences[i] &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>-10<sup>5<\/sup>&nbsp;&lt;= lower &lt;= upper &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Find the min and max of the cumulative sum of the differences.<\/p>\n\n\n\n<p>Ans = max(0, upper &#8211; lower &#8211; (hi &#8211; lo) + 1)<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numberOfArrays(vector<int>& differences, int lower, int upper) {\n    long long s = 0;\n    long long hi = 0;\n    long long lo = 0;\n    for (int d : differences) {\n      s += d;\n      hi = max(hi, s);\n      lo = min(lo, s);\n    }\n    return max(0LL, upper - lower - (hi - lo) + 1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;array of&nbsp;n&nbsp;integers&nbsp;differences, which describes the&nbsp;differences&nbsp;between each pair of&nbsp;consecutive&nbsp;integers of a&nbsp;hidden&nbsp;sequence of length&nbsp;(n + 1). More formally, call the hidden sequence&nbsp;hidden, then we&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,177,100],"class_list":["post-9454","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","tag-sequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9454","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=9454"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9454\/revisions"}],"predecessor-version":[{"id":9456,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9454\/revisions\/9456"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}