Skip to content

Commit a6aa486

Browse files
committedDec 24, 2024
Add destination city problem
1 parent d5238ac commit a6aa486

File tree

6 files changed

+124
-1
lines changed

6 files changed

+124
-1
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ List of Programs related to data structures and algorithms
6363
| 22 | Range Sum queries | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/22.sumRange/sumRange.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/22.sumRange/sumRange.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/22.sumRange/sumRange.md) | Easy | Array traversal |
6464
| 23 | Disappeared numbers | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/23.disappearedNumbers/disappearedNumbers.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/23.disappearedNumbers/disappearedNumbers.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/23.disappearedNumbers/disappearedNumbers.md) | Easy | Array traversal |
6565
| 24 | Identical pairs | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/24.identicalPairs/identicalPairs.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/24.identicalPairs/identicalPairs.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/24.identicalPairs/identicalPairs.md) | Easy | Array traversal & map |
66+
| 25 | Destination City | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/25.destinationCity/destinationCity.md) | Easy | Array traversal & set |
6667

6768
### String
6869

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package java1.algorithms.array.destinationCity;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class DestinationCity {
7+
private static String destinationCity(String[][] paths){
8+
Set<String> fromCities = new HashSet<>();
9+
10+
for (String[] city : paths) {
11+
fromCities.add(city[0]);
12+
}
13+
14+
for (String[] city : paths) {
15+
if(!fromCities.contains(city[1])){
16+
return city[1];
17+
}
18+
}
19+
20+
return "";
21+
22+
}
23+
24+
public static void main(String[] args) {
25+
String[][] paths1 = new String[][]{{"Hyderabad","KL"},{"KL","Singapore"},{"Singapore","Sydney"}};
26+
String[][] paths2 = new String[][]{{"New Jersey","Austin"},{"New York","New Jersey"},{"Austin","Dallas"}};
27+
String[][] paths3 = new String[][]{{"Dallas", "London"}};
28+
29+
System.out.println(destinationCity(paths1));
30+
System.out.println(destinationCity(paths2));
31+
System.out.println(destinationCity(paths3));
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
**Description:**
2+
Given an array of `paths`, where `paths[i] = [cityAi, cityBi]` indicates that a direct path exists from cityAi to cityBi. Considering the destination city as a city without any path outgoing to another city, return the destination city.
3+
4+
**Note:** Assume that the graph of paths form a line without any loop.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: paths = [["Hyderabad","KL"],["KL","Singapore"],["Singapore","Sydney"]]
10+
Output: Sydney
11+
12+
Example 2:
13+
14+
Input: paths = [["New Jersey","Austin"],["New York","New Jersey"],["Austin","Dallas"]]
15+
Output: Dallas
16+
17+
Example 3:
18+
19+
Input: paths = [["Dallas", "London"]],
20+
Output: London
21+
22+
**Algorithmic Steps**
23+
This problem is solved with the help of array traversal and hash set for storing for finding the destination city. The algorithmic approach can be summarized as follows:
24+
25+
1. Create a function named `destinationCity`, which accepts input array(`paths`) to find the destination city.
26+
27+
2. Initialize a `fromCities` set variale to store the list of from cities in paths. The given paths array is traversed and `fromCities` is populated with all from city values.
28+
29+
3. Iterate over given array(`path`) agin, return the toCity value as destination city if that city doesn't exist inside `fromCities`. This is because only destination city cannot have a outgoing path.
30+
31+
**Time and Space complexity:**
32+
This algorithm has a time complexity of `O(n)`, where `n` is the number of path elements in an array. This is because we need to iterate over all the elements at most twice for finding the destination city.
33+
34+
It takes constant time complexity of `O(n)` due to a set additional datastructure for storing the from cities.

‎src/javascript/algorithms/array/24.identicalPairs/identicalPairs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Example 2:
1414
Input: nums = [2, 2, 2, 2],
1515
Output: 6
1616

17-
Example 2:
17+
Example 3:
1818

1919
Input: nums = [1, 2, 3, 4],
2020
Output: 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function destinationCity(paths){
2+
let fromCities = new Set();
3+
4+
for(const [from, to] of paths) {
5+
fromCities.add(from);
6+
}
7+
8+
for(const [from, to] of paths) {
9+
if(!fromCities.has(to)) {
10+
return to;
11+
}
12+
}
13+
}
14+
15+
const paths1 = [["Hyderabad","KL"],["KL","Singapore"],["Singapore","Sydney"]];
16+
const paths2 = [["New Jersey","Austin"],["New York","New Jersey"],["Austin","Dallas"]];
17+
const paths3 = [["Dallas", "London"]];
18+
19+
console.log(destinationCity(paths1));
20+
console.log(destinationCity(paths2));
21+
console.log(destinationCity(paths3));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
**Description:**
2+
Given an array of `paths`, where `paths[i] = [cityAi, cityBi]` indicates that a direct path exists from cityAi to cityBi. Considering the destination city as a city without any path outgoing to another city, return the destination city.
3+
4+
**Note:** Assume that the graph of paths form a line without any loop.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: paths = [["Hyderabad","KL"],["KL","Singapore"],["Singapore","Sydney"]]
10+
Output: Sydney
11+
12+
Example 2:
13+
14+
Input: paths = [["New Jersey","Austin"],["New York","New Jersey"],["Austin","Dallas"]]
15+
Output: Dallas
16+
17+
Example 3:
18+
19+
Input: paths = [["Dallas", "London"]],
20+
Output: London
21+
22+
**Algorithmic Steps**
23+
This problem is solved with the help of array traversal and hash set for storing for finding the destination city. The algorithmic approach can be summarized as follows:
24+
25+
1. Create a function named `destinationCity`, which accepts input array(`paths`) to find the destination city.
26+
27+
2. Initialize a `fromCities` set variale to store the list of from cities in paths. The given paths array is traversed and `fromCities` is populated with all from city values.
28+
29+
3. Iterate over given array(`path`) agin, return the toCity value as destination city if that city doesn't exist inside `fromCities`. This is because only destination city cannot have a outgoing path.
30+
31+
**Time and Space complexity:**
32+
This algorithm has a time complexity of `O(n)`, where `n` is the number of path elements in an array. This is because we need to iterate over all the elements at most twice for finding the destination city.
33+
34+
It takes constant time complexity of `O(n)` due to a set additional datastructure for storing the from cities.

0 commit comments

Comments
 (0)