Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 235. Lowest Common Ancestor of a Binary Search Tree | [Link](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) | [Link](./lib/medium/235_lowest_common_ancestor_of_a_binary_search_tree.rb) | [Link](./test/medium/test_235_lowest_common_ancestor_of_a_binary_search_tree.rb) |
| 236. Lowest Common Ancestor of a Binary Tree | [Link](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | [Link](./lib/medium/236_lowest_common_ancestor_of_a_binary_tree.rb) | [Link](./test/medium/test_236_lowest_common_ancestor_of_a_binary_tree.rb) |
| 237. Delete Node in a Linked List | [Link](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Link](./lib/medium/237_delete_node_in_a_linked_list.rb) | [Link](./test/medium/test_237_delete_node_in_a_linked_list.rb) |
| 238. Product of Array Except Self | [Link](https://leetcode.com/problems/product-of-array-except-self/) | [Link](./lib/medium/238_product_of_array_except_self.rb) | [Link](./test/medium/test_238_product_of_array_except_self.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '6.7.6'
s.version = '6.7.7'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
28 changes: 28 additions & 0 deletions lib/medium/238_product_of_array_except_self.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

# https://leetcode.com/problems/product-of-array-except-self/
# @param {Integer[]} nums
# @return {Integer[]}
def product_except_self(nums)
mul = 1
zeros = 0

nums.each do |num|
zeros += num.zero? ? 1 : 0
mul *= num.zero? ? 1 : num
end

return ::Array.new(nums.length, 0) if zeros > 1

(0...nums.length).each do |i|
num = nums[i]
nums[i] =
if zeros.zero?
nums[i] = mul / num
else
num.zero? ? mul : 0
end
end

nums
end
25 changes: 25 additions & 0 deletions test/medium/test_238_product_of_array_except_self.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/238_product_of_array_except_self'
require 'minitest/autorun'

class ProductOfArrayExceptSelfTest < ::Minitest::Test
def test_default_one
assert_equal(
[24, 12, 8, 6],
product_except_self(
[1, 2, 3, 4]
)
)
end

def test_default_two
assert_equal(
[0, 0, 9, 0, 0],
product_except_self(
[-1, 1, 0, -3, 3]
)
)
end
end
Loading