Skip to content

Commit dda367b

Browse files
committed
0329 & 0572 folders & cpp solution
1 parent 2db2f92 commit dda367b

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Solution {
2+
private:
3+
int Dfs(vector<vector<int>> &m, vector<vector<int>> &v,
4+
int i, int j,
5+
bool first, int lastNum)
6+
{
7+
if (i < 0 || i >= m.size()
8+
|| j < 0 || j >= m[0].size())
9+
return 0 ;
10+
11+
if (!first && lastNum >= m[i][j])
12+
return 0 ;
13+
14+
if (v[i][j] >= 0)
15+
return v[i][j] ;
16+
17+
int d1 = Dfs(m, v, i-1, j, false, m[i][j]) ;
18+
int d2 = Dfs(m, v, i+1, j, false, m[i][j]) ;
19+
int d3 = Dfs(m, v, i, j-1, false, m[i][j]) ;
20+
int d4 = Dfs(m, v, i, j+1, false, m[i][j]) ;
21+
v[i][j] = 1 + max(max(d1, d2), max(d3, d4)) ;
22+
23+
return v[i][j] ;
24+
}
25+
public:
26+
int longestIncreasingPath(vector<vector<int>>& matrix) {
27+
if (matrix.size() == 0)
28+
return 0 ;
29+
const int h = matrix.size(), w = matrix[0].size() ;
30+
vector<vector<int>> visited(h, vector<int>(w, -1)) ;
31+
32+
int M = 0 ;
33+
for (int i = 0; i < h; ++i)
34+
for (int j = 0; j < w; ++j)
35+
{
36+
if (j + 1 < w && matrix[i][j] > matrix[i][j+1]
37+
|| i + 1 < h && matrix[i][j] > matrix[i+1][j])
38+
{
39+
continue ;
40+
}
41+
else
42+
{
43+
M = max(M, Dfs(matrix, visited, i, j, true, 0)) ;
44+
}
45+
}
46+
return M ;
47+
}
48+
};
49+
50+
static const auto __ = []() {
51+
std::ios::sync_with_stdio(false);
52+
std::cin.tie(nullptr);
53+
return nullptr;
54+
}();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
private:
12+
bool IsSameTree(TreeNode *s, TreeNode *t)
13+
{
14+
if (nullptr == s && nullptr == t)
15+
return true ;
16+
if (nullptr == s || nullptr == t
17+
|| s->val != t->val)
18+
return false ;
19+
20+
return IsSameTree(s->left, t->left)
21+
&& IsSameTree(s->right, t->right) ;
22+
}
23+
public:
24+
bool isSubtree(TreeNode* s, TreeNode* t) {
25+
if (nullptr == s)
26+
return false ;
27+
28+
if (s->val == t->val && IsSameTree(s, t))
29+
return true ;
30+
31+
if (!isSubtree(s->left, t)
32+
&& !isSubtree(s->right, t))
33+
return false ;
34+
35+
return true ;
36+
}
37+
};

0 commit comments

Comments
 (0)