Maximum Absolute Sum of Any Subarray – Leetcode 1749 – Python

学習

🚀 https://neetcode.io/ – A better way to prepare for Coding Interviews

🧑‍💼 LinkedIn: https://www.linkedin.com/in/navdeep-singh-3aaa14161/
🐦 Twitter: https://twitter.com/neetcode1

⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf

Problem Link: https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/description/

0:00 – Intro
1:00 – Understand Problem
5:55 – Drawing Explanation
11:49 – Dry Run
15:28 – Coding Explanation

leetcode 1749

#neetcode #leetcode #python

コメント

  1. @samyakmahapatra9154 より:

    I tried a solution and it worked pls lemme know this is always correct or not so here it goes

    Max absolute sum of subarray,for this first we can find max sum of subarray in the actual array.But note we can have max abs sum when we take lot of negatives as well or when lot of negatives over some positives but this particular abs sum can be written as max subarray sum of the array having negative elements as original array. So for final ans we just need to compare both the sums.
    vectortemp;
    for(int i=0;i

  2. @YashGupta-ty2hn より:

    A much easier solution is kadane’s algo
    class Solution:
    def maxAbsoluteSum(self, nums: List[int]) -> int:
    psum , nsum = 0, 0
    ans = 0
    for num in nums:
    psum += num
    nsum += num
    if psum < 0: psum = 0 if nsum > 0:
    nsum = 0
    ans = max(ans, abs(nsum), psum)
    return ans

  3. @F1REBALL-XL5 より:

    def maxAbsoluteSum(self, nums: List[int]) -> int:
    curr_pos, max_pos = nums[0], nums[0]
    curr_neg, max_neg = -nums[0], -nums[0]
    for num in nums[1:]:
    curr_pos, curr_neg = max(curr_pos + num, num), max(curr_neg-num, -num)
    max_pos, max_neg = max(max_pos, curr_pos), max(max_neg, curr_neg)
    return max(max_pos, max_neg)

タイトルとURLをコピーしました