Skip to content

Commit d60c460

Browse files
authored
2025-01-14 v. 7.9.9: added "2657. Find the Prefix Common Array of Two Arrays"
2 parents 05e3adc + 8237032 commit d60c460

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
644644
| 921. Minimum Add to Make Parentheses Valid | [Link](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | [Link](./lib/medium/921_minimum_add_to_make_parentheses_valid.rb) | [Link](./test/medium/test_921_minimum_add_to_make_parentheses_valid.rb) |
645645
| 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) |
646646
| 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) |
647+
| 2657. Find the Prefix Common Array of Two Arrays | [Link](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [Link](./lib/medium/2657_find_the_prefix_common_array_of_two_arrays.rb) | [Link](./test/medium/test_2657_find_the_prefix_common_array_of_two_arrays.rb) |
647648
| 3223. Minimum Length of String After Operations | [Link](https://leetcode.com/problems/minimum-length-of-string-after-operations/) | [Link](./lib/medium/3223_minimum_length_of_string_after_operations.rb) | [Link](./test/medium/test_3223_minimum_length_of_string_after_operations.rb) |
648649

649650
### Hard

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.9.8'
8+
s.version = '7.9.9'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/
4+
# @param {Integer[]} a
5+
# @param {Integer[]} b
6+
# @return {Integer[]}
7+
def find_the_prefix_common_array(a, b)
8+
n = a.size
9+
freq = ::Array.new(n + 1, 0)
10+
result = ::Array.new(n, 0)
11+
common = 0
12+
13+
(0...n).each do |i|
14+
freq[a[i]] += 1
15+
common += 1 if freq[a[i]] == 2
16+
17+
freq[b[i]] += 1
18+
common += 1 if freq[b[i]] == 2
19+
20+
result[i] = common
21+
end
22+
23+
result
24+
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/2657_find_the_prefix_common_array_of_two_arrays'
5+
require 'minitest/autorun'
6+
7+
class FindThePrefixCommonArrayOfTwoArraysTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[0, 2, 3, 4],
11+
find_the_prefix_common_array(
12+
[1, 3, 2, 4],
13+
[3, 1, 2, 4]
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
[0, 1, 3],
21+
find_the_prefix_common_array(
22+
[2, 3, 1],
23+
[3, 1, 2]
24+
)
25+
)
26+
end
27+
end

0 commit comments

Comments
 (0)