Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7ba618a

Browse files
Product Of Array Except Self
1 parent 4e68d15 commit 7ba618a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''Leetcode - https://leetcode.com/problems/product-of-array-except-self/'''
2+
'''
3+
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
4+
5+
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
6+
7+
You must write an algorithm that runs in O(n) time and without using the division operation.
8+
9+
Input: nums = [1,2,3,4]
10+
Output: [24,12,8,6]
11+
'''
12+
13+
# Solution
14+
def productExceptSelf(nums):
15+
res = [1] * (len(nums))
16+
17+
prefix = 1
18+
for i in range(len(nums)):
19+
res[i] = prefix
20+
prefix *= nums[i]
21+
postfix = 1
22+
for i in range(len(nums) - 1, -1, -1):
23+
res[i] *= postfix
24+
postfix *= nums[i]
25+
return res
26+
27+
# T:O(N)
28+
# S:O(1)

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ Check the notes for the explaination - [Notes](https://stingy-shallot-4ea.notion
2828
- [x] [Range Sum Query - Immutable](Dynamic-Programming/303-Range-Sum-Query-Immutable.py)
2929
- [x] [Counting Bits](Dynamic-Programming/338-Counting-Bits.py)
3030

31+
- [x] [Arrays](Arrays)
32+
- [x] [Contains Duplicate](Arrays/217-Contains-duplicate.py)
33+
- [x] [Product Of Array Except Self](Arrays/238-product-of-array-except-self.py)
34+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /