Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading