Skip to content

Commit 94ffc5a

Browse files
authored
2024-12-25 v. 7.4.9: added "713. Subarray Product Less Than K"
2 parents c823277 + b03277c commit 94ffc5a

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
613613
| 692. Top K Frequent Words | [Link](https://leetcode.com/problems/top-k-frequent-words/) | [Link](./lib/medium/692_top_k_frequent_words.rb) | [Link](./test/medium/test_692_top_k_frequent_words.rb) |
614614
| 701. Insert into a Binary Search Tree | [Link](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Link](./lib/medium/701_insert_into_a_binary_search_tree.rb) | [Link](./test/medium/test_701_insert_into_a_binary_search_tree.rb) |
615615
| 707. Design Linked List | [Link](https://leetcode.com/problems/design-linked-list/) | [Link](./lib/medium/707_design_linked_list.rb) | [Link](./test/medium/test_707_design_linked_list.rb) |
616+
| 713. Subarray Product Less Than K | [Link](https://leetcode.com/problems/subarray-product-less-than-k/) | [Link](./lib/medium/713_subarray_product_less_than_k.rb) | [Link](./test/medium/test_713_subarray_product_less_than_k.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '7.4.8'
8+
s.version = '7.4.9'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/subarray-product-less-than-k/
4+
# @param {Integer[]} nums
5+
# @param {Integer} k
6+
# @return {Integer}
7+
def num_subarray_product_less_than_k(nums, k)
8+
return 0 if k <= 1
9+
10+
result = 0
11+
prod = 1
12+
left = 0
13+
(0...nums.size).each do |right|
14+
prod *= nums[right]
15+
while prod >= k
16+
prod /= nums[left]
17+
left += 1
18+
end
19+
result += right - left + 1
20+
end
21+
22+
result
23+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/713_subarray_product_less_than_k'
5+
require 'minitest/autorun'
6+
7+
class SubarrayProductLessThanKTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
8,
11+
num_subarray_product_less_than_k(
12+
[10, 5, 2, 6],
13+
100
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
0,
21+
num_subarray_product_less_than_k(
22+
[1, 2, 3],
23+
0
24+
)
25+
)
26+
end
27+
end

0 commit comments

Comments
 (0)