【leetcode】1191. K-Concatenation Maximum Sum
题目如下:
Given an integer array
arrand an integerk, modify the array by repeating itktimes.For example, if
arr = [1, 2]andk = 3then 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
0and its sum in that case is0.As the answer can be very large, return the answer modulo
10^9 + 7.Example 1:
Input: arr = [1,2], k = 3
Output: 9Example 2:
Input: arr = [1,-2,1], k = 5
Output: 2Example 3:
Input: arr = [-1,-2], k = 7
Output: 0Constraints:
1 <= arr.length <= 10^51 <= 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的更多相关文章
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
- 【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 ...
- 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】930. Binary Subarrays With Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 字典 相似题目 参考资料 日期 题目地址: ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- 【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 ...
- 【LeetCode】327. Count of Range Sum
题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusiv ...
随机推荐
- Smartform给文本绑定值
点击字段显示器, 然后把字段拖过去即可
- python--008文件处理
一.文件操作 1.打开文件,获得文件句柄,并将句柄赋值给一个变量 2.通过句柄对文件操作 3.关闭文件 f=open('sg',encoding='utf-8') da=f.read() print( ...
- Unity Shader 基础
推荐: https://www.cnblogs.com/nanwei/p/7277417.html 上面链接作者的整个系列都写的不错 https://www.cnblogs.com/nanwei/ca ...
- Windown Server 2008配置tomcat9虚拟路径
一.用途 用于保存项目运产生的文件 二.步骤 1.修改conf\下的web.xml <!-- 找到listings将false改为true --> <init-para ...
- 【一个蒟蒻的挣扎】单源最短路(Dijkstra)
赛前没啥时间好好解释了,还有三天2019CSP,大家加油啊!!! ヾ(◍°∇°◍)ノ゙ 背掉它就好啦!!! 我觉得我这一版打得还行就放上来了 #include<cstdio> #inclu ...
- wpf学习笔记(1)
wpf常用控件 0x01. 常用布局控件 1.Canvas 不会对子控件施加任何帮助,也不会施加任何限制 2.DockPanel 可以让子控件贴靠在自己四条边的任意一边 ,最后一个子控件充满剩余区域 ...
- MyBatis删除多个类型不一致或不在同一个对象中参数的记录
控制层中: // 根据店家id查找图书,已售数量要大于等于1才显示 List<SoldBook> sbList = shopService.getSoldBookByShopidAndBo ...
- const关键字 C与C++分析
1 C与C++的区别 1.1.C允许定义两个变量名相同的变量,而C++不允许. 在C语言中是允许定义两个名字相同的全局变量. 在C++中是不允许定义两个名字相同的全局变量. 测试代码: /* 编译环 ...
- 5.jQuery之栏切换
<style> * { margin: 0; padding: 0; } li { list-style-type: none; } .tab { width: 978px; margin ...
- 一分钟理解sku和spu
SPU SPU = Standard Product Unit (标准化产品单位) SPU是商品信息聚合的最小单位,是一组可复用.易检索的标准化信息的集合,该集合描述了一个产品的特性.通俗点讲,属性值 ...