Skip to content

Commit b63ddc9

Browse files
committed
[DP] Add a solution to Wiggle Subsequence
1 parent 943046e commit b63ddc9

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

DP/WiggleSubsequence.swift

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/wiggle-subsequence/
3+
* Primary idea: Classic Dynamic Programming, two variables/arries to track up or down status
4+
* Time Complexity: O(n), Space Complexity: O(1)
5+
*/
6+
7+
class WiggleSubsequence {
8+
func wiggleMaxLength(_ nums: [Int]) -> Int {
9+
guard nums.count >= 2 else {
10+
return nums.count
11+
}
12+
13+
var up = 1, down = 1
14+
15+
for i in 1..<nums.count {
16+
if nums[i] > nums[i - 1] {
17+
up = down + 1
18+
} else if nums[i] < nums[i - 1] {
19+
down = up + 1
20+
}
21+
}
22+
23+
return max(up, down)
24+
}
25+
}

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 242 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 243 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -200,6 +200,7 @@
200200
[Edit Distance](https://leetcode.com/problems/edit-distance/)| [Swift](./DP/EditDistance.swift)| Hard| O(mn)| O(mn)|
201201
[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/)| [Swift](./DP/CombinationSumIV.swift)| Medium| O(2^n)| O(n)|
202202
[Triangle](https://leetcode.com/problems/triangle/)| [Swift](./DP/Triangle.swift)| Medium| O(2^n)| O(m)|
203+
[Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/)| [Swift](./DP/WiggleSubsequence.swift)| Medium| O(n)| O(1)|
203204
[Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)| [Swift](./DP/WildcardMatching.swift)| Hard| O(mn)| O(mn)|
204205
[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)| [Swift](./DP/RegularExpressionMatching.swift)| Hard| O(mn)| O(mn)|
205206
[Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/)| [Swift](./DP/MinimumWindowSubsequence.swift)| Hard| O(mn)| O(mn)|

0 commit comments

Comments
 (0)