Skip to content

Commit 485d437

Browse files
authored
Merge pull request #683 from fartem/62_Unique_Paths
2024-07-22 v. 6.2.4: added "62. Unique Paths"
2 parents e8f9955 + e6b884a commit 485d437

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,5 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
488488
| 56. Merge Intervals | [Link](https://leetcode.com/problems/merge-intervals/) | [Link](./lib/medium/56_merge_intervals.rb) |
489489
| 57. Insert Interval | [Link](https://leetcode.com/problems/insert-interval/) | [Link](./lib/medium/57_insert_interval.rb) |
490490
| 59. Spiral Matrix II | [Link](https://leetcode.com/problems/spiral-matrix-ii/) | [Link](./lib/medium/59_spiral_matrix_ii.rb) |
491-
| 61. Rotate ListII | [Link](https://leetcode.com/problems/rotate-list/) | [Link](./lib/medium/61_rotate_list.rb) |
491+
| 61. Rotate List | [Link](https://leetcode.com/problems/rotate-list/) | [Link](./lib/medium/61_rotate_list.rb) |
492+
| 62. Unique Paths | [Link](https://leetcode.com/problems/unique-paths/) | [Link](./lib/medium/62_unique_paths.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 = '6.2.3.2'
8+
s.version = '6.2.4'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/62_unique_paths.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/unique-paths/
4+
# @param {Integer} m
5+
# @param {Integer} n
6+
# @return {Integer}
7+
def unique_paths(m, n)
8+
find_paths({}, m, n)
9+
end
10+
11+
private
12+
13+
# @param {Hash} map
14+
# @param {Integer} m
15+
# @param {Integer} n
16+
# @return {Integer}
17+
def find_paths(map, m, n)
18+
return [m, n].min if m < 2 || n < 2
19+
20+
cell = "#{m},#{n}"
21+
22+
return map[cell] if map.key?(cell)
23+
24+
l = find_paths(map, m, n - 1)
25+
r = find_paths(map, m - 1, n)
26+
27+
map[cell] = l + r
28+
29+
l + r
30+
end

test/medium/test_62_unique_paths.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/62_unique_paths'
5+
require 'minitest/autorun'
6+
7+
class UniquePathsTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(28, unique_paths(3, 7))
10+
assert_equal(3, unique_paths(3, 2))
11+
end
12+
end

0 commit comments

Comments
 (0)