【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 ...
随机推荐
- scrapy 正则汉字的提取方法
[\u4E00-\u9FA5]
- C语言作业11
问题 答案 这个作业属于那个课程 C语言程序设计 这个作业要求在哪里 https://www.cnblogs.com/galen123/p/11996995.html 我在这个课程的目标是 在学好C语 ...
- etcd租约机制
新建租约 新建一个过期时间为120s的租约 # etcdctl lease grant lease 018f6d7bb11aba0d granted with TTL(120s) 查看新建的租约信息 ...
- MySQL5.6解压版服务无法启动—系统错误1067
记录一下今天被坑了一下午的BUG 就从半个月前说起吧 当时....................................................................... ...
- 手写数字识别 卷积神经网络 Pytorch框架实现
MNIST 手写数字识别 卷积神经网络 Pytorch框架 谨此纪念刚入门的我在卷积神经网络上面的摸爬滚打 说明 下面代码是使用pytorch来实现的LeNet,可以正常运行测试,自己添加了一些注释, ...
- php中文网--JavaScript
PHP中文网:http://www.php.cn/course/18.html 常用的两个客户端输出方法 document.write("你好呀js"); 描述:在网页的<b ...
- Makefile中include、-include、sinclude
include.-include.sinclude使用 在 Makefile 使用 include 关键字可以把别的 Makefile 包含进来,这很像 C 语言的#include,被包含的文件会原模 ...
- linux tricks 之 ALIGN解析.
------------------------------------------- 本文系作者原创, 欢迎大家转载! 转载请注明出处:netwalker.blog.chinaunix.net -- ...
- PythonError解决方案
# Pip Error pip install * error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visua ...
- java 工具类使用
BigDecimalUtil 金额计算工具类 import java.math.BigDecimal; public class BigDecimalUtil { private BigDecimal ...