当前位置:网站首页>[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.
边栏推荐
- English grammar_ Noun classification
- How many convolution methods does deep learning have? (including drawings)
- Win 11 major updates, new features love love.
- [combinatorics] generating function (use generating function to solve the combination number of multiple sets R)
- webcodecs
- 198. Looting - Dynamic Planning
- How to track the real-time trend of Bank of London
- CV in transformer learning notes (continuously updated)
- Implementation of cqrs architecture mode under Kratos microservice framework
- Reappearance of ASPP (atlas spatial pyramid pooling) code
猜你喜欢
![Bloom filter [proposed by bloom in 1970; redis cache penetration solution]](/img/f9/27a75454b464d59b9b3465d25fe070.jpg)
Bloom filter [proposed by bloom in 1970; redis cache penetration solution]

How does GCN use large convolution instead of small convolution? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)

Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)

CTO and programmer were both sentenced for losing control of the crawler

Grammaire anglaise Nom - Classification

How to draw non overlapping bubble chart in MATLAB

Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)

4. Load balancing and dynamic static separation

English语法_名词 - 分类

How many convolution methods does deep learning have? (including drawings)
随机推荐
简述服务量化分析体系
Should I be laid off at the age of 40? IBM is suspected of age discrimination, calling its old employees "dinosaurs" and planning to dismiss, but the employees can't refute it
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
2022-2028 global scar care product industry research and trend analysis report
SSH 远程执行命令简介
[Yu Yue education] world reference materials of Microbiology in Shanghai Jiaotong University
Redis cache avalanche, penetration, breakdown
PHP determines which constellation it belongs to today
[combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
Niuke monthly race 31 minus integer
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
[combinatorics] generating function (property summary | important generating function)*
[combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
Valentine's day, send you a little red flower~
[untitled]
Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation)
Golang string (string) and byte array ([]byte) are converted to each other
[combinatorics] generating function (positive integer splitting | unordered non repeated splitting example)
Solve the problem of inaccurate network traffic monitored by ZABBIX with SNMP