Skip to content

Commit 2b297b9

Browse files
author
liwentian
committed
fd
1 parent 159f0e7 commit 2b297b9

File tree

14 files changed

+107
-233
lines changed

14 files changed

+107
-233
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
|324|[Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/WiggleSortII.java)|60|
249249
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MaximumSizeSubarraySumEqualsK.java)|75|这题思路有意思,多做几遍|
250250
|328|[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/OddEvenLinkedList.java)|90|
251+
|329|[Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LongestIncreasingPathInAMatrix.java)|70|这题要多做几遍|
251252
|333|[Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/LargestBSTSubtree.java)|50|这道题虽然不难,但是折腾了很久,一定要多做|
252253
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/IncreasingTripletSubsequence.java)|75|
253254
|335|[Self Crossing](https://leetcode.com/problems/self-crossing/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/SelfCrossing.java)|80|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.leetcode.google;
2+
3+
/**
4+
* Created by liwentian on 2017/9/1.
5+
*/
6+
7+
public class LongestIncreasingPathInAMatrix {
8+
9+
public int longestIncreasingPath(int[][] matrix) {
10+
if (matrix.length == 0) {
11+
return 0;
12+
}
13+
int row = matrix.length, col = matrix[0].length;
14+
int max = 0;
15+
int[][] cache = new int[row][col];
16+
for (int i = 0; i < row; i++) {
17+
for (int j = 0; j < col; j++) {
18+
int len = dfs(matrix, i, j, cache);
19+
max = Math.max(max, len);
20+
}
21+
}
22+
return max;
23+
}
24+
25+
private int dfs(int[][] matrix, int i, int j, int[][] cache) {
26+
if (i < 0 || i >= matrix.length || j < 0 || j >= matrix[0].length) {
27+
return 0;
28+
}
29+
30+
if (cache[i][j] > 0) {
31+
return cache[i][j];
32+
}
33+
34+
int max = 1;
35+
36+
int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
37+
38+
for (int k = 0; k < dx.length; k++) {
39+
int x = i + dx[k], y = j + dy[k];
40+
if (x < 0 || x >= matrix.length || y < 0 || y >= matrix[0].length || matrix[x][y] <= matrix[i][j]) {
41+
continue;
42+
}
43+
max = Math.max(max, dfs(matrix, x, y, cache) + 1);
44+
}
45+
46+
cache[i][j] = max;
47+
return max;
48+
}
49+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':app', ':tools', ':solution', ':google', ':library', ':facebook', ':amazon', ':topics', ':test'
1+
include ':app', ':solution', ':google', ':library', ':facebook', ':amazon', ':topics', ':test'
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.inuker.solution;
2+
3+
/**
4+
* Created by liwentian on 2017/9/1.
5+
*/
6+
7+
/**
8+
* https://leetcode.com/articles/longest-increasing-path-matrix/
9+
*/
10+
11+
/**
12+
* 这题要注意缓存,否则会超时
13+
*/
14+
public class LongestIncreasingPathInAMatrix {
15+
16+
public int longestIncreasingPath(int[][] matrix) {
17+
if (matrix.length == 0) {
18+
return 0;
19+
}
20+
int row = matrix.length, col = matrix[0].length;
21+
int max = 0;
22+
int[][] cache = new int[row][col];
23+
for (int i = 0; i < row; i++) {
24+
for (int j = 0; j < col; j++) {
25+
int len = dfs(matrix, i, j, cache);
26+
max = Math.max(max, len);
27+
}
28+
}
29+
return max;
30+
}
31+
32+
private int dfs(int[][] matrix, int i, int j, int[][] cache) {
33+
if (i < 0 || i >= matrix.length || j < 0 || j >= matrix[0].length) {
34+
return 0;
35+
}
36+
37+
if (cache[i][j] > 0) {
38+
return cache[i][j];
39+
}
40+
41+
int max = 1;
42+
43+
int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
44+
45+
for (int k = 0; k < dx.length; k++) {
46+
int x = i + dx[k], y = j + dy[k];
47+
if (x < 0 || x >= matrix.length || y < 0 || y >= matrix[0].length || matrix[x][y] <= matrix[i][j]) {
48+
continue;
49+
}
50+
max = Math.max(max, dfs(matrix, x, y, cache) + 1);
51+
}
52+
53+
cache[i][j] = max;
54+
return max;
55+
}
56+
}

test/src/main/java/com/example/Dog.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/src/main/java/com/example/People.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/src/main/java/com/example/Pig.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/src/main/java/com/example/PigManager.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/src/main/java/com/example/Wentian.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

test/src/main/java/com/example/Yulu.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)