Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2025-01-31 v. 8.3.7: added "1248. Count Number of Nice Subarrays" #926

Merged
merged 1 commit into from
Jan 31, 2025
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 @@ -669,6 +669,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 1171. Remove Zero Sum Consecutive Nodes from Linked List | [Link](https://leetcode.com/problems/remove-zero-sum-consecutive-nodes-from-linked-list/) | [Link](./lib/medium/1171_remove_zero_sum_consecutive_nodes_from_linked_list.rb) | [Link](./test/medium/test_1171_remove_zero_sum_consecutive_nodes_from_linked_list.rb) |
| 1209. Remove All Adjacent Duplicates in String II | [Link](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/) | [Link](./lib/medium/1209_remove_all_adjacent_duplicates_in_string_ii.rb) | [Link](./test/medium/test_1209_remove_all_adjacent_duplicates_in_string_ii.rb) |
| 1247. Minimum Swaps to Make Strings Equal | [Link](https://leetcode.com/problems/minimum-swaps-to-make-strings-equal/) | [Link](./lib/medium/1247_minimum_swaps_to_make_strings_equal.rb) | [Link](./test/medium/test_1247_minimum_swaps_to_make_strings_equal.rb) |
| 1248. Count Number of Nice Subarrays | [Link](https://leetcode.com/problems/count-number-of-nice-subarrays/) | [Link](./lib/medium/1248_count_number_of_nice_subarrays.rb) | [Link](./test/medium/test_1248_count_number_of_nice_subarrays.rb) |
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
Expand Down
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 = '8.3.6'
s.version = '8.3.7'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
21 changes: 21 additions & 0 deletions lib/medium/1248_count_number_of_nice_subarrays.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

# https://leetcode.com/problems/count-number-of-nice-subarrays/
# @param {Integer[]} nums
# @param {Integer} k
# @return {Integer}
def number_of_subarrays(nums, k)
prefix = ::Array.new(nums.size + 1, 0)
max = 0
count = 0

nums.each do |num|
prefix[count] += 1

count += 1 if num.odd?

max += prefix[count - k] if count >= k
end

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

require_relative '../test_helper'
require_relative '../../lib/medium/1248_count_number_of_nice_subarrays'
require 'minitest/autorun'

class CountNumberOfNiceSubarraysTest < ::Minitest::Test
def test_default_one
assert_equal(
2,
number_of_subarrays(
[1, 1, 2, 1, 1],
3
)
)
end

def test_default_two
assert_equal(
0,
number_of_subarrays(
[2, 4, 6],
1
)
)
end

def test_default_three
assert_equal(
16,
number_of_subarrays(
[2, 2, 2, 1, 2, 2, 1, 2, 2, 2],
2
)
)
end
end