美文网首页
8.22 - hard - 86

8.22 - hard - 86

作者: 健时总向乱中忙 | 来源:发表于2017-08-23 03:13 被阅读0次

    446. Arithmetic Slices II - Subsequence

    一道dp的题目

    class Solution(object):
        def numberOfArithmeticSlices(self, A):
            """
            :type A: List[int]
            :rtype: int
            """
            total = 0
            dp = [collections.defaultdict(int) for item in A]
            # dp[i][j] stores the number of arithmetic slices (including those with only length 2) whose constant difference is j ending at i
            for i in range(len(A)):
                for j in range(i):
                    gap = A[i] - A[j]
                    dp[i][gap] += 1
                    if gap in dp[j]:
                        dp[i][gap] += dp[j][gap]
                        total += dp[j][gap]
            return total
            
    

    相关文章

      网友评论

          本文标题:8.22 - hard - 86

          本文链接:https://www.haomeiwen.com/subject/towrdxtx.html