Minor changes

This commit is contained in:
Vitor Oliveira 2021-02-28 17:01:15 -08:00
parent 8e1e5586a5
commit 29dc653dba
2 changed files with 27 additions and 29 deletions

View file

@ -5,10 +5,13 @@
# each element at index `i` of the new array is the product of
# all the numbers in the original array except the one at `i`.
# Generates prefix products
# prefix_products[i] contains the product of all the elements to the left
# Note: for the element at index '0', there are no elements to the left,
# so the prefix_products[0] would be 1
#
# This file solves the algorithm in 3 approaches:
#
# 1. Brute force
# 2. Left and Right product lists
# 3. O(1) space approach
#
#
# 1. Brute force solution
@ -16,9 +19,11 @@
def calculate_products_of_all_other_elements(nums)
product_of_other_elements = Array.new(nums.length, 1)
nums.each_with_index do |_num1, i|
nums.each_with_index do |num2, j|
product_of_other_elements[i] = product_of_other_elements[i] * num2 if i != j
nums.count.times do |i|
nums.count.times do |j|
next if i == j
product_of_other_elements[i] = product_of_other_elements[i] * nums[j]
end
end
@ -48,7 +53,7 @@ def build_prefix_products(nums)
nums.each do |num|
prefix_products << if prefix_products.count > 0
(prefix_products.last * num)
prefix_products.last * num
else
num
end
@ -66,7 +71,7 @@ def build_suffix_products(nums)
nums.reverse.each do |num|
suffix_products << if suffix_products.count > 0
(suffix_products.last * num)
suffix_products.last * num
else
num
end
@ -107,19 +112,13 @@ end
puts(products([1, 2, 3]))
# => [6, 3, 2]
#
# Approach 3: O(1) space approach
#
# Although the above solution is good enough to solve the problem since
# we are not using division anymore, there's a follow-up component as
# well which asks us to solve this using constant space. Understandably so,
# the output array does not count towards the space complexity.
# This approach is essentially an extension of the approach above.
# This approach is essentially an extension of the approach 2.
# Basically, we will be using the output array as one of L or R and we will
# be constructing the other one on the fly. Let's look at the algorithm based
# on this idea.
# be constructing the other one on the fly.
# Complexity analysis
#
@ -128,36 +127,35 @@ puts(products([1, 2, 3]))
# answer.
# Space complexity: O(1) since don't use any additional array for our
# computations. The problem statement mentions that using the answeranswer
# computations. The problem statement mentions that using the answer
# array doesn't add to the space complexity.
def products(nums)
return [] if nums.size < 2
# The answer array to be returned
res = [1]
# answer[i] contains the product of all the elements to the left
# Note: for the element at index '0', there are no elements to the left,
# so the answer[0] would be 1
(0..(nums.size - 2)).each do |idx|
(0..(nums.size - 2)).each do |i|
# answer[i - 1] already contains the product of elements to the left of 'i - 1'
# Simply multiplying it with nums[i - 1] would give the product of all
# elements to the left of index 'i'
num = nums[idx]
res << num * res[idx]
num = nums[i]
res << num * res[i]
end
# R contains the product of all the elements to the right
# Note: for the element at index 'length - 1', there are no elements to the right,
# so the R would be 1
product = 1
(nums.size - 1).downto(1).each do |idx|
num = nums[idx]
# For the index 'i', R would contain the
# product of all elements to the right. We update R accordingly
res[idx - 1] *= (product * num)
product *= num
(nums.size - 1).downto(1).each do |i|
num = nums[i]
# For the index 'i', R would contain the
# product of all elements to the right. We update R accordingly
res[i - 1] *= (product * num)
product *= num
end
res

View file

@ -146,7 +146,7 @@ class DoublyLinkedList
print "#{head.val} --> "
if head.next.nil?
puts("nil\n")
return
nil
else
print_values(head.next)
end