Skip to content

Commit d4dee8b

Browse files
authored
2025-01-01 v. 7.5.9: added "817. Linked List Components"
2 parents 77f43a3 + e01442a commit d4dee8b

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,3 +623,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
623623
| 784. Letter Case Permutation | [Link](https://leetcode.com/problems/letter-case-permutation/) | [Link](./lib/medium/784_letter_case_permutation.rb) | [Link](./test/medium/test_784_letter_case_permutation.rb) |
624624
| 797. All Paths From Source to Target | [Link](https://leetcode.com/problems/all-paths-from-source-to-target/) | [Link](./lib/medium/797_all_paths_from_source_to_target.rb) | [Link](./test/medium/test_797_all_paths_from_source_to_target.rb) |
625625
| 814. Binary Tree Pruning | [Link](https://leetcode.com/problems/binary-tree-pruning/) | [Link](./lib/medium/814_binary_tree_pruning.rb) | [Link](./test/medium/test_814_binary_tree_pruning.rb) |
626+
| 817. Linked List Components | [Link](https://leetcode.com/problems/linked-list-components/) | [Link](./lib/medium/817_linked_list_components.rb) | [Link](./test/medium/test_817_linked_list_components.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.5.8'
8+
s.version = '7.5.9'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/linked-list-components/
4+
# @param {ListNode} head
5+
# @param {Integer[]} nums
6+
# @return {Integer}
7+
def num_components(head, nums)
8+
values = nums.to_set
9+
result = 0
10+
is_connected = false
11+
12+
while head
13+
if values.include?(head.val)
14+
unless is_connected
15+
is_connected = true
16+
result += 1
17+
end
18+
else
19+
is_connected = false
20+
end
21+
22+
head = head.next
23+
end
24+
25+
result
26+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/linked_list'
5+
require_relative '../../lib/medium/817_linked_list_components'
6+
require 'minitest/autorun'
7+
8+
class LinkedListComponentsTest < ::Minitest::Test
9+
def test_default_one
10+
assert_equal(
11+
2,
12+
num_components(
13+
::ListNode.from_array(
14+
[0, 1, 2, 3]
15+
),
16+
[0, 1, 3]
17+
)
18+
)
19+
end
20+
21+
def test_default_two
22+
assert_equal(
23+
2,
24+
num_components(
25+
::ListNode.from_array(
26+
[0, 1, 2, 3, 4]
27+
),
28+
[0, 3, 1, 4]
29+
)
30+
)
31+
end
32+
end

0 commit comments

Comments
 (0)