Skip to content

Commit 164405a

Browse files
authored
2025-01-31 v. 8.3.7: added "1248. Count Number of Nice Subarrays"
2 parents a1a7f43 + caf049c commit 164405a

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
669669
| 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) |
670670
| 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) |
671671
| 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) |
672+
| 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) |
672673
| 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) |
673674
| 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) |
674675
| 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) |

leetcode-ruby.gemspec

+1-1
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 = '8.3.6'
8+
s.version = '8.3.7'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/count-number-of-nice-subarrays/
4+
# @param {Integer[]} nums
5+
# @param {Integer} k
6+
# @return {Integer}
7+
def number_of_subarrays(nums, k)
8+
prefix = ::Array.new(nums.size + 1, 0)
9+
max = 0
10+
count = 0
11+
12+
nums.each do |num|
13+
prefix[count] += 1
14+
15+
count += 1 if num.odd?
16+
17+
max += prefix[count - k] if count >= k
18+
end
19+
20+
max
21+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1248_count_number_of_nice_subarrays'
5+
require 'minitest/autorun'
6+
7+
class CountNumberOfNiceSubarraysTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
2,
11+
number_of_subarrays(
12+
[1, 1, 2, 1, 1],
13+
3
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
0,
21+
number_of_subarrays(
22+
[2, 4, 6],
23+
1
24+
)
25+
)
26+
end
27+
28+
def test_default_three
29+
assert_equal(
30+
16,
31+
number_of_subarrays(
32+
[2, 2, 2, 1, 2, 2, 1, 2, 2, 2],
33+
2
34+
)
35+
)
36+
end
37+
end

0 commit comments

Comments
 (0)