File tree Expand file tree Collapse file tree 4 files changed +45
-2
lines changed Expand file tree Collapse file tree 4 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -488,4 +488,5 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
488
488
| 56. Merge Intervals | [ Link] ( https://leetcode.com/problems/merge-intervals/ ) | [ Link] ( ./lib/medium/56_merge_intervals.rb ) |
489
489
| 57. Insert Interval | [ Link] ( https://leetcode.com/problems/insert-interval/ ) | [ Link] ( ./lib/medium/57_insert_interval.rb ) |
490
490
| 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 ) |
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ require 'English'
5
5
::Gem ::Specification . new do |s |
6
6
s . required_ruby_version = '>= 3.0'
7
7
s . name = 'leetcode-ruby'
8
- s . version = '6.2.3.2 '
8
+ s . version = '6.2.4 '
9
9
s . license = 'MIT'
10
10
s . files = ::Dir [ 'lib/**/*.rb' ] + %w[ README.md ]
11
11
s . executable = 'leetcode-ruby'
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments