All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : February 26, 2025
Last updated : February 26, 2025
Related Topics : Array, Dynamic Programming
Acceptance Rate : 71.57 %
class Solution:
def maxAbsoluteSum(self, nums: List[int]) -> int:
# kadane's but for both min and max
output = abs(nums[0])
minn, maxx = nums[0], nums[0]
return max([(output := max(output, (maxx := max(maxx + num, num)), -(minn := min(minn + num, num)))) for num in nums[1:]] + [output])
class Solution:
def maxAbsoluteSum(self, nums: List[int]) -> int:
# kadane's but for both min and max
output = abs(nums[0])
max_sub_arr = nums[0]
min_sub_arr = nums[0]
for num in nums[1:] :
max_sub_arr = max(max_sub_arr + num, num)
min_sub_arr = min(min_sub_arr + num, num)
output = max(output, max_sub_arr, -min_sub_arr)
return output