Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Approach: maintain a product prefix. However you do not do the same for product suffix, for that what you got to do is
- # start calcualting answer backwards and as you go from end to start calculate product suffix.
- class Solution:
- def productExceptSelf(self, nums: List[int]) -> List[int]:
- product = 1
- prefix = [1 for i in range(len(nums))]
- for i in range(1, len(nums)):
- prefix[i] = prefix[i-1]*nums[i-1]
- suffix = 1
- ans = [1 for i in range(len(nums))]
- for i in range(len(nums)-1, -1, -1):
- ans[i] = prefix[i]*suffix
- suffix = suffix*nums[i]
- return ans
Advertisement
Add Comment
Please, Sign In to add comment