Skip to content

Commit bef1316

Browse files
committed
[Array] Optimize the solution to Product of Array Except Self
[DP] Optimize code style to Unique Paths II
1 parent edf80cc commit bef1316

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

Array/ProductExceptSelf.swift

+10-29
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,26 @@
22
* Question Link: https://leetcode.com/problems/product-of-array-except-self/
33
* Primary idea: Use two arrays to hold multiplication result from left and right sides
44
* while iterating the original array
5-
* Time Complexity: O(n), Space Complexity: O(n)
5+
* Time Complexity: O(n), Space Complexity: O(1)
66
*/
77

88
class ProductExceptSelf {
9-
func productExceptSelf(nums: [Int]) -> [Int] {
10-
var res = [Int]()
11-
9+
func productExceptSelf(_ nums: [Int]) -> [Int] {
10+
var res = Array(repeating: 1, count: nums.count)
11+
var right = 1
12+
1213
guard nums.count > 0 else {
1314
return res
1415
}
15-
16-
let left = _initLeft(nums)
17-
let right = _initRight(nums)
18-
19-
for i in 0..<nums.count {
20-
res.append(left[i] * right[i])
21-
}
22-
23-
return res
24-
}
2516

26-
private func _initLeft(nums: [Int]) -> [Int] {
27-
var left = [Int]()
28-
left.append(1)
29-
3017
for i in 1..<nums.count {
31-
left.append(left[i - 1] * nums[i - 1])
18+
res[i] = res[i - 1] * nums[i - 1]
3219
}
33-
34-
return left
35-
}
36-
37-
private func _initRight(nums: [Int]) -> [Int] {
38-
var right = Array(count: nums.count, repeatedValue: 1)
39-
40-
for i in (nums.count - 2).stride(through: 0, by: -1) {
41-
right[i] = right[i + 1] * nums[i + 1]
20+
for i in (0..<nums.count).reversed() {
21+
res[i] = right * res[i]
22+
right = right * nums[i]
4223
}
4324

44-
return right
25+
return res
4526
}
4627
}

DP/UniquePathsII.swift

+20-11
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,37 @@
55
*/
66

77
class UniquePathsII {
8-
func uniquePathsWithObstacles(obstacleGrid: [[Int]]) -> Int {
8+
func uniquePathsWithObstacles(_ obstacleGrid: [[Int]]) -> Int {
99
let m = obstacleGrid.count
10+
guard m > 0 else {
11+
return 0
12+
}
13+
1014
let n = obstacleGrid[0].count
15+
guard n > 0 else {
16+
return 0
17+
}
1118

12-
var pathNums = Array(count: m, repeatedValue: Array(count: n, repeatedValue: 0))
13-
return _helper(&pathNums, m - 1, n - 1, obstacleGrid)
19+
var dp = Array(repeating: Array(repeating: -1, count: n), count: m)
20+
21+
return help(m - 1, n - 1, &dp, obstacleGrid)
1422
}
1523

16-
func _helper(inout _ pathNums: [[Int]], _ m: Int, _ n: Int, _ obstacleGrid: [[Int]]) -> Int {
17-
// termination
18-
if m < 0 || n < 0 || obstacleGrid[m][n] == 1 {
24+
fileprivate func help(_ m: Int, _ n: Int, _ dp: inout [[Int]], _ obstacleGrid: [[Int]]) -> Int {
25+
if m < 0 || n < 0 {
26+
return 0
27+
}
28+
if obstacleGrid[m][n] == 1 {
1929
return 0
2030
}
2131
if m == 0 && n == 0 {
2232
return 1
2333
}
24-
if pathNums[m][n] != 0 {
25-
return pathNums[m][n]
34+
if dp[m][n] != -1 {
35+
return dp[m][n]
2636
}
2737

28-
pathNums[m][n] = _helper(&pathNums, m - 1, n, obstacleGrid) + _helper(&pathNums, m, n - 1, obstacleGrid)
29-
30-
return pathNums[m][n]
38+
dp[m][n] = help(m - 1, n, &dp, obstacleGrid) + help(m, n - 1, &dp, obstacleGrid)
39+
return dp[m][n]
3140
}
3241
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)| [Swift](./Array/MinimumSizeSubarraySum.swift)| Medium| O(n)| O(1)|
6161
[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)| [Swift](./Array/MaximumSizeSubarraySumEqualsK.swift)| Medium| O(n)| O(n)|
6262
[Smallest Range](https://leetcode.com/problems/smallest-range/)| [Swift](./Array/SmallestRange.swift)| Hard | O(nm)| O(nm)|
63-
[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)| [Swift](./Array/ProductExceptSelf.swift)| Medium| O(n)| O(n)|
63+
[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)| [Swift](./Array/ProductExceptSelf.swift)| Medium| O(n)| O(1)|
6464
[Rotate Array](https://leetcode.com/problems/rotate-array/)| [Swift](./Array/RotateArray.swift)| Easy| O(n)| O(1)|
6565
[Rotate Image](https://leetcode.com/problems/rotate-image/)| [Swift](./Array/RotateImage.swift)| Medium| O(n^2)| O(1)|
6666
[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/)| [Swift](./Array/SpiralMatrix.swift)| Medium| O(n^2)| O(1)|

0 commit comments

Comments
 (0)