Skip to content

Commit 5daf3bd

Browse files
committed
Add solution for product array except self
1 parent aea24c9 commit 5daf3bd

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed

array/product-array/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 238. Product of Array Except Self
2+
3+
## Description
4+
See https://leetcode.com/problems/product-of-array-except-self/description/
5+
6+
## Problem
7+
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]`.
8+
9+
The product of any prefix or suffix of `nums` is guaranteed to fit in a 32-bit integer.
10+
11+
You must write an algorithm that runs in `O(n)` time and without using the division operation.
12+
13+
## Example 1
14+
15+
```
16+
Input: nums = [1,2,3,4]
17+
Output: [24,12,8,6]
18+
```
19+
20+
## Example 2
21+
22+
```
23+
Input: nums = [-1,1,0,-3,3]
24+
Output: [0,0,9,0,0]
25+
```
26+
27+
## Constraints
28+
29+
```
30+
2 <= nums.length <= 105
31+
-30 <= nums[i] <= 30
32+
The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer.
33+
```
34+
35+
**Follow up:** Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)

array/product-array/productarray.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def product_array(nums: list[int]) -> list[int]:
2+
n = len(nums)
3+
output = [1] * n
4+
5+
left = 1
6+
for i in range(n):
7+
output[i] *= left
8+
left *= nums[i]
9+
10+
right = 1
11+
for i in range(n - 1, -1, -1):
12+
output[i] *= right
13+
right *= nums[i]
14+
15+
return output
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from productarray import product_array
2+
3+
4+
def test_example_1():
5+
nums = [1, 2, 3, 4]
6+
assert product_array(nums) == [24, 12, 8, 6]
7+
8+
9+
def test_example_2():
10+
nums = [5, 6, 2, 3]
11+
assert product_array(nums) == [36, 30, 90, 60]
12+
13+
14+
def test_example_3():
15+
nums = [1, 2, 3, 4, 5]
16+
assert product_array(nums) == [120, 60, 40, 30, 24]
17+
18+
19+
def test_example_4():
20+
nums = [10, 3, 5, 6, 2]
21+
assert product_array(nums) == [180, 600, 360, 300, 900]
22+
23+
24+
def test_example_5():
25+
nums = [1, 1, 1, 1]
26+
assert product_array(nums) == [1, 1, 1, 1]

array/reverse-vowels/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
# 605. Can Place Flowers
1+
# 345. Reverse Vowels of a String
22

33
## Description
4-
See https://leetcode.com/problems/can-place-flowers/description/
4+
See https://leetcode.com/problems/reverse-vowels-of-a-string/description/
55

66
## Problem
7-
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
7+
Given a string `s`, reverse only all the vowels in the string and return it.
88

9-
Given an integer array `flowerbed` containing `0`'s and `1`'s, where `0` means empty and `1` means not empty, and an integer `n`, return `true` if `n` new flowers can be planted in the `flowerbed` without violating the no-adjacent-flowers rule and `false` otherwise.
9+
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
1010

1111
## Example 1
1212

1313
```
14-
Input: flowerbed = [1,0,0,0,1], n = 1
15-
Output: true
14+
Input: s = "IceCreAm"
15+
Output: "AceCreIm"
16+
Explanation:
17+
The vowels in s are ['I', 'e', 'e', 'A']. On reversing the vowels, s becomes "AceCreIm".
1618
```
1719

1820
## Example 2
1921

2022
```
21-
Input: flowerbed = [1,0,0,0,1], n = 2
22-
Output: false
23+
Input: s = "leetcode"
24+
Output: "leotcede"
2325
```
2426

2527
## Constraints
2628

2729
```
28-
1 <= flowerbed.length <= 2 * 104
29-
flowerbed[i] is 0 or 1.
30-
There are no two adjacent flowers in flowerbed.
31-
0 <= n <= flowerbed.length
30+
1 <= s.length <= 3 * 105
31+
s consist of printable ASCII characters.
3232
```

0 commit comments

Comments
 (0)