Skip to content

Commit

Permalink
Because of The problem statement mentions that don't using divide , b…
Browse files Browse the repository at this point in the history
…ut the solution use divide in last line in file, the advantages of the solution is use less space . So I add back solution that do not using divide as before. The time cost is almost same both solution.
  • Loading branch information
imfaker committed May 31, 2019
1 parent b2e7678 commit d16fc61
Showing 1 changed file with 60 additions and 23 deletions.
83 changes: 60 additions & 23 deletions Array/ProductExceptSelf.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,63 @@
/**
* 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(1)
*/
* 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(1)
*/

class ProductExceptSelf {
func productExceptSelf(_ nums: [Int]) -> [Int] {
var zeroCount = 0
let total = nums.reduce(1) {
if $1 == 0 {
zeroCount += 1
}
return $0 * ($1 == 0 ? 1 : $1)
}
if zeroCount > 1 {return nums.map({_ in return 0})}
return nums.map({
if zeroCount == 1 {
return ($0 == 0 ? total : 0)
} else {
return ($0 == 0 ? total : total / $0)
}
})
}
}
func productExceptSelf(_ nums: [Int]) -> [Int] {
var zeroCount = 0
let total = nums.reduce(1) {
if $1 == 0 {
zeroCount += 1
}
return $0 * ($1 == 0 ? 1 : $1)
}
if zeroCount > 1 {return nums.map({_ in return 0})}
return nums.map({
if zeroCount == 1 {
return ($0 == 0 ? total : 0)
} else {
return ($0 == 0 ? total : total / $0)
}
})
}
}




/**
* Question Link: https://leetcode.com/problems/product-of-array-except-self/
* Primary idea: Dynamic Programming, track Left and Right product lists at the same time and just use one additional array.The problem statement mentions that using theanswer array doesn't add to the space complexity.
* Time Complexity: O(n), Space Complexity: O(1)
*/


class ProductExceptSelfNotUseDivide{

func productExceptSelf(_ nums: [Int]) -> [Int] {

var arr = Array.init(repeating: 1, count: nums.count)

for i in (0..<(nums.count - 1)).reversed() {

arr[i] = arr[i + 1] * nums[i + 1]

}

var left = 1
for i in 0..<nums.count {
if i == 0 {
arr[i] = left * arr[i]
} else {
left = left * nums[i - 1]
arr[i] = left * arr[i]
}
}

return arr
}

}

0 comments on commit d16fc61

Please sign in to comment.