Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Warmup : product of array except self

Johnlin
April 11, 2017

Swift Warmup : product of array except self

Johnlin

April 11, 2017
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. 238. Product of Array Except Self • https://leetcode.com/problems/product-of-array-except- self •

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. • څ㟬Ұݸ࠷গ༗ၷݸᏐࣈత Array nums, ճၚҰݸ Array output, output[i] ౳ԙ nums ཫ໘আྃ nums[i] Ҏ֎ॴ༗త Ꮠࣈ૬ငɻ

  2. • Follow up:
 Could you solve it with constant space

    complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.) • Ճ෼୊ɿ
 ༗ᔒ༗㭎๏༻ݻఆతۭؒबಘ౸݁Ռʁʢ༌ग़༻త Array ໵ࢉੋݻఆۭؒʣ
  3. ൣྫ • ༌ೖ: [1,2,3,4] • ճၚ: [24, 12, 8, 6]


    24 => 2*3*4
 12 => 1*3*4
 8 => 1*2*4
 6 => 1*2*3
  4. ൣྫ • ༌ೖ: [2, 3, 5, 7] • ճၚ: [105,

    70, 42, 30]
 105 => 3*5*7
 70 => 2*5*7
 42 => 2*3*7
 30 => 2*3*5
  5. ՄҎ༻আ๏త၏๏ class Solution { func productExceptSelf(_ nums: [Int]) -> [Int]

    { let productAll = nums.reduce(1) { $0 * $1 } return nums.map{productAll/$0} } }
  6. ෆೳ༻আ๏తԋࢉ๏ nums 2 3 5 7 ࠨᬑင աိ 2 6

    30 210 ӈᬑင աိ 210 105 35 7 ݁Ռ 105 70 42 30
  7. ෆೳ༻আ๏తԋࢉ๏ nums 2 3 5 7 ࠨᬑင աိ 2 6

    30 210 ӈᬑင աိ 210 105 35 7 ݁Ռ 105 70 42 30
  8. ෆೳ༻আ๏తԋࢉ๏ nums 2 3 5 7 ࠨᬑင աိ 2 6

    30 210 ӈᬑင աိ 210 105 35 7 ݁Ռ 105 70 42 30
  9. ෆೳ༻আ๏తԋࢉ๏ nums 2 3 5 7 ࠨᬑင աိ 2 6

    30 210 ӈᬑင աိ 210 105 35 7 (1) ݁Ռ 105 70 42 30
  10. ෆೳ༻আ๏తԋࢉ๏ nums 2 3 5 7 ࠨᬑင աိ 2 6

    30 210 ӈᬑင աိ 210 105 35 7 ݁Ռ 105 70 42 30
  11. લޙత210 ੋ༻ෆ౸త nums 2 3 5 7 ࠨᬑင աိ 2

    6 30 210 ӈᬑင աိ 210 105 35 7 (1) ݁Ռ 105 70 42 30
  12. ܭࢉࠨᬑငաိ class Solution { func productExceptSelf(_ nums: [Int]) -> [Int]

    { var frontProd:[Int] = [1] for n in nums { frontProd.append(frontProd.last! * n) }
 frontProd.removeLast() } } nums 2 3 5 7 frontP rod 1 2 6 30 210
  13. ܭࢉӈᬑငաိ class Solution { func productExceptSelf(_ nums: [Int]) -> [Int]

    { var backProd:[Int] = [1] for n in nums.reversed() { backProd.append(backProd.last! * n) } backProd.reverse() backProd.removeFirst() } } nums 2 3 5 7 back prod 210 105 35 7 1
  14. ܭࢉ݁Ռ var res:[Int] = [] for idx in 0..<nums.count {

    res.append(frontProd[idx] * backProd[idx]) } return res nums 2 3 5 7 frontProd 1 2 6 30 backProd 105 35 7 1 ݁Ռ 105 70 42 30
  15. class Solution { func productExceptSelf(_ nums: [Int]) -> [Int] {

    var frontProd:[Int] = [1] for n in nums { frontProd.append(frontProd.last! * n) } frontProd.removeLast() var backProd:[Int] = [1] for n in nums.reversed() { backProd.append(backProd.last! * n) } backProd.reverse() backProd.removeFirst() var res:[Int] = [] for idx in 0..<nums.count { res.append(frontProd[idx] * backProd[idx]) } return res } } ࣌ؒෳᯑ౓: O(n)
 ۭؒෳᯑ౓: O(n)
  16. Ճ෼୊၏๏ • ೺frontProd ଘਐ݁Ռ • ಈଶܭࢉbackProd ࠶ငਐ݁Ռ
 nums 2 3

    5 7 frontProd 1 2 6 30 backProd 105 35 7 1 ݁Ռ 105 70 42 30
  17. class Solution { func productExceptSelf(_ nums: [Int]) -> [Int] {

    var frontProd:[Int] = [1] for n in nums { frontProd.append(frontProd.last! * n) } frontProd.removeLast() var backProd = 1 for idx in (0..<nums.count).reversed() { backProd *= nums[idx] let skippedOneIdx = idx - 1 guard skippedOneIdx >= 0 else {break} frontProd[skippedOneIdx] *= backProd } return frontProd } } ࣌ؒෳᯑ౓: O(n)
 ۭؒෳᯑ౓: O(1)
  18. Q&A