当前位置:网站首页>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)
总结
思维题
边栏推荐
- 中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队
- And console Log say goodbye
- 二叉树之_哈夫曼树_哈弗曼编码
- KDD 2022 | GraphMAE:自监督掩码图自编码器
- Conway's law can not be flexibly applied as an architect?
- CUDA error: unspecified launch failure
- C # switch between Chinese and English input methods
- Shenzhen mintai'an intelligent second side_ The first offer of autumn recruitment
- Which Chinese virtual human is better? Sullivan, IDC: Xiaobing Baidu Shangtang ranks in the first echelon
- Germany holds global food security Solidarity Conference
猜你喜欢

[pit avoidance refers to "difficult"] antd cascader implements new customized functions
![[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1](/img/36/167397ce61240036c865dd99463f1b.jpg)
[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1

Update PIP & Download jupyter Lab

Sword finger offer II 028 Flatten multi-level bidirectional linked list

爱可可AI前沿推介(6.25)

Sword finger offer day 3 string (simple)

揭秘GaussDB(for Redis):全面对比Codis

几分钟上线一个网站 真是神器

Serenvlt first met

It's an artifact to launch a website in a few minutes
随机推荐
Sword finger offer 04 Find in 2D array
Alibaba stability fault emergency handling process
Summary of leetcode linked list problem solving skills
解析数仓lazyagg查询重写优化
[machine learning] model and cost function
Another night when visdom crashed
剑指offer 第 3 天字符串(简单)
汇编标志位相关知识点(连)
J2EE从入门到入土01.MySQL安装
三行代码简单修改jar包的项目代码
QT display ffmpeg decoded pictures
Update PIP & Download jupyter Lab
[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box
ByteDance dev better technology salon is coming! Participate in the activity to win a good gift, and sign up for free within a limited time!
[visio] solving the fuzzy problem of parallelogram in word
À propos du stockage des données en mémoire
515. Find Largest Value in Each Tree Row
字符串入门十八讲合集四
Restful and RPC
Back test of quantitative trading - tqzfuturerenkowavestrategy