本文主要是介绍Leetcode 1749. Maximum Absolute Sum of Any Subarray,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
**解析:**Version 1,分别求连续子数组的最大值与最小值,然后取二者绝对值较大的一个即可。
- Version 1
class Solution:def maxAbsoluteSum(self, nums: List[int]) -> int:n = len(nums)pos = 0neg = 0maximum = 0for i in range(n):pos += nums[i]neg += nums[i]maximum = max(maximum, abs(pos), abs(neg))pos = max(0, pos)neg = min(0, neg)return maximum
Reference
- https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/
这篇关于Leetcode 1749. Maximum Absolute Sum of Any Subarray的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!