Created
April 15, 2020 15:18
-
-
Save munguial/7718fc0326451291ef826550a9a1011f to your computer and use it in GitHub Desktop.
Day 15 - Product Of Array Except Self
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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