Skip to content

Commit 50b4825

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.8 MB (68.01%)
1 parent ea405e3 commit 50b4825

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.</p>
2+
3+
<ul>
4+
<li>For example, <code>[1, 7, 4, 9, 2, 5]</code> is a <strong>wiggle sequence</strong> because the differences <code>(6, -3, 5, -7, 3)</code> alternate between positive and negative.</li>
5+
<li>In contrast, <code>[1, 4, 7, 2, 5]</code> and <code>[1, 7, 4, 5, 5]</code> are not wiggle sequences. The first is not because its first two differences are positive, and the second is not because its last difference is zero.</li>
6+
</ul>
7+
8+
<p>A <strong>subsequence</strong> is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.</p>
9+
10+
<p>Given an integer array <code>nums</code>, return <em>the length of the longest <strong>wiggle subsequence</strong> of </em><code>nums</code>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> nums = [1,7,4,9,2,5]
17+
<strong>Output:</strong> 6
18+
<strong>Explanation:</strong> The entire sequence is a wiggle sequence with differences (6, -3, 5, -7, 3).
19+
</pre>
20+
21+
<p><strong class="example">Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> nums = [1,17,5,10,13,15,10,5,16,8]
25+
<strong>Output:</strong> 7
26+
<strong>Explanation:</strong> There are several subsequences that achieve this length.
27+
One is [1, 17, 10, 13, 10, 16, 8] with differences (16, -7, 3, -3, 6, -8).
28+
</pre>
29+
30+
<p><strong class="example">Example 3:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> nums = [1,2,3,4,5,6,7,8,9]
34+
<strong>Output:</strong> 2
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
42+
<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
43+
</ul>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Follow up:</strong> Could you solve this in <code>O(n)</code> time?</p>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def wiggleMaxLength(self, nums: List[int]) -> int:
3+
# n = len(nums)
4+
# dp1 = [1] * n
5+
# dp2 = [1] * n
6+
# result = 1
7+
8+
# for i in range(1, n):
9+
# for j in range(i):
10+
# if nums[i] < nums[j]:
11+
# dp1[i] = max(dp1[i], dp2[j] + 1)
12+
# elif nums[i] > nums[j]:
13+
# dp2[i] = max(dp2[i], dp1[j] + 1)
14+
# result = max(result, dp1[i], dp2[i])
15+
16+
# return result
17+
18+
inc, dec = 1, 1
19+
for num1, num2 in pairwise(nums):
20+
if num2 > num1:
21+
inc = dec + 1
22+
elif num2 < num1:
23+
dec = inc + 1
24+
return max(inc, dec)

0 commit comments

Comments
 (0)