Skip to content

Commit d26f8ac

Browse files
committed
Sync LeetCode submission Runtime - 113 ms (56.57%), Memory - 32.3 MB (57.58%)
1 parent d4cd024 commit d26f8ac

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<p>You are given a <strong>0-indexed</strong> array of <code>n</code> integers <code>differences</code>, which describes the <strong>differences </strong>between each pair of <strong>consecutive </strong>integers of a <strong>hidden</strong> sequence of length <code>(n + 1)</code>. More formally, call the hidden sequence <code>hidden</code>, then we have that <code>differences[i] = hidden[i + 1] - hidden[i]</code>.</p>
2+
3+
<p>You are further given two integers <code>lower</code> and <code>upper</code> that describe the <strong>inclusive</strong> range of values <code>[lower, upper]</code> that the hidden sequence can contain.</p>
4+
5+
<ul>
6+
<li>For example, given <code>differences = [1, -3, 4]</code>, <code>lower = 1</code>, <code>upper = 6</code>, the hidden sequence is a sequence of length <code>4</code> whose elements are in between <code>1</code> and <code>6</code> (<strong>inclusive</strong>).
7+
8+
<ul>
9+
<li><code>[3, 4, 1, 5]</code> and <code>[4, 5, 2, 6]</code> are possible hidden sequences.</li>
10+
<li><code>[5, 6, 3, 7]</code> is not possible since it contains an element greater than <code>6</code>.</li>
11+
<li><code>[1, 2, 3, 4]</code> is not possible since the differences are not correct.</li>
12+
</ul>
13+
</li>
14+
</ul>
15+
16+
<p>Return <em>the number of <strong>possible</strong> hidden sequences there are.</em> If there are no possible sequences, return <code>0</code>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> differences = [1,-3,4], lower = 1, upper = 6
23+
<strong>Output:</strong> 2
24+
<strong>Explanation:</strong> The possible hidden sequences are:
25+
- [3, 4, 1, 5]
26+
- [4, 5, 2, 6]
27+
Thus, we return 2.
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> differences = [3,-4,5,1,-2], lower = -4, upper = 5
34+
<strong>Output:</strong> 4
35+
<strong>Explanation:</strong> The possible hidden sequences are:
36+
- [-3, 0, -4, 1, 2, 0]
37+
- [-2, 1, -3, 2, 3, 1]
38+
- [-1, 2, -2, 3, 4, 2]
39+
- [0, 3, -1, 4, 5, 3]
40+
Thus, we return 4.
41+
</pre>
42+
43+
<p><strong class="example">Example 3:</strong></p>
44+
45+
<pre>
46+
<strong>Input:</strong> differences = [4,-7,2], lower = 3, upper = 6
47+
<strong>Output:</strong> 0
48+
<strong>Explanation:</strong> There are no possible hidden sequences. Thus, we return 0.
49+
</pre>
50+
51+
<p>&nbsp;</p>
52+
<p><strong>Constraints:</strong></p>
53+
54+
<ul>
55+
<li><code>n == differences.length</code></li>
56+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
57+
<li><code>-10<sup>5</sup> &lt;= differences[i] &lt;= 10<sup>5</sup></code></li>
58+
<li><code>-10<sup>5</sup> &lt;= lower &lt;= upper &lt;= 10<sup>5</sup></code></li>
59+
</ul>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Approach: Determine the Difference Between the Hidden Array's Upper and Lower Bounds
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
8+
x = y = curr = 0
9+
10+
for d in differences:
11+
curr += d
12+
x = min(x, curr)
13+
y = max(y, curr)
14+
15+
if y - x > upper - lower:
16+
return 0
17+
18+
return (upper - lower) - (y - x) + 1
19+

0 commit comments

Comments
 (0)