Leetcode 1749. Maximum Absolute Sum of Any Subarray

Posted SnailTyan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = 0
        neg = 0
        maximum = 0
        for 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

  1. https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/

以上是关于Leetcode 1749. Maximum Absolute Sum of Any Subarray的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode --- 1614. Maximum Nesting Depth of the Parentheses 解题报告

LeetCode --- 1614. Maximum Nesting Depth of the Parentheses 解题报告

[Leetcode] Maximum Subarray

[LeetCode]Maximum Subarray

#Leetcode# 53. Maximum Subarray

LeetCode 53. Maximum Subarray