Skip to content

Instantly share code, notes, and snippets.

@munguial
Created April 15, 2020 15:18
Show Gist options
  • Save munguial/7718fc0326451291ef826550a9a1011f to your computer and use it in GitHub Desktop.
Save munguial/7718fc0326451291ef826550a9a1011f to your computer and use it in GitHub Desktop.
Day 15 - Product Of Array Except Self
// https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/530/week-3/3300/
class Solution {
fun productExceptSelf(nums: IntArray): IntArray {
var output = nums.copyOf()
for (i in nums.lastIndex - 1 downTo 0) {
output[i] *= output[i + 1]
}
var acumulated = 1
for (i in 0 .. nums.size - 1) {
val multiplier = if (i < nums.size - 1) output[i + 1] else 1
output[i] = acumulated * multiplier
acumulated *= nums[i]
}
return output
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment