diff --git a/Python/left-and-right-sum-differences.py b/Python/left-and-right-sum-differences.py new file mode 100644 index 0000000000..90b469fe2e --- /dev/null +++ b/Python/left-and-right-sum-differences.py @@ -0,0 +1,17 @@ +# Time: O(n) +# Space: O(1) + +# prefix sum +class Solution(object): + def leftRigthDifference(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