- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2328-number-of-increasing-paths-in-a-grid.rb
81 lines (66 loc) · 2.33 KB
/
2328-number-of-increasing-paths-in-a-grid.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true
# https://leetcode.com/problems/number-of-increasing-paths-in-a-grid
# 2328. Number of Increasing Paths in a Grid
# Hard
=begin
You are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions.
Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell. Since the answer may be very large, return it modulo 109 + 7.
Two paths are considered different if they do not have exactly the same sequence of visited cells.
Example 1:
Input: grid = [[1,1],[3,4]]
Output: 8
Explanation: The strictly increasing paths are:
- Paths with length 1: [1], [1], [3], [4].
- Paths with length 2: [1 -> 3], [1 -> 4], [3 -> 4].
- Paths with length 3: [1 -> 3 -> 4].
The total number of paths is 4 + 3 + 1 = 8.
Example 2:
Input: grid = [[1],[2]]
Output: 3
Explanation: The strictly increasing paths are:
- Paths with length 1: [1], [2].
- Paths with length 2: [1 -> 2].
The total number of paths is 2 + 1 = 3.
Constraints:
* m == grid.length
* n == grid[i].length
* 1 <= m, n <= 1000
* 1 <= m * n <= 105
* 1 <= grid[i][j] <= 105
=end
# @param {Integer[][]} grid
# @return {Integer}
defcount_paths(grid)
max_row_index=grid.size - 1
max_col_index=grid.first.size - 1
result_grid=Array.new(grid.size){Array.new(grid.first.size,nil)}
total_result=0
mod=10**9 + 7
dfs=lambdado |row_index,col_index,previous|
return0unlessrow_index.between?(0,max_row_index) && col_index.between?(0,max_col_index)
cell=grid[row_index][col_index]
return0unlesscell > previous
returnresult_grid[row_index][col_index]ifresult_grid[row_index][col_index]
steps=[[-1,0],[1,0],[0, -1],[0,1]]
result=steps.sumdo |step_row,step_col|
dfs.call(row_index + step_row,col_index + step_col,cell)
end + 1
result_grid[row_index][col_index]=result % mod
end
0.upto(max_row_index)do |row_index|
0.upto(max_col_index)do |col_index|
total_result += dfs.call(row_index,col_index, -Float::INFINITY)
end
end
total_result % mod
end
# **************** #
# TEST #
# **************** #
require"test/unit"
classTest_count_paths < Test::Unit::TestCase
deftest_
assert_equal8,count_paths([[1,1],[3,4]])
assert_equal3,count_paths([[1],[2]])
end
end