题目如下:

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. C# 二维码、条形码生成

    1.工具类:BarCodeHelper(条码生成类),二维码生成类(QRCodeHelper) 2.BarCodeHelper(条码生成类)代码: public class BarCodeHelper ...

  2. 封装cookie,自定义过期时间,domain,path

    在使用Cookie进行存储的时候,遇到了许多不可思议的bug,特地标识出来,以作总结. 是这样一个项目,登录是放在官网进行操作的,而登录进入的是后台,后台和官网属于同一域名的不同目录,那么常规进行co ...

  3. cocos2dx[3.2](1) 浅析cocos2dx3.2引擎目录

    3.x的引擎目录与2.x的引擎目录的差别是非常大的.3.x主要是将引擎的各个文件按照用途进行了分类,使得引擎目录结构更加清晰了. 从目录中我们主要了解一下以下几个文件: 文件名 说明 build 官方 ...

  4. python学习之模块-模块(五)

    5.10 包 5.10.1 包的概念 [官网解释] Packages are a way of structuring Python's module namespace by using " ...

  5. Centos7搭建Open-ldap

    OpenLDAP是轻型目录访问协议(Lightweight Directory Access Protocol,LDAP)的自由和开源的实现,可用于实现统一认证 一.安装环境 安装方式:yum 系统: ...

  6. Node.js使用redis进行订阅发布管理

    redis NPM 官方介绍地址:https://www.npmjs.com/package/redis let redis = require('redis'); let subscriber; l ...

  7. JS中的 map, filter, some, every, forEach, for in, for of 用法总结和区别

    JS中的 map, filter, some, every, forEach, for in, for of 用法总结和区别  :https://blog.csdn.net/hyupeng1006/a ...

  8. Maven 相关功能介绍

    一: Maven环境隔离

  9. mysql-5.7.25安装以及使用

    1. wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz 2. ...

  10. ELK的搭建以及使用

    一.架构如图: 二.工作机制: 在需要收集日志的应用上安装filebeat(需要修改配置文件,配置文件稍后介绍),启动filebeat后,会收集该应用的日志推送给redis,然后logstash从re ...