I need help with the following problem. When I have a list of elements which contains two values, that represents the start and endpoint of a bridge. These Values are representing a "bridge". So for example [0,1] means a bridge connects island0 with island1.
How can I iterate through that list starting with 0 and only sum up the values which are connected and ignoring the other ones?
Example 1 - Every Island is indirectly connected
[0,1]
[1,2]
[2,3]
[3,4]
Solution should be: 0+1+2+3+4 = 10
Example 2
[0,1]
[1,2]
[3,4] !!! NOT CONNECTED
Solution should be: 0+1+2 = 3
Thanks!!!
try this simple solution
import java.util.ArrayList;
import java.util.List;
public class TestBridge {
public static final class Bridge {
int start;
int end;
Bridge(int start, int end) {
this.start = start;
this.end = end;
}
}
private static int getSum(List<Bridge> bridges) {
int sum = 0;
int prevEnd = -1;
for (Bridge bridge : bridges) {
int actualStart = bridge.start;
int actualEnd = bridge.end;
if (actualStart == 0 || prevEnd == actualStart) {
prevEnd = actualEnd;
sum += actualEnd;
}
}
return sum;
}
private static List<Bridge> getFirstExample() {
List<Bridge> bridges = new ArrayList<>();
bridges.add(new Bridge(0, 1));
bridges.add(new Bridge(1, 2));
bridges.add(new Bridge(2, 3));
bridges.add(new Bridge(3, 4));
return bridges;
}
private static List<Bridge> getSecondExample() {
List<Bridge> bridges = new ArrayList<>();
bridges.add(new Bridge(0, 1));
bridges.add(new Bridge(1, 2));
bridges.add(new Bridge(3, 4));
return bridges;
}
public static void main(String[] args) {
System.out.println(getSum(getFirstExample()));
System.out.println(getSum(getSecondExample()));
}
}
Hagen??
With some contraints it could be easy, without it can become more complicated. With constraints I mean the first bridge should be the source of truth. Just think about those cases:
[0,1]
[1,2]
[3,4]
[4,5]
(two parts internally connected with the same length)
[0,1]
[1,2]
[3,4]
[4,5]
(The first Bridge is the excluded one)
What happens if Bridges 1+2 are connected and 3+4 are connected but not both parts together? I mean this...
[0,1]
[2,3]
[3,4]
[4,5]
Assuming the first bridge as the source of thruth you can try this:
public class IslandConnector {
private final List<Bridge> includedBridges = new ArrayList<>();
private List<Bridge> excludedBridges;
public IslandConnector(List<Bridge> bridges) {
this.excludedBridges = new ArrayList<>();
for(Bridge bridge : bridges) {
if(includedBridges.isEmpty()) {
includedBridges.add(bridge);
}
else {
if(!tryIncludeBridge(bridge)) {
excludedBridges.add(bridge);
}
}
}
}
private boolean tryIncludeBridge(Bridge bridge) {
for(Bridge includedBridge: includedBridges) {
if(bridge.hasSameS(includedBridge)) {
includeBridge(bridge);
return true;
}
}
return false;
}
private void includeBridge(Bridge bridge) {
includedBridges.add(bridge);
for(Bridge excludedBridge: excludedBridges) {
if(bridge.hasSameS(excludedBridge)) {
excludedBridges.remove(excludedBridge);
includeBridge(excludedBridge);
}
}
}
public List<Bridge> getIncludedBridges() {
return includedBridges;
}
public static void main(String... args) {
System.out.println(new IslandConnector(Arrays.asList(
new Bridge(0, 1),
new Bridge(1, 2),
new Bridge(2, 3),
new Bridge(3, 4)
)).getIncludedBridges());
System.out.println(new IslandConnector(Arrays.asList(
new Bridge(0, 1),
new Bridge(1, 2),
new Bridge(3, 4)
)).getIncludedBridges());
System.out.println(new IslandConnector(Arrays.asList(
new Bridge(0, 1),
new Bridge(2, 3),
new Bridge(3, 4)
)).getIncludedBridges());
}
}
printing
[[0,1], [1,2], [2,3], [3,4]]
[[0,1], [1,2]]
[[0,1]]
Try this solution with HashMap
public class BridgeSolution {
public static void main(String[] args) {
Map<Integer, Integer> bridgeMap = new HashMap<>();
List<Integer[]> bridges = new ArrayList<>();
bridges.add(new Integer[]{0, 1});
bridges.add(new Integer[]{1, 2});
bridges.add(new Integer[]{2, 3});
bridges.add(new Integer[]{3, 4});
for (int i = 0; i < bridges.size(); i++) {
bridgeMap.put(bridges.get(i)[0], bridges.get(i)[1]);
}
Integer start = bridges.get(0)[0];
Integer value = null;
Integer sum = start;
while ((value = bridgeMap.get(start)) != null) {
sum += value;
start = value;
}
System.out.println(sum);
}
}
Having the following class:
public class Graph<V> {
private static class Edge<VV> {
private final VV from;
private final VV to;
private Edge(VV from, VV to) {
this.from = from;
this.to = to;
}
}
private final Set<V> vertices = new HashSet<>();
private final Collection<Edge<V>> edges = new ArrayList<>();
public void addEdge(V from, V to) {
vertices.add(from);
vertices.add(to);
edges.add(new Edge(from, to));
}
public Set<V> closure(V start) {
Set<V> result = new HashSet<>();
closure(start, result);
return result;
}
private void closure(V start, Set<V> seen) {
if (vertices.contains(start) && !seen.contains(start)) {
seen.add(start);
for (Edge<V> e: edges) {
if (start.equals(e.from)) {
closure(e.to, seen);
}
}
}
}
}
You can do this:
Graph<Integer> graph = new Graph<>();
graph.addEdge(0,1);
graph.addEdge(1,2);
graph.addEdge(3,4);
int sum = 0;
for (int v: graph.closure(0)) {
System.out.println("Vertex: " + v);
sum += v;
}
System.out.println("Sum: " + sum);
With the following output:
Vertex: 0
Vertex: 1
Vertex: 2
Sum: 3
Try the below. Change the Bridge
object to suit your requirements
public class Bridge {
int start;
int end;
public Bridge(int start, int end) {
this.start = start;
this.end = end;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
}
Calculate the sum
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class BridgeSum {
public static void main(String... args) throws IOException {
Bridge B1 = new Bridge(0, 1);
Bridge B2 = new Bridge(1, 2);
Bridge B3 = new Bridge(2, 4);
Bridge B4 = new Bridge(4, 8);
Bridge B5 = new Bridge(8, 9);
List<Bridge> list = new ArrayList<>();
list.add(B5);
list.add(B2);
list.add(B3);
list.add(B1);
list.add(B4);
list.sort((a, b) -> a.getStart() > b.getStart() ? 0 : -1);
int sum = 0;
for (int i = 0; i < list.size(); i++) {
if (i == 0) {
sum = sum + list.get(i).getStart() + list.get(i).getEnd();
} else {
if (list.get(i).getStart() == list.get(i - 1).getEnd()) {
sum = sum + list.get(i).getEnd();
} else {
break;
}
}
}
System.out.println(sum);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.