Skip to content

Commit

Permalink
Create productexceptself.go
Browse files Browse the repository at this point in the history
  • Loading branch information
rerichardjr authored Sep 26, 2023
1 parent 746470a commit 25fe5a4
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions productexceptself.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
func productExceptSelf(nums []int) []int {
//1rst attempt failed with oom on case 19 of 22
//2nd attempt uses prefix product from left to right and right to left, then multiplies each element from both lists

numLen := len(nums)
productL := make([]int, numLen)
productR := make([]int, numLen)
answer := make([]int, numLen)

productL[0] = 1
productR[numLen-1] = 1

for i := 1; i < numLen; i++ {
j := numLen - i - 1
productL[i] = nums[i-1] * productL[i-1]
productR[j] = nums[j+1] * productR[j+1]
}

for i := 0; i < numLen; i++ {
answer[i] = productL[i] * productR[i]
}

return answer

}

0 comments on commit 25fe5a4

Please sign in to comment.