15% off all plans & roadmaps Use code
See plansLeetCode 1749 · Solution & editorial
You are given an integer array nums. The absolute sum of a subarray [numsl, numsl+1, ..., numsr-1, numsr] is abs(numsl + numsl+1 + ... + numsr-1 + numsr).
Maximum Absolute Sum of Any Subarray is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Kadane's Algorithm for Max/Min Subarray) subpattern. The pattern covers fibonacci, Kadane, knapsack, LCS, LIS, edit distance, grid paths, and word break
Master Kadane's algorithm for finding maximum (or minimum) subarray sum in O(n) time.
The reference solution runs in O(n) time and O(1) space.
Maximum Absolute Sum of Any Subarray has been reported in interviews at nan.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Maximum Absolute Sum of Any Subarray is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Kadane's Algorithm for Max/Min Subarray) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.