Skip to content

Commit

Permalink
[Array] Optimize the solution to Product of Array Except Self
Browse files Browse the repository at this point in the history
[DP] Optimize code style to Unique Paths II
soapyigu committed Jun 22, 2018
1 parent edf80cc commit bef1316
Showing 3 changed files with 31 additions and 41 deletions.
39 changes: 10 additions & 29 deletions Array/ProductExceptSelf.swift
Original file line number Diff line number Diff line change
@@ -2,45 +2,26 @@
* Question Link: https://leetcode.com/problems/product-of-array-except-self/
* Primary idea: Use two arrays to hold multiplication result from left and right sides
* while iterating the original array
* Time Complexity: O(n), Space Complexity: O(n)
* Time Complexity: O(n), Space Complexity: O(1)
*/

class ProductExceptSelf {
func productExceptSelf(nums: [Int]) -> [Int] {
var res = [Int]()

func productExceptSelf(_ nums: [Int]) -> [Int] {
var res = Array(repeating: 1, count: nums.count)
var right = 1

guard nums.count > 0 else {
return res
}

let left = _initLeft(nums)
let right = _initRight(nums)

for i in 0..<nums.count {
res.append(left[i] * right[i])
}

return res
}

private func _initLeft(nums: [Int]) -> [Int] {
var left = [Int]()
left.append(1)

for i in 1..<nums.count {
left.append(left[i - 1] * nums[i - 1])
res[i] = res[i - 1] * nums[i - 1]
}

return left
}

private func _initRight(nums: [Int]) -> [Int] {
var right = Array(count: nums.count, repeatedValue: 1)

for i in (nums.count - 2).stride(through: 0, by: -1) {
right[i] = right[i + 1] * nums[i + 1]
for i in (0..<nums.count).reversed() {
res[i] = right * res[i]
right = right * nums[i]
}

return right
return res
}
}
31 changes: 20 additions & 11 deletions DP/UniquePathsII.swift
Original file line number Diff line number Diff line change
@@ -5,28 +5,37 @@
*/

class UniquePathsII {
func uniquePathsWithObstacles(obstacleGrid: [[Int]]) -> Int {
func uniquePathsWithObstacles(_ obstacleGrid: [[Int]]) -> Int {
let m = obstacleGrid.count
guard m > 0 else {
return 0
}

let n = obstacleGrid[0].count
guard n > 0 else {
return 0
}

var pathNums = Array(count: m, repeatedValue: Array(count: n, repeatedValue: 0))
return _helper(&pathNums, m - 1, n - 1, obstacleGrid)
var dp = Array(repeating: Array(repeating: -1, count: n), count: m)

return help(m - 1, n - 1, &dp, obstacleGrid)
}

func _helper(inout _ pathNums: [[Int]], _ m: Int, _ n: Int, _ obstacleGrid: [[Int]]) -> Int {
// termination
if m < 0 || n < 0 || obstacleGrid[m][n] == 1 {
fileprivate func help(_ m: Int, _ n: Int, _ dp: inout [[Int]], _ obstacleGrid: [[Int]]) -> Int {
if m < 0 || n < 0 {
return 0
}
if obstacleGrid[m][n] == 1 {
return 0
}
if m == 0 && n == 0 {
return 1
}
if pathNums[m][n] != 0 {
return pathNums[m][n]
if dp[m][n] != -1 {
return dp[m][n]
}

pathNums[m][n] = _helper(&pathNums, m - 1, n, obstacleGrid) + _helper(&pathNums, m, n - 1, obstacleGrid)

return pathNums[m][n]
dp[m][n] = help(m - 1, n, &dp, obstacleGrid) + help(m, n - 1, &dp, obstacleGrid)
return dp[m][n]
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@
[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)| [Swift](./Array/MinimumSizeSubarraySum.swift)| Medium| O(n)| O(1)|
[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)|
[Smallest Range](https://leetcode.com/problems/smallest-range/)| [Swift](./Array/SmallestRange.swift)| Hard | O(nm)| O(nm)|
[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)| [Swift](./Array/ProductExceptSelf.swift)| Medium| O(n)| O(n)|
[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)| [Swift](./Array/ProductExceptSelf.swift)| Medium| O(n)| O(1)|
[Rotate Array](https://leetcode.com/problems/rotate-array/)| [Swift](./Array/RotateArray.swift)| Easy| O(n)| O(1)|
[Rotate Image](https://leetcode.com/problems/rotate-image/)| [Swift](./Array/RotateImage.swift)| Medium| O(n^2)| O(1)|
[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/)| [Swift](./Array/SpiralMatrix.swift)| Medium| O(n^2)| O(1)|

0 comments on commit bef1316

Please sign in to comment.