Leetcode 1191. K-Concatenation Maximum Sum

Description:

Given an integer array arr and an integer k, modify the array by repeating it k times.
For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2].

Return the maximum sub-array sum in the modified array. Note that the length of the sub-array can be 0 and its sum in that case is 0.

As the answer can be very large, return the answer modulo 10^9 + 7.

Example 1:

Input: arr = [1,2], k = 3
Output: 9

Example 2:

Input: arr = [1,-2,1], k = 5
Output: 2

Example 3:

Input: arr = [-1,-2], k = 7
Output: 0

Constraints:

  • 1 <= arr.length <= 10^5
  • 1 <= k <= 10^5
  • -10^4 <= arr[i] <= 10^4

参考Maximum Sum Circular Subarray,可分为以下三种情况:

  1. 最大子数组和在输入数组的中间;
  2. 最大子数组和由输入数组的头尾部分组成;
  3. 最大子数组和跨越中间跨越多个输入数组。

其中,第三种情况可以看作是第二种情况的结果假设(k-2)*sum(arr),即子数组和大于0时,第三种情况时肯定大于第二种情况的。最终结果取上面三种情况种的最大值即可。

class Solution:
    def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:
        mod = pow(10, 9)+7
        # 计算最大子数组和以及数组和
        cur_max, total, max_sum = 0, 0, 0
        for num in arr:
            cur_max = max(cur_max+num, num)
            max_sum = max(max_sum, cur_max)
            total += num

        if k == 1:
            return max_sum%mod

        # 计算最大前缀和
        prefix_sum = 0
        cur_sum = 0
        for i in range(len(arr)):
            cur_sum += arr[i]
            prefix_sum = max(cur_sum, prefix_sum)

        #计算最大后缀和
        suffix_sum = 0
        cur_sum = 0
        for i in range(len(arr))[::-1]:
            cur_sum += arr[i]
            suffix_sum = max(cur_sum, suffix_sum)

        if total > 0:
            print(prefix_sum, suffix_sum, total)
            return max(((k-2)*total+prefix_sum+suffix_sum)%mod, max_sum%mod)
        else:
            print(prefix_sum, suffix_sum, total)
            return max((prefix_sum+suffix_sum)%mod, max_sum%mod)

参考资料

  1. https://leetcode.com/problems/k-concatenation-maximum-sum/
  2. https://leetcode.com/problems/k-concatenation-maximum-sum/discuss/382350/Java-Solution(Kadens-Algo)-with-Explanation
  3. https://leetcode.com/problems/k-concatenation-maximum-sum/discuss/382429/C%2B%2B-O(N)-time-O(1)-space-with-ExplanationPicture

类似题目:

  1. Maximum Sum Circular Subarray

原文地址:https://www.cnblogs.com/zhaoyinghe/p/12158096.html

时间: 2024-10-07 21:43:25

Leetcode 1191. K-Concatenation Maximum Sum的相关文章

Leetcode 1191 K-Concatenation Maximum Sum 动态规划

Leetcode 1191 K-Concatenation Maximum Sum 动态规划 题目描述 Given an integer array arr and an integer k, modify the array by repeating it k times. For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2]. Return the maximum s

[LeetCode] 689. Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maximize the sum of all 3*k entries. Return the result as a list of indices representing the starting p

LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays

原题链接在这里:https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/ 题目: In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maximize the sum

[Leetcode][Tree][Binary Tree Maximum Path Sum]

找书中权值和最大的路径,至少包含一个点. 有点类似LCA(最近公共祖先),树的问题关键是如何划分子问题,然后递归求解. 想到了可以返回两种结果,一个是单独路径的最大和,一种是子树的最大和,然后在求解的过程中不断的更新答案. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val

[LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个菲重叠子数组的最大和

In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maximize the sum of all 3*k entries. Return the result as a list of indices representing the starting p

maximum sum of a subarray with at-least k elements.

// Returns maximum sum of a subarray with at-least // k elements. static int maxSumWithK(int a[], int n, int k) { // maxSum[i] is going to store maximum sum // till index i such that a[i] is part of the // sum. int maxSum[] = new int [n]; maxSum[0] =

【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays

题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maximize the sum of all 3*k entries. Return the result as a list of indices representing the star

Leetcode 1031 Maximum Sum of Two Non-Overlapping Subarrays (滑动窗口)

Leetcode 1031 题目描述 Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray could occur before or after the M-le

Leetcode Week5 Maximum Sum Circular Subarray

Question Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C. Here, a circular array means the end of the array connects to the beginning of the array.  (Formally, C[i] = A[i] when 0 <= i