当前位置:网站首页>leetcode:918. Maximum sum of circular subarray [reverse thinking + maximum subarray sum]
leetcode:918. Maximum sum of circular subarray [reverse thinking + maximum subarray sum]
2022-06-25 13:20:00 【Review of the white speed Dragon King】

analysis
This question If it is not a ring, the normal subarray is OK If you find out maxn Less than 0, All negative numbers , Just go back
otherwise Start thinking about having rings , It's one end at a time , Then you need the least in the middle , So it's the minimal array and
And then use sum Just subtract it
ac code
class Solution:
def maxSubarraySumCircular(self, nums: List[int]) -> int:
# acyclic : Normal maximum subarray and
maxn = -inf
now = 0
for num in nums:
now += num
maxn = max(maxn, now)
now = max(0, now)
if maxn < 0:
return maxn # All negative numbers
# Ring : Normal minimal arrays and
minn = inf
now = 0
for num in nums:
now += num
minn = min(minn, now)
now = min(0, now)
# The maximum value in a ring without a ring
return max(maxn, sum(nums) - minn)
summary
Thinking questions
边栏推荐
- MySQL 学习笔记
- Online service emergency research methodology
- 药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质
- J2EE from entry to earth 01 MySQL installation
- [pit avoidance means "difficult"] to realize editable drag and drop sorting of protable
- Sword finger offer 04 Find in 2D array
- Maui's learning path (II) -- setting
- 剑指 Offer II 029. 排序的循环链表
- Accidentally modify or delete the system variable path to restore
- 關於數據在內存中的存儲下
猜你喜欢
随机推荐
剑指offer 第 3 天字符串(简单)
Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation
Custom vertical table
Confusion caused by the ramp
[visio] solving the fuzzy problem of parallelogram in word
À propos du stockage des données en mémoire
Detailed explanation of string operation functions and memory functions
Sword finger offer day 3 string (simple)
It's an artifact to launch a website in a few minutes
OpenStack学习笔记之-Nova组件深入了解
[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box
There is a problem with the date when MySQL imports and exports data to excel
Storage related contents of data in memory
《MongoDB入门教程》第01篇 MongoDB简介
Common colors for drawing
关于扫雷的简易实现
[AI helps scientific research] fool drawing of loss curve
KVM 脚本管理 —— 筑梦之路
Sword finger offer II 029 Sorted circular linked list
解析数仓lazyagg查询重写优化








