Skip to content

Commit d48a637

Browse files
committed
2025-01-18 v. 8.1.6: added "1368. Minimum Cost to Make at Least One Valid Path in a Grid"
1 parent 6b60be7 commit d48a637

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,5 +683,6 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
683683
| 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) |
684684
| 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) |
685685
| 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) |
686+
| 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) |
686687
| 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) |
687688
| 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) |

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 = '8.1.5'
8+
s.version = '8.1.6'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: false
2+
3+
# https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/
4+
# @param {Integer[][]} grid
5+
# @return {Integer}
6+
def min_cost(grid)
7+
m = grid.length
8+
n = grid[0].length
9+
dist = ::Array.new(m) { ::Array.new(n, ::Float::INFINITY) }
10+
dist[0][0] = 0
11+
12+
dirs = [
13+
[0, 1],
14+
[0, -1],
15+
[1, 0],
16+
[-1, 0]
17+
]
18+
pq = [[0, 0, 0]]
19+
20+
until pq.empty?
21+
cost, row, col = pq.shift
22+
23+
next if cost > dist[row][col]
24+
25+
4.times do |i|
26+
new_row = row + dirs[i][0]
27+
new_col = col + dirs[i][1]
28+
29+
next if new_row.negative? || new_row >= m || new_col.negative? || new_col >= n
30+
31+
new_cost = cost
32+
new_cost += 1 if grid[row][col] != i + 1
33+
34+
next unless new_cost < dist[new_row][new_col]
35+
36+
dist[new_row][new_col] = new_cost
37+
idx = pq.bsearch_index { |x| x[0] > new_cost } || pq.length
38+
pq.insert(idx, [new_cost, new_row, new_col])
39+
end
40+
end
41+
42+
dist[m - 1][n - 1]
43+
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: false
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/hard/1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid'
5+
require 'minitest/autorun'
6+
7+
class MinimumCostToMakeAtLeastOneValidPathInAGridTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
3,
11+
min_cost(
12+
[
13+
[1, 1, 1, 1],
14+
[2, 2, 2, 2],
15+
[1, 1, 1, 1],
16+
[2, 2, 2, 2]
17+
]
18+
)
19+
)
20+
end
21+
22+
def test_default_two
23+
assert_equal(
24+
0,
25+
min_cost(
26+
[
27+
[1, 1, 3],
28+
[3, 2, 2],
29+
[1, 1, 4]
30+
]
31+
)
32+
)
33+
end
34+
35+
def test_default_three
36+
assert_equal(
37+
1,
38+
min_cost(
39+
[
40+
[1, 2],
41+
[4, 3]
42+
]
43+
)
44+
)
45+
end
46+
end

0 commit comments

Comments
 (0)