题目如下:

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

解题思路:和最大的一段子数组,无外乎一下六种情况。

1.  0;

2. arr[i] ;

3. sum(arr) * k ;

4. sum(arr[i:j]) ,这种场景其实就是求最大字段和;

5.sum(arr[i:len(arr)]) + sum(arr[0:j]) ,需要满足k>1。这种场景是第一个arr的尾部的最大值加上第二个arr头部的最大值;

6.sum(arr[i:len(arr)]) + sum(arr[0:j]) + (k-2) * sum(arr) ,需要满足k>2。这种场景是第一个arr的尾部的最大值加上最后一个arr头部的最大值。

代码如下:

class Solution(object):
def kConcatenationMaxSum(self, arr, k):
"""
:type arr: List[int]
:type k: int
:rtype: int
"""
res = 0
left_max = []
amount = 0
min_sub_arr_sum = 0
max_arr_sum = 0
for i in arr:
res = max(i,res) # single item
amount += i
min_sub_arr_sum = min(min_sub_arr_sum, amount)
max_arr_sum = max(max_arr_sum,amount)
res = max(res, amount - min_sub_arr_sum)
left_max.append(max_arr_sum)
total_sum = amount
res = max(res,total_sum*k) # all
amount = 0
max_sub_arr_sum = -float('inf')
for i in range(len(arr) - 1, -1, -1):
amount += arr[i]
max_sub_arr_sum = max(max_sub_arr_sum,amount)
res = max(res,max_sub_arr_sum + left_max[-1])
if k > 2:
res = max(res,max_sub_arr_sum + left_max[-1] + (k-2)*total_sum)
return res % (10 ** 9 + 7)

【leetcode】1191. K-Concatenation Maximum Sum的更多相关文章

  1. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  2. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  3. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

  4. 【LeetCode】124. Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  5. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  6. 【LeetCode】930. Binary Subarrays With Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 字典 相似题目 参考资料 日期 题目地址: ...

  7. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  8. 【leetcode】Substring with Concatenation of All Words (hard) ★

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  9. 【LeetCode】327. Count of Range Sum

    题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusiv ...

随机推荐

  1. jmeter业务建模中遇到的问题

    1.jmeter函数助手中的jexl3函数,不支持${__jexl3(15<${__Random(1,100,)}<36,)}这种写法,须这样写${__jexl3(15<${__Ra ...

  2. TCP/IP笔记——TCP特点、首部格式、滑动窗口

    这次总结一下TCP相关的知识. TCP主要特点 面向连接:在通信前必须建立连接(只是逻辑上存在,而不是物理连接) 只能有两个端点:即只能一对一通信(所以通常p2p是用UDP实现的) 提供可靠交付服务: ...

  3. java中enum----枚举的学习(更新中)

    package com.hdmaxfun; import java.util.Scanner; import com.icpc.Icpm; import java.util.HashMap; impo ...

  4. linux是什么与如何学习(三)

     1.1Linux是什么 Linux是在计算机上面运作的,所以说是一组软件. 1.2 Linux是什么?操作系统还是应用程序? 计算机主机是由一套硬件所组成的,为了有效的控制这些硬件资源,于是就有了操 ...

  5. mybatis generator 源码修改

    项目中使用mybatis + 通用mapper,用mybatis generator生成代码时有些不方便,参考了网上的一些例子,修改mybatis genrerator的源码. 首先,下载mybati ...

  6. 微服务理论之二:面向微服务架构与传统架构、SOA对比,以及云化对比

    一.Monolith 网上对Microservice进行介绍的文章常常以Monolith作为开头,我也不会例外.原因是,知道了Monolith的不便之后才能更容易地理解Microservice架构模式 ...

  7. WordPress 在Ubuntu下安装插件、主题输入FTP后无法创建目录

    最近自己在搞基于lnmp+wordpress的个人博客, 一切都就绪后,想改变下自己的主题,然后去Wordpress里面内置的主题安装下载的时候,提示:无法创建目录! 一般我们在Ubuntu系统上面安 ...

  8. MySQL总结(4)

    insert 数据的插入 INSERT插入数据

  9. MongoDB和Redis的区别

    1).内存管理机制 a.Redis的数据全部存储在内存当中,会定期写入到磁盘当中,当内存不够用时, 可以选择指定的LRU(最近最少使用算法)的算法删除数据: b.MongoDB数据存在内存,有Linu ...

  10. C#实现Web链接启动应用程序

    C#实现Web链接启动应用程序 最近需要配合Web端实现用户点击链接来启动应用程序并且需要能够传参数给应用程序. 那么就可以使用注册表来实现这个功能 编写注册表可以在软件安装程序中加入,也可以在软件启 ...