Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cc2b1ed

Browse files
update comments for 376
1 parent 8ff22ca commit cc2b1ed

File tree

1 file changed

+10
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+10
-0
lines changed

src/main/java/com/fishercoder/solutions/_376.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ public class _376 {
55
public static class Solution1 {
66
/**
77
* credit: https://leetcode.com/problems/wiggle-subsequence/discuss/84843/Easy-understanding-DP-solution-with-O(n)-Java-version
8+
* <p>
9+
* For every position in the array, there are only three possible statuses for it.
10+
* <p>
11+
* up position, it means nums[i] > nums[i-1]
12+
* down position, it means nums[i] < nums[i-1]
13+
* equals to position, nums[i] == nums[i-1]
14+
* So we can use two arrays up[] and down[] to record the max wiggle sequence length so far at index i.
15+
* If nums[i] > nums[i-1], that means it wiggles up. the element before it must be a down position. so up[i] = down[i-1] + 1; down[i] keeps the same with before.
16+
* If nums[i] < nums[i-1], that means it wiggles down. the element before it must be a up position. so down[i] = up[i-1] + 1; up[i] keeps the same with before.
17+
* If nums[i] == nums[i-1], that means it will not change anything becasue it didn't wiggle at all. so both down[i] and up[i] keep the same.
818
*/
919
public int wiggleMaxLength(int[] nums) {
1020
int len = nums.length;

0 commit comments

Comments
 (0)