2574. Left and Right Sum Differences
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 87% Topics: Array, Prefix Sum
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
# prefix sum
class Solution(object):
def leftRightDifference(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
total = sum(nums)
result = []
curr = 0
for x in nums:
curr += x
result.append(abs((curr-x)-(total-curr)))
return result
Solution from kamyu104/LeetCode-Solutions · MIT