Leetcode 1191/5191 K 次串联后最大子数组之和

class Solution:
    def kConcatenationMaxSum(self, arr, k):
        """
        :type arr: List[int]
        :type k: int
        :rtype: int
        """
        res=0
        most=0
        n = len(arr)
        for i in range(0, n*2):
            x = arr[i%n]
            most=max(most+x, x)
            res=max(res, most)
        total=sum(arr)
        for _ in range(k-2):
            res=max(res+total, res)
        return res % (10**9+7)

 

你可能感兴趣的:(python,算法,leetcode)