当前位置:网站首页>[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
2022-07-03 18:35:00 【51CTO】
6110. 网格图中递增路径的数目
难度困难6收藏分享切换为英文接收动态反馈
给你一个 m x n 的整数网格图 grid ,你可以从一个格子移动到 4 个方向相邻的任意一个格子。
请你返回在网格图中从 任意 格子出发,达到 任意 格子,且路径中的数字是 严格递增 的路径数目。由于答案可能会很大,请将结果对 109 + 7 取余 后返回。
如果两条路径中访问过的格子不是完全相同的,那么它们视为两条不同的路径。
示例 1:
![[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难_leetcode周赛](/img/50/47bbb6080e5e864c9f1aa994fbc67a.png)
示例 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~~,截图记录一下
![[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难_网图_02](/img/8d/0e515af6c17971ddf461e3f3b87c30.png)
![[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难_记忆化_03](/img/ab/e7aa1e39916651d248428b8cb09a47.png)
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.
边栏推荐
- Raft 日志复制
- Count the number of pixel values in the image
- Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
- Computer graduation project PHP library book borrowing management system
- Image 24 bits de profondeur à 8 bits de profondeur
- 2022-2028 global plasmid DNA cdmo industry research and trend analysis report
- Boost.Asio Library
- An academic paper sharing and approval system based on PHP for computer graduation design
- How to draw non overlapping bubble chart in MATLAB
- CV in transformer learning notes (continuously updated)
猜你喜欢

2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report

2022-2028 global lithium battery copper foil industry research and trend analysis report

论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding

Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)

Shell script return value with which output

Sensor debugging process

Summary and Reflection on the third week of winter vacation

Computer graduation project PHP library book borrowing management system

Win32: analyse du fichier dump pour la défaillance du tas
![Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]](/img/da/0a282b4773fe3909d1e5e9d19f8549.jpg)
Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
随机推荐
Analysis of the reasons why enterprises build their own software development teams to use software manpower outsourcing services at the same time
Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
webcodecs
Golang string (string) and byte array ([]byte) are converted to each other
Torch learning notes (1) -- 19 common ways to create tensor
Class exercises
[combinatorics] generating function (example of generating function | calculating generating function with given general term formula | calculating general term formula with given generating function)
Have you learned the correct expression posture of programmers on Valentine's day?
What kind of experience is it when the Institute earns 20000 yuan a month?
Kratos微服务框架下实现CQRS架构模式
Opencv learning notes (continuously updated)
How to track the real-time trend of Bank of London
Codeforces Round #803 (Div. 2) C. 3SUM Closure
What London Silver Trading software supports multiple languages
Sensor debugging process
G1 garbage collector of garbage collector
How to disable the clear button of ie10 insert text box- How can I disable the clear button that IE10 inserts into textboxes?
Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
CV in transformer learning notes (continuously updated)
Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing