当前位置:网站首页>leetcode:918. 环形子数组的最大和【逆向思维 + 最大子数组和】
leetcode:918. 环形子数组的最大和【逆向思维 + 最大子数组和】
2022-06-25 12:35:00 【白速龙王的回眸】
分析
这道题 如果不是环形的话就正常子数组就可以了 如果求出来的maxn是小于0,那全部负数,直接返回即可
否则 开始考虑有环的,就是一头一尾的,那么就需要中间最少,那么就是最小子数组和
然后用sum减掉就可以
ac code
class Solution:
def maxSubarraySumCircular(self, nums: List[int]) -> int:
# 无环:正常最大子数组和
maxn = -inf
now = 0
for num in nums:
now += num
maxn = max(maxn, now)
now = max(0, now)
if maxn < 0:
return maxn # 全是负数
# 有环:正常最小子数组和
minn = inf
now = 0
for num in nums:
now += num
minn = min(minn, now)
now = min(0, now)
# 有环无环中的最大值
return max(maxn, sum(nums) - minn)
总结
思维题
边栏推荐
- Configuring pytorch in win10 environment
- AGCO AI frontier promotion (6.25)
- [AI helps scientific research] fool drawing of loss curve
- 剑指Offer 第 2 天链表(简单)
- 爱可可AI前沿推介(6.25)
- [machine learning] what is machine learning?
- Capabilities required by architects
- Drawing cubes with Visio
- Uncover gaussdb (for redis): comprehensive comparison of CODIS
- Sword finger offer II 025 Adding two numbers in a linked list
猜你喜欢
剑指Offer 第 2 天链表(简单)
Sword finger offer II 032 Effective anagrams
[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box
關於數據在內存中的存儲下
药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质
20220620 interview reply
[visio] solving the fuzzy problem of parallelogram in word
Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream
关于数据在内存中的存储下
.NET in China - What's New in .NET
随机推荐
[machine learning] model and cost function
剑指Offer 第 2 天链表(简单)
CUDA error: unspecified launch failure
Back test of quantitative trading - example of futures CTA strategy (tqzfuturerenkoscalpingstrategy)
Capabilities required by architects
[flask tutorial] flask overview
Introduction to mongodb chapter 01 introduction to mongodb
@Scheduled implementation of scheduled tasks (concurrent execution of multiple scheduled tasks)
Qt鼠标跟踪
Three lines of code to simply modify the project code of the jar package
解析數倉lazyagg查詢重寫優化
KVM 脚本管理 —— 筑梦之路
Sword finger offer day 2 linked list (simple)
德国举行全球粮食安全团结会议
golang键盘输入语句scanln scanf代码示例
[pit avoidance refers to "difficult"] antd cascader implements new customized functions
剑指offer 第 3 天字符串(简单)
剑指 Offer II 028. 展平多级双向链表
J2EE从入门到入土01.MySQL安装
Stockage des données en mémoire