Skip to content

Commit

Permalink
Create left-and-right-sum-differences.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Feb 27, 2023
1 parent afd9504 commit 7e4604c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Python/left-and-right-sum-differences.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7e4604c

Please sign in to comment.