Skip to content

Commit 8150bc3

Browse files
committed
200523
1 parent 4d16b49 commit 8150bc3

File tree

4 files changed

+277
-40
lines changed

4 files changed

+277
-40
lines changed

src/Contest.java

Lines changed: 142 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,162 @@
1+
import javafx.util.Pair;
2+
13
import java.util.*;
24

35
import java.math.BigInteger;
46

57
class Solution {
6-
public boolean doesValidArrayExist(int[] derived) {
7-
if (derived.length == 1) return derived[0] == 0;
8-
int[] original = new int[derived.length];
9-
Arrays.fill(original, -1);
10-
for (int i = 0; i < derived.length; i++) {
11-
if (derived[i] == 0) {
12-
original[i] = 0;
13-
original[(i + 1) % derived.length] = 0;
8+
9+
HashSet<Pair<Integer, Integer>> edgesToReplace = new HashSet<>();
10+
11+
public int[][] modifiedGraphEdges(int n, int[][] edges, int source, int destination, int target) {
12+
HashMap<Integer, HashMap<Integer, Integer>> adjList = makeAdjList(edges);
13+
LinkedList<Integer> minimumPath = bfs(n, adjList, source, destination, target);
14+
HashSet<Pair<Integer, Integer>> mustReplace = new HashSet<>();
15+
replaceMinimumPath(minimumPath, 0, adjList, target, mustReplace);
16+
17+
for (Pair<Integer, Integer> edgeToReplace : edgesToReplace) {
18+
int from = edgeToReplace.getKey();
19+
int to = edgeToReplace.getValue();
20+
Pair<Integer, Integer> pairKey = new Pair<>(Math.min(from, to), Math.max(from, to));
21+
if (mustReplace.contains(pairKey)) continue;
22+
adjList.get(from).put(to, Integer.MAX_VALUE);
23+
adjList.get(to).put(from, Integer.MAX_VALUE);
24+
}
25+
26+
target -= mustReplace.size();
27+
if (target < 0) return new int[0][0];
28+
29+
30+
for (Pair<Integer, Integer> edgeToReplace : mustReplace) {
31+
int from = edgeToReplace.getKey();
32+
int to = edgeToReplace.getValue();
33+
34+
adjList.get(from).put(to, 1);
35+
adjList.get(to).put(from, 1);
36+
37+
38+
int maxAllowed = Integer.MAX_VALUE;
39+
int replace = 1;
40+
if (target > 0) {
41+
HashMap<Integer, Integer> edgesFrom = adjList.get(from);
42+
for (Map.Entry<Integer, Integer> kv : edgesFrom.entrySet()) {
43+
if (kv.getValue() == -1 || kv.getKey() == to) continue;
44+
maxAllowed = Math.min(maxAllowed, kv.getValue());
45+
}
46+
if (maxAllowed >= target + 1) {
47+
replace = target + 1;
48+
} else {
49+
replace = maxAllowed;
50+
}
51+
target -= replace;
1452
}
53+
adjList.get(from).put(to, replace);
54+
adjList.get(to).put(from, replace);
55+
56+
}
57+
if (target > 0) return new int[0][0];
58+
for (int[] edge : edges) {
59+
int from = edge[0];
60+
int to = edge[1];
61+
edge[2] = adjList.get(from).get(to);
1562
}
16-
return doesValidArrayExistHelper(0, derived, original);
63+
return edges;
64+
1765
}
1866

19-
public boolean doesValidArrayExistHelper(int i, int[] derived, int[] original) {
20-
if (i == derived.length) return true;
67+
private void replaceMinimumPath(
68+
LinkedList<Integer> minimumPath,
69+
int i,
70+
HashMap<Integer, HashMap<Integer, Integer>> adjList,
71+
int target,
72+
HashSet<Pair<Integer, Integer>> mustReplace
73+
) {
74+
if (i == minimumPath.size() - 1) {
75+
return;
76+
}
77+
int from = minimumPath.get(i);
78+
int to = minimumPath.get(i + 1);
79+
int cost = adjList.get(from).get(to);
80+
if (cost == -1) {
81+
mustReplace.add(new Pair<>(Math.min(from, to), Math.max(from, to)));
82+
}
83+
replaceMinimumPath(minimumPath, i + 1, adjList, target - cost, mustReplace);
2184

22-
if (derived[i] == 1) {
23-
if (original[i] == 0 && original[(i + 1) % derived.length] == 0) return false;
24-
if (original[i] == 0) {
25-
if (original[(i + 1) % derived.length] == 1) return false;
26-
original[(i + 1) % derived.length] = 0;
85+
}
86+
87+
private HashMap<Integer, HashMap<Integer, Integer>> makeAdjList(int[][] edges) {
88+
HashMap<Integer, HashMap<Integer, Integer>> adjList = new HashMap<>();
89+
for (int[] edge : edges) {
90+
int from = edge[0];
91+
int to = edge[1];
92+
int weight = edge[2];
93+
adjList.computeIfAbsent(from, (v) -> new HashMap<>()).put(to, weight);
94+
adjList.computeIfAbsent(to, (v) -> new HashMap<>()).put(from, weight);
95+
if (weight == -1) {
96+
edgesToReplace.add(new Pair<>(Math.min(from, to), Math.max(from, to)));
2797
}
28-
int before = (i - 1) < 0 ? derived.length - 1 : i - 1;
29-
int after = (i + 1) % derived.length;
30-
31-
32-
if (original[before] == 0 || original[after] == 0) {
33-
original[i] = 1;
34-
return doesValidArrayExistHelper(i + 1, derived, original);
35-
} else {
36-
// if (original[before] == -1) {
37-
// original[before] = 0;
38-
// original[i] = 1;
39-
// if (doesValidArrayExistHelper(i + 1, derived, original)) {
40-
// return true;
41-
// }
42-
// original[i] = -1;
43-
// original[before] = -1;
44-
// }
45-
if (original[after] == -1) {
46-
original[after] = 0;
47-
original[i] = 1;
98+
}
99+
return adjList;
100+
}
101+
102+
private LinkedList<Integer> bfs(int n, HashMap<Integer, HashMap<Integer, Integer>> adjList, int source, int destination, int target) {
103+
Queue<int[]> q = new LinkedList<>();
104+
q.add(new int[]{source, 0});
105+
HashMap<Integer, Integer> visited = new HashMap<>();
106+
int[] path = new int[n + 1];
107+
Arrays.fill(path, -1);
108+
visited.put(source, -1);
109+
110+
boolean found = false;
111+
while (!q.isEmpty()) {
112+
int size = q.size();
113+
114+
for (int i = 0; i < size; i++) {
115+
int[] info = q.poll();
116+
int current = info[0];
117+
int cost = info[1];
118+
visited.put(current, Math.min(cost, visited.getOrDefault(current, Integer.MAX_VALUE)));
119+
if (current == destination) {
120+
found = true;
121+
continue;
122+
}
123+
124+
HashMap<Integer, Integer> connections = adjList.get(current);
125+
for (Map.Entry<Integer, Integer> kv : connections.entrySet()) {
126+
int to = kv.getKey();
127+
int weight = kv.getValue();
128+
if (weight == -1) {
129+
weight = 1;
130+
131+
}
132+
if (visited.getOrDefault(to, Integer.MAX_VALUE) >= cost + weight) {
133+
path[to] = current;
134+
q.add(new int[]{to, cost + weight});
135+
}
48136
}
49137
}
138+
if (found) break;
50139

51-
return doesValidArrayExistHelper(i + 1, derived, original);
52-
} else {
53-
return doesValidArrayExistHelper(i + 1, derived, original);
54140
}
55141

142+
LinkedList<Integer> pathList = new LinkedList<>();
143+
144+
getPath(path, destination, pathList);
145+
146+
147+
return pathList;
148+
56149
}
150+
151+
private void getPath(int[] path, int i, LinkedList<Integer> pathList) {
152+
pathList.addFirst(i);
153+
if (path[i] == -1) {
154+
return;
155+
}
156+
getPath(path, path[i], pathList);
157+
}
158+
159+
57160
}
58161

59162
public class Contest extends Solution {

src/Main.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import leetcode.editor.en.Q354.RussianDollEnvelopes;
1414
import leetcode.editor.en.Q376.WiggleSubsequence;
1515
import leetcode.editor.en.Q377.CombinationSumIv;
16+
import leetcode.editor.en.Q399.EvaluateDivision;
1617
import leetcode.editor.en.Q474.OnesAndZeroes;
1718
import leetcode.editor.en.Q518.CoinChangeII;
1819
import leetcode.editor.en.Q54.SpiralMatrix;
@@ -35,7 +36,7 @@
3536
public class Main {
3637
public static void main(String[] args) throws IOException {
3738

38-
System.out.println(new IsGraphBipartite().isBipartite(toIntMatrix("[[1,2,3],[0,2],[0,1,3],[0,2]]")));
39+
System.out.println(new Contest().modifiedGraphEdges(5, toIntMatrix("[[4,1,-1],[2,0,-1],[0,3,-1],[4,3,-1]]"), 0, 1, 5));
3940

4041
}
4142

@@ -79,6 +80,15 @@ private static List<Boolean> toBooleanList(String s) {
7980
return res;
8081
}
8182

83+
private static double[] toDoubleArray(String s) {
84+
JSONArray jsonArray = new JSONArray(s);
85+
double[] result = new double[jsonArray.length()];
86+
for (int i = 0; i < jsonArray.length(); i++) {
87+
result[i] = jsonArray.getDouble(i);
88+
}
89+
return result;
90+
}
91+
8292
private static boolean[] toBooleanArray(String s) {
8393
JSONArray jsonArray = new JSONArray(s);
8494
boolean[] result = new boolean[jsonArray.length()];
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package leetcode.editor.en.Q1463;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
10+
private static final int[][] DIRECTIONS = new int[][]{
11+
new int[]{1, -1},
12+
new int[]{1, 0},
13+
new int[]{1, 1}
14+
};
15+
16+
Integer[][][] cache;
17+
18+
public int cherryPickup(int[][] grid) {
19+
cache = new Integer[grid.length][grid[0].length][grid[0].length];
20+
return cherryPickup(0, 0, grid[0].length - 1, grid);
21+
}
22+
23+
private int cherryPickup(int row, int col1, int col2, int[][] grid) {
24+
if (row == grid.length) return 0;
25+
int colBoundary = grid[row].length;
26+
int score = (col1 == col2 ? grid[row][col1] : grid[row][col1] + grid[row][col2]);
27+
if (cache[row][col1][col2] != null) {
28+
return cache[row][col1][col2];
29+
}
30+
int best = score;
31+
for (int[] dir1 : DIRECTIONS) {
32+
int newCol1 = col1 + dir1[1];
33+
if (newCol1 >= 0 && newCol1 < colBoundary) {
34+
for (int[] dir2 : DIRECTIONS) {
35+
int newCol2 = col2 + dir2[1];
36+
if (newCol2 >= 0 && newCol2 < colBoundary) {
37+
best = Math.max(best, score + cherryPickup(row + 1, newCol1, newCol2, grid));
38+
}
39+
}
40+
}
41+
}
42+
cache[row][col1][col2] = best;
43+
44+
return best;
45+
46+
47+
}
48+
49+
50+
}
51+
//leetcode submit region end(Prohibit modification and deletion)
52+
53+
54+
public class CherryPickupIi extends Solution {
55+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package leetcode.editor.en.Q399;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
10+
HashMap<String, HashMap<String, Double>> equationMap = buildEquationMap(equations, values);
11+
double[] result = new double[queries.size()];
12+
int idx = 0;
13+
for (List<String> query : queries) {
14+
String dividend = query.get(0);
15+
String divisor = query.get(1);
16+
if (!equationMap.containsKey(dividend) || !equationMap.containsKey(divisor)) {
17+
result[idx++] = -1.0;
18+
continue;
19+
}
20+
result[idx++] = findValue(dividend, divisor, equationMap, new HashSet<>());
21+
}
22+
23+
return result;
24+
}
25+
26+
27+
private HashMap<String, HashMap<String, Double>> buildEquationMap(List<List<String>> equations, double[] values) {
28+
HashMap<String, HashMap<String, Double>> equationMap = new HashMap<>();
29+
30+
for (int i = 0; i < equations.size(); i++) {
31+
List<String> equation = equations.get(i);
32+
33+
String dividend = equation.get(0);
34+
String divisor = equation.get(1);
35+
double result = values[i];
36+
37+
equationMap.computeIfAbsent(dividend, (v) -> new HashMap<>()).put(divisor, result);
38+
equationMap.computeIfAbsent(divisor, (v) -> new HashMap<>()).put(dividend, (1.0 / result));
39+
}
40+
41+
42+
return equationMap;
43+
}
44+
45+
private double findValue(String dividend, String divisor, HashMap<String, HashMap<String, Double>> equationMap, HashSet<Pair<String, String>> stack) {
46+
HashMap<String, Double> divisors = equationMap.get(dividend);
47+
if (divisors.containsKey(divisor)) return divisors.get(divisor);
48+
Pair<String, String> execKey = new Pair<>(dividend, divisor);
49+
stack.add(execKey);
50+
double ans = -1.0;
51+
for (String dividendOfDivisor : divisors.keySet()) {
52+
if (stack.contains(new Pair<>(dividendOfDivisor, divisor))) continue;
53+
double combine = findValue(dividendOfDivisor, divisor, equationMap, stack);
54+
if (combine != -1) {
55+
combine *= divisors.get(dividendOfDivisor);
56+
stack.remove(execKey);
57+
ans = combine;
58+
break;
59+
}
60+
}
61+
stack.remove(execKey);
62+
return ans;
63+
}
64+
}
65+
//leetcode submit region end(Prohibit modification and deletion)
66+
67+
68+
public class EvaluateDivision extends Solution {
69+
}

0 commit comments

Comments
 (0)