Skip to content

Commit 4d68aa7

Browse files
committed
Add more solutions
1 parent 9a0cfdd commit 4d68aa7

File tree

5 files changed

+200
-8
lines changed

5 files changed

+200
-8
lines changed

src/main/java/org/sean/array/FruitCounter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.*;
44

5-
// LeetCode 904 https://leetcode.com/problems/fruit-into-baskets/
5+
// 904 Fruit into baskets
66
public class FruitCounter {
77
public int totalFruit1(int[] fruits) {
88
// Sliding Window
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.sean.backtracking;
2+
3+
// Rat in a Maze | Backtracking-2
4+
// https://www.geeksforgeeks.org/rat-in-a-maze-backtracking-2
5+
//
6+
// Count number of ways to reach destination in a Maze
7+
// Given a maze with obstacles, count the number of paths to reach the rightmost-bottommost cell from the
8+
// topmost-leftmost cell. A cell in the given maze has a value of -1 if it is a blockage or dead-end, else 0.
9+
//
10+
// From a given cell, we are allowed to move to cells (i+1, j) and (i, j+1) only.
11+
//
12+
// Examples:
13+
// Input: maze[R][C] = {{0, 0, 0, 0},
14+
// {0, -1, 0, 0},
15+
// {-1, 0, 0, 0},
16+
// {0, 0, 0, 0}};
17+
//Output: 4
18+
19+
import java.util.Deque;
20+
import java.util.LinkedList;
21+
import java.util.Locale;
22+
23+
public class MazePathFinder {
24+
// Returns count of possible paths in
25+
// a maze[R][C] from (0,0) to (R-1,C-1)
26+
int[][] moves = new int[][]{{1, 0}, {0, 1}};
27+
int total = 0;
28+
29+
Deque<int[]> paths = new LinkedList<>();
30+
31+
void move(int row, int col, int maze[][]) {
32+
int maxRow = maze.length - 1;
33+
int maxCol = maze[0].length - 1;
34+
if (row == maxRow && col == maxCol) {
35+
36+
for (int[] loc : paths) {
37+
System.out.print(String.format(Locale.ENGLISH, "[%d,%d]", loc[0], loc[1]));
38+
System.out.print(" ");
39+
}
40+
System.out.println("");
41+
42+
++total;
43+
return;
44+
}
45+
46+
for (int[] mv : moves) {
47+
int r = row + mv[0];
48+
int c = col + mv[1];
49+
50+
if (r <= maxRow && c <= maxCol && maze[r][c] != -1) {
51+
paths.add(new int[]{r, c});
52+
move(r, c, maze);
53+
paths.removeLast();
54+
}
55+
}
56+
}
57+
58+
int countPaths(int[][] maze) {
59+
paths.clear();
60+
paths.add(new int[]{0, 0});
61+
62+
move(0, 0, maze);
63+
return total;
64+
}
65+
66+
public static void main(String[] args) {
67+
int[][] input = {
68+
{0, 0, 0, 0},
69+
{0, -1, 0, 0},
70+
{-1, 0, 0, 0},
71+
{0, 0, 0, 0}
72+
};
73+
MazePathFinder finder = new MazePathFinder();
74+
int count = finder.countPaths(input);
75+
76+
System.out.println(">>> total paths : " + count);
77+
}
78+
79+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.sean.dynamicpro;
2+
3+
import java.util.*;
4+
import java.util.stream.Collectors;
5+
6+
/**
7+
* 216. Combination Sum III
8+
*/
9+
public class CombinationSum3 {
10+
List<Set<List<Integer>>> caches;
11+
12+
// k numbers that add up to a number n
13+
public List<List<Integer>> combinationSum3(int k, int n) {
14+
int[] candidates = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
15+
int maxSum = 0;
16+
for (int i : candidates) {
17+
maxSum += i;
18+
}
19+
if (n > maxSum || n < 1) {
20+
return Collections.emptyList();
21+
}
22+
23+
caches = new ArrayList<>(n + 1);
24+
caches.add(Collections.emptySet()); // 0
25+
26+
int length = candidates.length;
27+
for (int i = 1; i <= n; i++) {
28+
Set<List<Integer>> listResult = new HashSet<>();
29+
for (int j = 0; j < length; j++) {
30+
int w = candidates[j];
31+
if (i == w) {
32+
listResult.add(Arrays.asList(w));
33+
} else if (i > w) {
34+
int diff = i - w;
35+
reduceSet(listResult, w, diff);
36+
} else {
37+
break;
38+
}
39+
}
40+
caches.add(listResult);
41+
}
42+
Set<List<Integer>> result = caches.get(n);
43+
return result.stream().filter(integers -> integers.size() == k).collect(Collectors.toList());
44+
}
45+
46+
private void reduceSet(Set<List<Integer>> dpOut, int candidate, int rest) {
47+
if (rest > 0) { // skip <= 0
48+
Set<List<Integer>> sets = caches.get(rest);
49+
if (sets != null) {
50+
for (List<Integer> set : sets) {
51+
// without duplicate elem
52+
Set<Integer> existedSet = new HashSet<>(set);
53+
if (existedSet.contains(candidate)) {
54+
continue;
55+
}
56+
57+
List<Integer> matchedOut = new ArrayList<>(set);
58+
matchedOut.add(candidate);
59+
60+
// !!n*lg(n)
61+
Collections.sort(matchedOut);
62+
63+
dpOut.add(matchedOut);
64+
}
65+
}
66+
}
67+
}
68+
69+
public static void main(String[] args) {
70+
CombinationSum3 sum3 = new CombinationSum3();
71+
List<List<Integer>> lists = sum3.combinationSum3(3, 7);
72+
System.out.println(Arrays.toString(lists.toArray()));
73+
}
74+
}

src/main/java/org/sean/recursive/UniquePaths2.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
import java.util.Arrays;
44

5-
/** * 63. Unique Paths II */
5+
/**
6+
* 63. Unique Paths II
7+
*/
68
public class UniquePaths2 {
79
private int[][] cache;
810

911
/**
1012
* * Counts the unique path for an obstacle Grid
1113
*
12-
* @param m width of grid
13-
* @param n height of grid
14+
* @param m width of grid
15+
* @param n height of grid
1416
* @param obstacleGrid two-dimensional array
1517
* @return number of unique path from [0, 0] -> [m-1, n-1]
1618
*/
@@ -50,6 +52,47 @@ private int uniquePaths(int m, int n, int[][] obstacleGrid) {
5052
return up + left;
5153
}
5254

55+
/// solution II
56+
private int[][] dp;
57+
58+
public int uniquePaths2(int m, int n, int[][] obstacleGrid) {
59+
int row = m;
60+
int col = n;
61+
62+
if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1)
63+
return 0;
64+
65+
dp = new int[m + 1][n + 1];
66+
dp[1][1] = 1;
67+
68+
for (int j = 1; j <= row; j++) {
69+
for (int i = 1; i <= col; i++) {
70+
if (obstacleGrid[j - 1][i - 1] == 1) {
71+
dp[j][i] = 0;
72+
continue;
73+
}
74+
75+
if (j == 1 && i == 1)
76+
continue;
77+
78+
if (j == 1 || i == 1) {
79+
if (j == 1) {
80+
dp[j][i] = dp[j][i - 1] != 0 ? 1 : 0;
81+
} else {
82+
dp[j][i] = dp[j - 1][i] != 0 ? 1 : 0;
83+
}
84+
} else {
85+
dp[j][i] = getCnt(j, i - 1, obstacleGrid) + getCnt(j - 1, i, obstacleGrid);
86+
}
87+
}
88+
}
89+
return dp[m][n];
90+
}
91+
92+
private int getCnt(int r, int c, int[][] array) {
93+
return array[r - 1][c - 1] == 1 ? 0 : dp[r][c];
94+
}
95+
5396
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
5497
if (obstacleGrid == null || obstacleGrid.length == 0) return 0;
5598

src/main/java/org/sean/tree/TreeSerializer.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
/**
88
* * 297. Serialize and Deserialize Binary Tree
9-
*
10-
* <p>Note: The first solution (BFS) based on the sequence of the node is flawed if depth of the
11-
* tree is larger than 31. In that case, the sequence number will be at least 2 ^ 32 which is out of
12-
* range for an Integer.
139
*/
1410
public class TreeSerializer {
1511

0 commit comments

Comments
 (0)