Plan a fishing trip PlanningFishing.java
public static int maximizeFishing(int[][] A) {for (int i = 0; i < A.length; ++i) {
for (int j = 0; j < A[i].length; ++j) {
A[i][j] += Math.max(i < 1 ? 0 : A[i - 1][j], j < 1 ? 0 : A[i][j - 1]);
}
}
return A[A.length - 1][A[0].length - 1];
}
Enumerate binary trees UniqueBinaryTreesAll.java
public static List<BinaryTree<Integer>> generateAllBinaryTrees(int n) {return generateAllBinaryTreesHelper(1, n);
}
private static List<BinaryTree<Integer>> generateAllBinaryTreesHelper(
int start, int end) {
List<BinaryTree<Integer>> result = new ArrayList<>();
if (start > end) {
result.add(null);
return result;
}
for (int i = start; i <= end; ++i) {
// Tries all possible combinations of left subtrees and right subtrees.
List<BinaryTree<Integer>> leftresult = generateAllBinaryTreesHelper(start,
i - 1);
List<BinaryTree<Integer>> rightresult = generateAllBinaryTreesHelper(
i + 1, end);
for (BinaryTree<Integer> left : leftresult) {
for (BinaryTree<Integer> right : rightresult) {
result.add(new BinaryTree<>(i, left, right));
}
}
}
return result;
}