Skip to content

Instantly share code, notes, and snippets.

@catwell
Created November 28, 2022 13:51
Show Gist options
  • Save catwell/d24810fc659072e8fcc1a179f9a95604 to your computer and use it in GitHub Desktop.
Save catwell/d24810fc659072e8fcc1a179f9a95604 to your computer and use it in GitHub Desktop.
smlpth daily 2022-11-26
def efficient_part2(input)
n = input.length
r = Array.new(n, 1)
p_up, p_down = 1, 1
0.upto(n - 1) do |i|
j = n - i - 1
r[i] *= p_up
r[j] *= p_down
p_up *= input[i]
p_down *= input[j]
end
r
end
@catwell
Copy link
Author

catwell commented Nov 28, 2022

Potentiellement plus simple à comprendre, c'est la même chose sinon :

def efficient_part2(input)
  n = input.length
  r = Array.new(n, 1)
  p_up = 1
  0.upto(n - 1) do |i|
    r[i] *= p_up
    p_up *= input[i]
  end
  p_down = 1
  (n - 1).downto(0) do |j|
    r[j] *= p_down
    p_down *= input[j]
  end
  r
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment