Skip to content

Commit b80876b

Browse files
author
Fan, Zhou
committed
wef
1 parent f8db304 commit b80876b

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

CF/algNotes/assets/kruskal.png

110 KB
Loading

CF/algNotes/hf1.md

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
![subset](./assets/kruskal.png)
22
```java
33

44
/*************Valid Word Abbreviation******************************
@@ -71,7 +71,8 @@ Output: [[1,1],[2,5],[4,4]]
7171
7272
******************cloneTree***********************************
7373
74-
******************cloneGraph***********************************
74+
******************Minimum Spanning Tree***********************************
75+
7576
*/
7677

7778

@@ -396,8 +397,14 @@ public RandomListNode copyRandomList(RandomListNode head) {
396397

397398
}
398399

399-
400-
400+
/**
401+
* Definition for undirected graph.
402+
* class UndirectedGraphNode {
403+
* int label;
404+
* ArrayList<UndirectedGraphNode> neighbors;
405+
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
406+
* };
407+
*/
401408
public class Solution {
402409
/*
403410
* @param node: A undirected graph node
@@ -448,4 +455,89 @@ public RandomListNode copyRandomList(RandomListNode head) {
448455
return new ArrayList<UndirectedGraphNode>(set);
449456
}
450457
}
458+
459+
460+
/**
461+
* Definition for a Connection.
462+
* public class Connection {
463+
* public String city1, city2;
464+
* public int cost;
465+
* public Connection(String city1, String city2, int cost) {
466+
* this.city1 = city1;
467+
* this.city2 = city2;
468+
* this.cost = cost;
469+
* }
470+
* }
471+
*/
472+
public class Solution {
473+
/**
474+
* @param connections given a list of connections include two cities and cost
475+
* @return a list of connections from results
476+
*/
477+
478+
public List<Connection> lowestCost(List<Connection> connections) {
479+
// Write your code here
480+
List<Connection> res = new ArrayList<Connection>();
481+
482+
Collections.sort(connections, new Comparator<Connection>() {
483+
public int compare(Connection a, Connection b) {
484+
if (a.cost != b.cost) {
485+
return a.cost - b.cost;
486+
}
487+
if (!a.city1.equals(b.city1)) {
488+
return a.city1.compareTo(b.city1);
489+
}
490+
return a.city2.compareTo(b.city2);
491+
}
492+
});
493+
494+
Map<String, String> father = new HashMap<>();
495+
for (Connection con : connections) {
496+
String root1 = find(con.city1, father), root2 = find(con.city2, father);
497+
System.out.println(root1 + " " + root2);
498+
if (!root1.equals(root2)) {
499+
father.put(root1, root2);
500+
res.add(con);
501+
}
502+
}
503+
504+
if (res.size() != father.size() - 1) {
505+
return new ArrayList<Connection>();
506+
}
507+
508+
return res;
509+
}
510+
511+
private String find(String str, Map<String, String> father) {
512+
if (!father.containsKey(str)) {
513+
father.put(str, str);
514+
} else if (!father.get(str).equals(str)) {
515+
father.put(str, find(father.get(str), father));
516+
System.out.println(str + " + " + find(father.get(str), father));
517+
}
518+
519+
return father.get(str);
520+
}
521+
522+
}
523+
524+
Input
525+
Show Difference
526+
["Acity","Bcity",1]
527+
["Acity","Ccity",2]
528+
["Bcity","Ccity",3]
529+
Your stdout
530+
Acity Bcity
531+
Acity + Bcity
532+
Bcity Ccity
533+
Bcity + Ccity
534+
Ccity Ccity
535+
Output
536+
["Acity","Bcity",1]
537+
["Acity","Ccity",2]
538+
Expected
539+
["Acity","Bcity",1]
540+
["Acity","Ccity",2]
541+
542+
451543
```

0 commit comments

Comments
 (0)