今際の国の呵呵君
Sunday, September 17, 2017
[LeetCode]Arithmetic Slices II - Subsequence
用DP[i][j]表示在i处结尾差为j的等差数列的数量(长度大于等于2),我们有递推公式:
DP[i][j] = DP[i][j] + k(k为nums[i] - nums[m] == j的数量, m < i, 即以nums[i]结尾的差为j的pair的数目)
我们只需统计在所有idx结尾的等差数列的数目就能知道总数,时间复杂度O(n^2),空间复杂度O(n^2)(对于nums[i],其和之前所有数的差我们都会存储)。代码如下:
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment