From d4e1ac9b56cd76f3794546f7598df92b89f5996e Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Mon, 22 Sep 2025 16:35:48 +0800 Subject: [PATCH] Add solution for "Destination City" with unit tests. --- src/DestinationCity1436.java | 31 ----------- .../leetcode/easy/DestinationCity1436.java | 32 ++++++++++++ .../easy/DestinationCity1436Test.java | 51 +++++++++++++++++++ 3 files changed, 83 insertions(+), 31 deletions(-) delete mode 100644 src/DestinationCity1436.java create mode 100644 src/main/java/com/leetcode/easy/DestinationCity1436.java create mode 100644 src/test/java/com/leetcode/easy/DestinationCity1436Test.java diff --git a/src/DestinationCity1436.java b/src/DestinationCity1436.java deleted file mode 100644 index b159a27..0000000 --- a/src/DestinationCity1436.java +++ /dev/null @@ -1,31 +0,0 @@ -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class DestinationCity1436 { - public String destCity(List> paths) { - Map> map = new HashMap<>(); - for (List path : paths) { - String src = path.get(0); - String dst = path.get(1); - if (!map.containsKey(src)) { - List dsts = new ArrayList<>(); - dsts.add(dst); - map.put(src, dsts); - } else { - List dsts = map.get(src); - dsts.add(dst); - map.put(src, dsts); - } - } - for (Map.Entry> entry : map.entrySet()) { - for (String dst : entry.getValue()) { - if (!map.containsKey(dst)) { - return dst; - } - } - } - return ""; - } -} diff --git a/src/main/java/com/leetcode/easy/DestinationCity1436.java b/src/main/java/com/leetcode/easy/DestinationCity1436.java new file mode 100644 index 0000000..b30294f --- /dev/null +++ b/src/main/java/com/leetcode/easy/DestinationCity1436.java @@ -0,0 +1,32 @@ +// Tags: Array, Hash Set +package com.leetcode.easy; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class DestinationCity1436 { + /* + Given n as the length of paths, + + Time complexity: O(n) + We first iterate over paths to populate hasOutgoing, this costs O(n). + Next, we iterate over paths again to find the answer, checking at each step whether candidate is in the hash set, which takes O(1). Thus the iteration costs O(n). + + Space complexity: O(n) + hasOutgoing will grow to a size of O(n). + */ + public String destCity(List> paths) { + Set hasOutgoing = new HashSet<>(); + for (List path : paths) { + hasOutgoing.add(path.get(0)); + } + for (List path : paths) { + String candidate = path.get(1); + if (!hasOutgoing.contains(candidate)) { + return candidate; + } + } + return ""; + } +} diff --git a/src/test/java/com/leetcode/easy/DestinationCity1436Test.java b/src/test/java/com/leetcode/easy/DestinationCity1436Test.java new file mode 100644 index 0000000..8e21994 --- /dev/null +++ b/src/test/java/com/leetcode/easy/DestinationCity1436Test.java @@ -0,0 +1,51 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DestinationCity1436Test { + private DestinationCity1436 solution; + + @BeforeEach + void setUp() { + solution = new DestinationCity1436(); + } + + @Test + void testDestCity_Example1() { + List> paths = List.of( + List.of("London", "New York"), + List.of("New York", "Lima"), + List.of("Lima", "Sao Paulo") + ); + String expected = "Sao Paulo"; + String result = solution.destCity(paths); + assertEquals(expected, result); + } + + @Test + void testDestCity_Example2() { + List> paths = List.of( + List.of("B", "C"), + List.of("D", "B"), + List.of("C", "A") + ); + String expected = "A"; + String result = solution.destCity(paths); + assertEquals(expected, result); + } + + @Test + void testDestCity_Example3() { + List> paths = List.of( + List.of("A", "Z") + ); + String expected = "Z"; + String result = solution.destCity(paths); + assertEquals(expected, result); + } +}