当前位置:网站首页>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)
总结
思维题
边栏推荐
- CUDA error: unspecified launch failure
- Seven competencies required by architects
- The editor is used every day. What is the working principle of language service protocol?
- 量化交易之回测篇 - 期货CTA策略策略(TQZFutureRenkoWaveStrategy)
- Where is it safe to open an account for buying funds? Please give me your advice
- 提高排名的 15 个基本 SEO 技巧
- [machine learning] model and cost function
- Storage related contents of data in memory
- Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation
- Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"
猜你喜欢
![[flask tutorial] flask overview](/img/e8/d85ac54f3a9eb3b1ab31852761154c.jpg)
[flask tutorial] flask overview

It is extraordinary to make a move, which is very Oracle!

golang键盘输入语句scanln scanf代码示例

Shenzhen mintai'an intelligent second side_ The first offer of autumn recruitment

There is a problem with the date when MySQL imports and exports data to excel

Django framework - caching, signaling, cross site request forgery, cross domain issues, cookie session token

1251- client does not support authentication protocol MySQL error resolution

关于数据在内存中存储的相关例题

An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down

Sword finger offer II 025 Adding two numbers in a linked list
随机推荐
Summer Ending
Common colors for drawing
爱可可AI前沿推介(6.25)
剑指 Offer 04. 二维数组中的查找
汇编标志位相关知识点(连)
Update PIP & Download jupyter Lab
1024 hydrology
[pit avoidance means "difficult"] the antd form dynamic form is deleted, and the first line is displayed by default
[pit avoidance means "difficult"] to realize editable drag and drop sorting of protable
与生产环境中的 console.log 说再见
Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream
MySQL 学习笔记
Storage related contents of data in memory
德国举行全球粮食安全团结会议
数据在内存中的存储相关内容
Assemble relevant knowledge points of flag bit (connected)
Back test of quantitative trading - example of futures CTA strategy (tqzfuturerenkoscalpingstrategy)
Possible problems when idea encounters errors occurred while compiling module (solved)
[flask tutorial] flask development foundation and introduction
关于结构体,枚举,联合的一些知识