From b60f2536874bf9f4f14c053ad1add077b471da1c Mon Sep 17 00:00:00 2001 From: fartem Date: Tue, 5 Nov 2024 11:47:36 +0300 Subject: [PATCH] 2024-11-05 v. 6.9.6: added "388. Longest Absolute File Path" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/medium/388_longest_absolute_file_path.rb | 19 +++++++++++ .../test_388_longest_absolute_file_path.rb | 32 +++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 lib/medium/388_longest_absolute_file_path.rb create mode 100644 test/medium/test_388_longest_absolute_file_path.rb diff --git a/README.md b/README.md index b13ff509..1fdbd7fa 100644 --- a/README.md +++ b/README.md @@ -561,3 +561,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 378. Kth Smallest Element in a Sorted Matrix | [Link](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/) | [Link](./lib/medium/378_kth_smallest_element_in_a_sorted_matrix.rb) | [Link](./test/medium/test_378_kth_smallest_element_in_a_sorted_matrix.rb) | | 380. Insert Delete GetRandom O(1) | [Link](https://leetcode.com/problems/insert-delete-getrandom-o1/) | [Link](./lib/medium/380_insert_delete_getrandom_o1.rb) | [Link](./test/medium/test_380_insert_delete_getrandom_o1.rb) | | 382. Linked List Random Node | [Link](https://leetcode.com/problems/linked-list-random-node/) | [Link](./lib/medium/382_linked_list_random_node.rb) | [Link](./test/medium/test_382_linked_list_random_node.rb) | +| 388. Longest Absolute File Path | [Link](https://leetcode.com/problems/longest-absolute-file-path/) | [Link](./lib/medium/388_longest_absolute_file_path.rb) | [Link](./test/medium/test_388_longest_absolute_file_path.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index ad4e78c1..f90d30db 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '6.9.5' + s.version = '6.9.6' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/388_longest_absolute_file_path.rb b/lib/medium/388_longest_absolute_file_path.rb new file mode 100644 index 00000000..bdc6a972 --- /dev/null +++ b/lib/medium/388_longest_absolute_file_path.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/longest-absolute-file-path/ +# @param {String} input +# @return {Integer} +def length_longest_path(input) + result = 0 + file_sizes = ::Array.new(input.size, 0) + input.split("\n").each do |name| + dir_level = name.count("\t") + c_name = name.gsub("\t", '') + part = dir_level.positive? ? file_sizes[dir_level - 1] + 1 : 0 + file_sizes[dir_level] = part + c_name.size + + result = [result, file_sizes[dir_level]].max if c_name.include?('.') + end + + result +end diff --git a/test/medium/test_388_longest_absolute_file_path.rb b/test/medium/test_388_longest_absolute_file_path.rb new file mode 100644 index 00000000..20bb57cd --- /dev/null +++ b/test/medium/test_388_longest_absolute_file_path.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/388_longest_absolute_file_path' +require 'minitest/autorun' + +class LongestAbsoluteFilePathTest < ::Minitest::Test + def test_default_one + assert_equal( + 20, + length_longest_path( + "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" + ) + ) + end + + def test_default_two + assert_equal( + 32, + length_longest_path( + "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" + ) + ) + end + + def test_default_three + assert_equal( + 0, + length_longest_path('a') + ) + end +end