Skip to content

Commit 83f568b

Browse files
committed
Added 1 solution & updated 1 solution
1 parent f127d8d commit 83f568b

File tree

2 files changed

+99
-19
lines changed

2 files changed

+99
-19
lines changed

Hard/Unique Paths III.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Solution {
2+
int numOfPaths;
3+
4+
public int uniquePathsIII(int[][] grid) {
5+
numOfPaths = 0;
6+
if (grid.length == 0 || grid[0].length == 0) {
7+
return numOfPaths;
8+
}
9+
10+
int startX = -1;
11+
int startY = -1;
12+
int emptyObstacleCount = 0;
13+
14+
for (int i = 0; i < grid.length; i++) {
15+
for (int j = 0; j < grid[i].length; j++) {
16+
if (grid[i][j] == 1) {
17+
startX = i;
18+
startY = j;
19+
} else if (grid[i][j] == 0) {
20+
emptyObstacleCount++;
21+
}
22+
}
23+
}
24+
25+
dfsHelper(grid, startX, startY, emptyObstacleCount, 0, new boolean[grid.length][grid[0].length]);
26+
27+
return numOfPaths;
28+
}
29+
30+
private void dfsHelper(int[][] grid,
31+
int startX,
32+
int startY,
33+
int emptyObstacleCount,
34+
int currObstacleCount,
35+
boolean[][] visited) {
36+
if (startX < 0 ||
37+
startX >= grid.length ||
38+
startY < 0 ||
39+
startY >= grid[0].length ||
40+
visited[startX][startY] ||
41+
grid[startX][startY] == -1) {
42+
return;
43+
}
44+
45+
if (grid[startX][startY] == 2) {
46+
if (currObstacleCount == emptyObstacleCount) {
47+
numOfPaths++;
48+
}
49+
50+
return;
51+
}
52+
53+
visited[startX][startY] = true;
54+
55+
dfsHelper(grid, startX + 1, startY, emptyObstacleCount,
56+
grid[startX][startY] == 0 ? currObstacleCount + 1 : currObstacleCount, visited);
57+
dfsHelper(grid, startX - 1, startY, emptyObstacleCount,
58+
grid[startX][startY] == 0 ? currObstacleCount + 1 : currObstacleCount, visited);
59+
dfsHelper(grid, startX, startY + 1, emptyObstacleCount,
60+
grid[startX][startY] == 0 ? currObstacleCount + 1 : currObstacleCount, visited);
61+
dfsHelper(grid, startX, startY - 1, emptyObstacleCount,
62+
grid[startX][startY] == 0 ? currObstacleCount + 1 : currObstacleCount, visited);
63+
64+
visited[startX][startY] = false;
65+
}
66+
}

Medium/Simplify Path.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
class Solution {
22
public String simplifyPath(String path) {
3-
String[] splitPaths = path.split("\\/");
4-
Stack<String> paths = new Stack<>();
5-
for (String p : splitPaths) {
6-
if (p.length() == 0 || p.equals("/") || p.equals(" ") || p.equals(".")) {
7-
continue;
8-
}
3+
Stack<String> stack = new Stack<>();
4+
StringBuilder sb = new StringBuilder();
5+
int idx = 0;
6+
int n = path.length();
97

10-
if (p.equals("..")) {
11-
if (!paths.isEmpty()) {
12-
paths.pop();
8+
while (idx < n) {
9+
if (path.charAt(idx) != '/') {
10+
sb.append(path.charAt(idx));
11+
while (idx + 1 < n && path.charAt(idx + 1) != '/') {
12+
sb.append(path.charAt(idx + 1));
13+
idx++;
1314
}
15+
16+
if (sb.toString().equals("..")) {
17+
if (!stack.isEmpty()) {
18+
stack.pop();
19+
}
20+
}
21+
else if (!sb.toString().equals(".")) {
22+
stack.push(sb.toString());
23+
}
24+
25+
sb.setLength(0);
1426
}
15-
else {
16-
paths.push(p.trim());
17-
}
27+
28+
idx++;
1829
}
1930

20-
Stack<String> revPath = new Stack<>();
21-
while (!paths.isEmpty()) {
22-
revPath.push(paths.pop());
31+
List<String> list = new ArrayList<>();
32+
while (!stack.isEmpty()) {
33+
list.add(stack.pop());
2334
}
2435

25-
StringBuilder sb = new StringBuilder();
26-
while (!revPath.isEmpty()) {
27-
sb.append("/").append(revPath.pop());
36+
sb.append("/");
37+
for (int i = list.size() - 1; i >= 0; i--) {
38+
sb.append(list.get(i));
39+
if (i != 0) {
40+
sb.append("/");
41+
}
2842
}
2943

30-
return sb.length() == 0 ? "/" : sb.toString();
44+
return sb.toString();
3145
}
3246
}

0 commit comments

Comments
 (0)