From d48a6379a24ffb3d4682b4bce5055e935ce554c2 Mon Sep 17 00:00:00 2001 From: fartem Date: Sat, 18 Jan 2025 12:31:30 +0300 Subject: [PATCH] 2025-01-18 v. 8.1.6: added "1368. Minimum Cost to Make at Least One Valid Path in a Grid" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ..._make_at_least_one_valid_path_in_a_grid.rb | 43 +++++++++++++++++ ..._make_at_least_one_valid_path_in_a_grid.rb | 46 +++++++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 lib/hard/1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid.rb create mode 100644 test/hard/test_1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid.rb diff --git a/README.md b/README.md index 64ba4c6a..25d230a5 100644 --- a/README.md +++ b/README.md @@ -683,5 +683,6 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 968. Binary Tree Cameras | [Link](https://leetcode.com/problems/binary-tree-cameras/) | [Link](./lib/hard/968_binary_tree_cameras.rb) | [Link](./test/hard/test_968_binary_tree_cameras.rb) | | 1106. Parsing A Boolean Expression | [Link](https://leetcode.com/problems/parsing-a-boolean-expression/) | [Link](./lib/hard/1106_parsing_a_boolean_expression.rb) | [Link](./test/hard/test_1106_parsing_a_boolean_expression.rb) | | 1220. Count Vowels Permutation | [Link](https://leetcode.com/problems/count-vowels-permutation/) | [Link](./lib/hard/1220_count_vowels_permutation.rb) | [Link](./test/hard/test_1220_count_vowels_permutation.rb) | +| 1368. Minimum Cost to Make at Least One Valid Path in a Grid | [Link](https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/) | [Link](./lib/hard/1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid.rb) | [Link](./test/hard/test_1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid.rb) | | 1770. Maximum Score from Performing Multiplication Operations | [Link](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/) | [Link](./lib/hard/1770_maximum_score_from_performing_multiplication_operations.rb) | [Link](./test/hard/test_1770_maximum_score_from_performing_multiplication_operations.rb) | | 1944. Number of Visible People in a Queue | [Link](https://leetcode.com/problems/number-of-visible-people-in-a-queue/) | [Link](./lib/hard/1944_number_of_visible_people_in_a_queue.rb) | [Link](./test/hard/test_1944_number_of_visible_people_in_a_queue.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 8eb3eb62..dc21a137 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 = '8.1.5' + s.version = '8.1.6' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/hard/1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid.rb b/lib/hard/1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid.rb new file mode 100644 index 00000000..cb8d7d04 --- /dev/null +++ b/lib/hard/1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: false + +# https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/ +# @param {Integer[][]} grid +# @return {Integer} +def min_cost(grid) + m = grid.length + n = grid[0].length + dist = ::Array.new(m) { ::Array.new(n, ::Float::INFINITY) } + dist[0][0] = 0 + + dirs = [ + [0, 1], + [0, -1], + [1, 0], + [-1, 0] + ] + pq = [[0, 0, 0]] + + until pq.empty? + cost, row, col = pq.shift + + next if cost > dist[row][col] + + 4.times do |i| + new_row = row + dirs[i][0] + new_col = col + dirs[i][1] + + next if new_row.negative? || new_row >= m || new_col.negative? || new_col >= n + + new_cost = cost + new_cost += 1 if grid[row][col] != i + 1 + + next unless new_cost < dist[new_row][new_col] + + dist[new_row][new_col] = new_cost + idx = pq.bsearch_index { |x| x[0] > new_cost } || pq.length + pq.insert(idx, [new_cost, new_row, new_col]) + end + end + + dist[m - 1][n - 1] +end diff --git a/test/hard/test_1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid.rb b/test/hard/test_1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid.rb new file mode 100644 index 00000000..bd387294 --- /dev/null +++ b/test/hard/test_1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: false + +require_relative '../test_helper' +require_relative '../../lib/hard/1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid' +require 'minitest/autorun' + +class MinimumCostToMakeAtLeastOneValidPathInAGridTest < ::Minitest::Test + def test_default_one + assert_equal( + 3, + min_cost( + [ + [1, 1, 1, 1], + [2, 2, 2, 2], + [1, 1, 1, 1], + [2, 2, 2, 2] + ] + ) + ) + end + + def test_default_two + assert_equal( + 0, + min_cost( + [ + [1, 1, 3], + [3, 2, 2], + [1, 1, 4] + ] + ) + ) + end + + def test_default_three + assert_equal( + 1, + min_cost( + [ + [1, 2], + [4, 3] + ] + ) + ) + end +end