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 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

解题思路

首先,定义新方法maxSum(k)。根据k==1时的解题方法,循环k次即可。但不符合时间复杂度要求。以下是优化方法。
当 len(arr) == 0 时,返回0。
当 k<3 时,可根据k直接求得结果。
当 k<=3 时,记数组arr的总和为sum
当 sum <= 0 时,结果有maxSum(1)和maxSum(2)两种情况;
当 sum > 0 时, 有 maxSum(2) + (k-2)*sum.

Java代码
class Solution {
    public int kConcatenationMaxSum(int[] arr, int k) {
        if ( arr.length == 0 || arr == null ) return 0;
        if (k < 3) return (int) (maxSum(arr, k)%(1e9+7));

        long sum = 0;  for(int num:arr)  sum += num;
        long ans = maxSum(arr, 2);
        return (int) ((ans + (sum>0 ? sum:0) * (k-2))%(1e9+7));
    }
    private long maxSum(int[] arr, int k){
        long sum=0, ans=0;
        for(int i=0; i<k; i++){
            for(int num : arr){
                sum = Math.max(0, sum+=num);
                ans = Math.max(sum, ans);
            }
        }
        return ans;
    }
}
Python代码
class Solution:
    def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:
        def maxSum(arr, res = 0, cur = 0):
            for num in arr:
                res = max(0, res+num)
                cur = max(cur, res)
            return cur
        return ( (k-2)*max(sum(arr), 0) + maxSum(arr*2) )%(10**9+7) if k>1 else maxSum(arr)%(10**9+7)

感谢 花花酱

原文地址:https://www.cnblogs.com/willwuss/p/12241983.html

时间: 2024-11-01 19:45:10

Leetcode 1191 K-Concatenation Maximum Sum 动态规划的相关文章

[ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33363   Accepted: 10330 Description Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below: Your task is to calculate d(A). Input The input consists o

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. Not

[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] =

动态规划——Maximum Sum of 3 Non-Overlapping Subarrays

这个题对我来说真的是相当难的题目了,严格来讲可能不算是个动态规划的题目,但这个题目对类似的划分多个非重叠连续子区间的问题提供了一个很好解决方案 这个题目需要找三个非重叠的连续子区间,通过维护两个数组将第一个和第三个子区间可能的开始pos记录下来,在中间那个子区间开始的pos遍历时限制其边界范围,根据长度能恰到好处的将三个子区间划分成非重叠的,还使用了集合相减代替累加这样比较简单好用的技巧 下面提供代码: 1 class Solution { 2 public int[] maxSumOfThre

【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