-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path329.LongestIncreasingPathInAMatrix.cpp
109 lines (97 loc) · 2.45 KB
/
329.LongestIncreasingPathInAMatrix.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* @lc app=leetcode id=329 lang=cpp
*
* [329] Longest Increasing Path in a Matrix
*
* https://leetcode.com/problems/longest-increasing-path-in-a-matrix/description/
*
* algorithms
* Hard (44.75%)
* Likes: 2855
* Dislikes: 52
* Total Accepted: 200.2K
* Total Submissions: 442.4K
* Testcase Example: '[[9,9,4],[6,6,8],[2,1,1]]'
*
* Given an m x n integers matrix, return the length of the longest increasing
* path in matrix.
*
* From each cell, you can either move in four directions: left, right, up, or
* down. You may not move diagonally or move outside the boundary (i.e.,
* wrap-around is not allowed).
*
*
* Example 1:
*
*
* Input: matrix = [[9,9,4],[6,6,8],[2,1,1]]
* Output: 4
* Explanation: The longest increasing path is [1, 2, 6, 9].
*
*
* Example 2:
*
*
* Input: matrix = [[3,4,5],[3,2,6],[2,2,1]]
* Output: 4
* Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally
* is not allowed.
*
*
* Example 3:
*
*
* Input: matrix = [[1]]
* Output: 1
*
*
*
* Constraints:
*
*
* m == matrix.length
* n == matrix[i].length
* 1 <= m, n <= 200
* 0 <= matrix[i][j] <= 2^31 - 1
*
*
*/
// @lc code=start
#include <vector>
class Solution {
public:
int longestIncreasingPath(std::vector<std::vector<int>>& matrix) {
directions =
std::vector<std::pair<int, int>>{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
max_lens = std::vector<std::vector<int>>(
matrix.size(), std::vector<int>(matrix[0].size(), 0));
int result = 1;
for (int i = 0; i < matrix.size(); ++i) {
for (int j = 0; j < matrix[0].size(); ++j) {
result = std::max(result, dfs(matrix, i, j));
}
}
return result;
}
private:
std::vector<std::pair<int, int>> directions;
std::vector<std::vector<int>> max_lens;
int dfs(const std::vector<std::vector<int>>& matrix, int row, int col) {
if (max_lens[row][col] != 0) {
return max_lens[row][col];
}
int len = 1;
for (auto& dir : directions) {
int r = row + dir.first;
int c = col + dir.second;
if (r < 0 || r >= matrix.size() || c < 0 || c >= matrix[0].size())
continue;
if (matrix[r][c] > matrix[row][col]) {
len = std::max(len, 1 + dfs(matrix, r, c));
}
}
max_lens[row][col] = len;
return max_lens[row][col];
}
};
// @lc code=end