Skip to content

Commit

Permalink
feat: best sum any tree path
Browse files Browse the repository at this point in the history
  • Loading branch information
berrywhj committed Sep 22, 2023
1 parent f596ff0 commit 92a148a
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package algorithmns.DP.LongestArithmeticSubsequence;

import java.util.HashMap;
import java.util.Map;

class LongestArithmeticSubsequence {
public int longestSubsequence(int[] arr, int difference) {
int maxLen = 0;
int ret = 0;
int n = arr.length;
Map<Integer, Integer>[] dp = new HashMap[n];
for (int i = 0; i < n; i++) {
dp[i] = new HashMap<Integer, Integer>();
dp[i].put(0, 1);
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
int localDifference = arr[i] - arr[j];
if (dp[j].containsKey(localDifference)) {
int len = dp[j].get(localDifference);
if (!dp[i].containsKey(localDifference)) {
dp[i].put(localDifference, 1 + len);
} else {
dp[i].put(localDifference, Math.max(dp[i].get(i), 1 + len));
}
} else {
dp[i].put(localDifference, 2);
}
if (dp[i].get(localDifference) > maxLen) {
ret = localDifference;
}
}
}
return ret;
}
}
59 changes: 59 additions & 0 deletions src/algorithmns/DP/Stock/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.util.ArrayList;

class Solution {
public int maxProfit(int k, int[] prices) {
int ret = 0;
ArrayList<int[]>[] dp = new ArrayList[prices.length];

// dp[0][0] = {-prices[0], 0};
ArrayList<int[]> init = new ArrayList<>();
int[] initArr = { -prices[0], 0 };
init.add(initArr);
dp[0] = init;
for (int i = 1; i < prices.length; i++) {
// dp[i] = new ArrayList<>(dp[i-1]);

dp[i] = new ArrayList<>();
for (int it = 0; it < dp[i-1].size(); it++) {
int[] arr = new int[2];
arr[0] = dp[i-1].get(it)[0];
arr[1] = dp[i-1].get(it)[1];
dp[i].add(arr);
}
int arraySizeCache = dp[i].size();
for (int j = 0; j < Math.min(k, arraySizeCache); j++) {
if (j == 0) {
dp[i].get(j)[0] = Math.max(-prices[i], dp[i-1].get(j)[0]);
dp[i].get(j)[1] = Math.max(dp[i-1].get(j)[0] + prices[i], dp[i-1].get(j)[1]);
System.out.println("i: " + i + " j: " + j +". " + dp[i].get(j)[0]);
System.out.println("i: " + i + " j: " + j +". " + dp[i].get(j)[1]);
continue;
}
if (dp[i-1].get(j-1)[1] == 0) {
break;
}
dp[i].get(j)[0] = Math.max(dp[i-1].get(j)[0], dp[i-1].get(j-1)[1] - prices[i]);
dp[i].get(j)[1] = Math.max(dp[i-1].get(j)[1], dp[i-1].get(j)[0] + prices[i]);
System.out.println("i: " + i + " j: " + j +". " + dp[i].get(j)[0]);
System.out.println("i: " + i + " j: " + j +". " + dp[i].get(j)[1]);
}
if (arraySizeCache < k && dp[i-1].get(arraySizeCache-1)[1] > 0) {
int[] additionalArr = new int[2];
additionalArr[0] = dp[i-1].get(arraySizeCache - 1)[1] - prices[i];
dp[i].add(additionalArr);
}

}
for (int i = 0; i < dp[prices.length - 1].size(); i++) {
ret = Math.max(ret, dp[prices.length - 1].get(i)[1]);
}
return ret;
}

public static void main(String[] args) {
Solution sl = new Solution();
int[] prices = {3,2,6,5,0,3};
System.out.println("my test:");
System.out.println(sl.maxProfit(2, prices));
}
}
46 changes: 46 additions & 0 deletions src/algorithmns/graph/BestSumAnyTreePath/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.List;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.Arrays;
import java.util.HashSet;

public class Solution {
int ret;
public int bestSumAnyTreePath(List<Integer> parent, List<Integer> values) {
ret = Integer.MIN_VALUE;
int n = parent.size();
Set<Integer>[] graph = new HashSet[n + 1];
for (int i = 0; i < n + 1; i++) graph[i] = new HashSet<>();
for (int i = 0; i < n; i++) {
int p = parent.get(i);
if (p != -1) {
graph[p].add(i);
}
}
graph[n].add(0);
dfs(graph, values, n);
return ret;
}

public int dfs(Set<Integer>[] graph, List<Integer> values, int curr) {
Set<Integer> children = graph[curr];
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
for (int child: children) {
pq.add(dfs(graph, values, child));
}
if (curr == graph.length - 1) return 0;
int childMax = pq.size() == 0 ? 0 : Math.max(pq.remove(), 0);
int childSecondMax = pq.size() == 0 ? 0 : Math.max(pq.remove(), 0);
int currValue = values.get(curr);
ret = Math.max(ret, currValue + childMax + childSecondMax);
return Math.max(currValue + childMax, 0);
}

public static void main(String[] args) {
Solution solution1 = new Solution();
System.out.println(solution1.bestSumAnyTreePath(Arrays.asList(-1, 0, 0), Arrays.asList(10, 10, 10)));;
System.out.println(solution1.bestSumAnyTreePath(Arrays.asList(-1, 0, 0), Arrays.asList(10, -1, 10)));;
System.out.println(solution1.bestSumAnyTreePath(Arrays.asList(-1, 0, 0), Arrays.asList(-1, 10, 10)));;
System.out.println(solution1.bestSumAnyTreePath(Arrays.asList(-1, 0, 0), Arrays.asList(-2, -1, -3)));;
}
}

0 comments on commit 92a148a

Please sign in to comment.