当前位置:网站首页>[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
2022-07-03 18:35:00 【51CTO】
6110. 网格图中递增路径的数目
难度困难6收藏分享切换为英文接收动态反馈
给你一个 m x n
的整数网格图 grid
,你可以从一个格子移动到 4
个方向相邻的任意一个格子。
请你返回在网格图中从 任意 格子出发,达到 任意 格子,且路径中的数字是 严格递增 的路径数目。由于答案可能会很大,请将结果对 109 + 7
取余 后返回。
如果两条路径中访问过的格子不是完全相同的,那么它们视为两条不同的路径。
示例 1:
示例 2:
提示:
-
m == grid.length
-
n == grid[i].length
-
1 <= m, n <= 1000
-
1 <= m * n <= 105
-
1 <= grid[i][j] <= 105
题解
方法一:暴力递归解法
思路:
很简单,就是暴力递归遍历,可惜只过来34/36个测试用例,第35个超时了。很蛋疼,第一次参加周赛,第四题因为超时没过,有点不开心~~
第一次参加周赛,显示排名1325~~,截图记录一下
class Solution {
public:
int mod = 1e9 + 7;
void path( vector < vector < int >> & grid, int x, int y, int & res, int pre)
{
if ( y >= grid. size() || x >= grid[ 0]. size() || x < 0 || y < 0)
{
return;
}
if ( grid[ y][ x] <= pre)
{
return;
}
res = ( res + 1) % mod;
int cur = grid[ y][ x];
path( grid, x - 1, y, res, cur);
path( grid, x + 1, y, res, cur);
path( grid, x, y - 1, res, cur);
path( grid, x, y + 1, res, cur);
}
int countPaths( vector < vector < int >> & grid)
{
if ( grid. size() == 0)
{
return 0;
}
if ( grid[ 0]. size() == 0)
{
return 0;
}
int res = 0;
vector < vector < int >> v( grid. size(), std::vector < int >( grid[ 0]. size(), 0));
for ( int i = 0; i < grid. size(); ++ i)
{
for ( int k = 0; k < grid[ 0]. size(); ++ k)
{
path( grid, k, i, res, INT_MIN);
}
}
// return (res + grid.size() * grid[0].size()) % mod;
return res;
}
};
- 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.
方法二:记忆化解法
后来想了下,用一个数组记录一下每个路径的值即可,当已经访问过后直接返回就可以了。我们使用dp[y][x]表示坐标x、y为七点的坐标所有的搜索路径。说实话,这个题目是真的不难~~~
代码如下:
int path( vector < vector < int >> & grid, vector < vector < int >> & v, int x, int y)
{
if ( y >= grid. size() || x >= grid[ 0]. size() || x < 0 || y < 0)
{
return 0;
}
// 如果已经计算过,则直接返回
if ( v[ y][ x] != 0)
{
return v[ y][ x];
}
int direct[ 4][ 2] = { 0, 1, 0, - 1, 1, 0, - 1, 0}; // 四个搜索方向
v[ y][ x] = 1; // <--- 假设四周的元素都不大于它,则只有1个路径
for ( int i = 0; i < 4; ++ i)
{
int next_x = direct[ i][ 0] + x;
int next_y = direct[ i][ 1] + y;
// 边界条件检查
if ( next_y >= grid. size() || next_x >= grid[ 0]. size() || next_x < 0 || next_y < 0)
{
continue;
}
// 只有递增的才需要计算
if ( grid[ next_y][ next_x] <= grid[ y][ x])
{
continue;
}
v[ y][ x] = ( v[ y][ x] + path( grid, v, next_x, next_y)) % mod; // 累加四个方向的相邻长度
}
return v[ y][ x];
}
int countPaths( vector < vector < int >> & grid)
{
int res = 0;
vector < vector < int >> v( grid. size(), std::vector < int >( grid[ 0]. size(), 0));
for ( int i = 0; i < grid. size(); ++ i)
{
for ( int k = 0; k < grid[ 0]. size(); ++ k)
{
res = ( res + path( grid, v, k, i)) % mod;
}
}
return res;
}
- 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.
边栏推荐
- Usage of laravel conditional array in
- Torch learning notes (2) -- 11 common operation modes of tensor
- Sepconv (separable revolution) code recurrence
- An academic paper sharing and approval system based on PHP for computer graduation design
- Bloom filter [proposed by bloom in 1970; redis cache penetration solution]
- How to quickly view the inheritance methods of existing models in torchvision?
- Bidding procurement scheme management of Oracle project management system
- Line by line explanation of yolox source code of anchor free series network (6) -- mixup data enhancement
- [combinatorics] exponential generating function (proving that the exponential generating function solves the arrangement of multiple sets)
- English grammar_ Adjective / adverb Level 3 - multiple expression
猜你喜欢
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
2022-2028 global aircraft head up display (HUD) industry research and trend analysis report
Sensor debugging process
How to analyze the rising and falling rules of London gold trend chart
Kratos微服务框架下实现CQRS架构模式
An academic paper sharing and approval system based on PHP for computer graduation design
Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals
Prototype inheritance..
How does GCN use large convolution instead of small convolution? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
随机推荐
Niuke monthly race 31 minus integer
Use of unsafe class
多媒体NFT聚合平台OKALEIDO即将上线,全新的NFT时代或将来临
企业级自定义表单引擎解决方案(十二)--表单规则引擎2
[combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
Torch learning notes (3) -- univariate linear regression model (self training)
Read the paper glodyne global topology preserving dynamic network embedding
Multifunctional web file manager filestash
041. (2.10) talk about manpower outsourcing
[combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*
Administrative division code acquisition
What does foo mean in programming?
ES7 - Optimization of promise
This diversion
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
The vscode code is automatically modified to a compliance code when it is formatted and saved
统计图像中各像素值的数量
[combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
[combinatorics] generating function (commutative property | derivative property | integral property)