【leetcode】1191. K-Concatenation Maximum Sum
题目如下:
Given an integer array
arr
and an integerk
, modify the array by repeating itk
times.For example, if
arr = [1, 2]
andk = 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 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^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的更多相关文章
- 【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 ...
随机推荐
- 使用FreeHttp任意篡改http报文 (FreeHttp使用及实现说明)
本文转自:https://www.cnblogs.com/lulianqi/p/10428551.html 前言 FreeHttp是一个Fiddler插件借助FreeHttp您可按照您自己的设定修改请 ...
- Ubuntu虚拟机共享文件夹的1234
第一 在虚拟机内添加路径 进入虚拟机软件,点开工具栏上方虚拟机,点击设置,选择选项,查看共享文件夹,点击添加,下一步 第二: 第三 点击启用此共享 点击完成 第四 查看共享的文件 在mnt里可以看到S ...
- [ASP.NET] 数据绑定以及Container.DataItem的具体分析 [转]
灵活的运用数据绑定操作 绑定到简单属性:<%#UserName%> 绑定到集合:<asp:ListBox id="ListBox1" ...
- [总集] LOJ 分块1 – 9
目录 分块9题 出题人hzw的解析 数列分块入门 1 修改:区间加 查询:单点值查询 代码 数列分块入门 2 修改:区间加 查询:区间排名 代码 数列分块入门 6 修改:单点插入 查询:单点值 代码 ...
- python selenium中Excel数据维护(二)
接着python里面的xlrd模块详解(一)中我们我们来举一个实例: 我们来举一个从Excel中读取账号和密码的例子并调用: ♦1.制作Excel我们要对以上输入的用户名和密码进行参数化,使得这些数据 ...
- 数组遍历方法forEach 和 map 的区别
数组遍历方法forEach 和 map 的区别:https://www.cnblogs.com/sticktong/p/7602783.html
- inline void 树状数组神奇感悟【雾
才发现扫描线可以用树状数组搞... 致远星患者 (另外根据这篇博文的内容怎么越来越感觉自己往 PJ 入门靠拢了...) 还有一点,咱把树状数组当做线段树来康的话其实一切都会很清晰,这个来张四合一的图: ...
- php前台表单限制PHP上传大小
在php文件上传时候,一般我都认为考虑php.ini配置修改文件上传大小,还后台控制上传大小,这里教你php前台表单限制PHP上传大小 <form action="http://www ...
- P1397 [NOI2013]矩阵游戏
传送门 首先显然可以矩乘快速幂然后 $T$ 飞 看一眼题解发现因为这一题矩阵的特殊性所以可以对矩阵的次数欧拉降幂 然而我并不懂证明,所以我选择暴力乱搞的做法 十进制快速幂,然后注意一下常数,还有矩阵乘 ...
- Timetable CodeForces - 946D (区间dp)
大意: n天, 每天m小时, 给定课程表, 每天的上课时间为第一个1到最后一个1, 一共可以逃k次课, 求最少上课时间. 每天显然是独立的, 对每天区间dp出逃$x$次课的最大减少时间, 再对$n$天 ...