-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_increasing_path.cpp
More file actions
77 lines (67 loc) · 1.9 KB
/
longest_increasing_path.cpp
File metadata and controls
77 lines (67 loc) · 1.9 KB
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
/*
* =====================================================================================
*
* Filename: longest_increasing_path.cpp
*
* Description: 329. Longest Increasing Path in a Matrix
*
* Version: 1.0
* Created: 11/20/2025 11:26:24
* Revision: none
* Compiler: gcc
*
* Author: Michael Zhu, xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <algorithm>
#include <utility>
#include <vector>
#include "gtest/gtest.h"
class Solution {
public:
int longestIncreasingPath(std::vector<std::vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
std::vector<std::vector<int>> dp(m, std::vector<int>(n, 0));
int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
res = std::max(res, dfs(matrix, i, j, dp));
}
}
return res;
}
private:
std::vector<std::pair<int, int>> moves = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int dfs(const std::vector<std::vector<int>>& matrix, int i, int j,
std::vector<std::vector<int>>& dp) {
if (dp[i][j] != 0) {
return dp[i][j];
}
int m = matrix.size();
int n = matrix[0].size();
int res = 1;
for (auto [first, second] : moves) {
int x = i + first;
int y = j + second;
if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] <= matrix[i][j]) {
continue;
}
res = std::max(res, 1 + dfs(matrix, x, y, dp));
}
dp[i][j] = res;
return res;
}
};
TEST(Solution, longestIncreasingPath) {
std::vector<std::pair<std::vector<std::vector<int>>, int>> cases = {
{{{9, 9, 4}, {6, 6, 8}, {2, 1, 1}}, 4},
{{{3, 4, 5}, {3, 2, 6}, {2, 2, 1}}, 4},
{{{1}}, 1},
};
for (auto& [matrix, res] : cases) {
EXPECT_EQ(Solution().longestIncreasingPath(matrix), res);
}
}