-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDestinationCity.cpp
More file actions
28 lines (27 loc) · 805 Bytes
/
DestinationCity.cpp
File metadata and controls
28 lines (27 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// https://leetcode.com/problems/destination-city
// O(n^2) time complexity for two for loops and O(1) space for extra variable
// Second solution is O(n) time using hash set and O(n) for space because of it
string destCity(vector<vector<string>>& paths) {
// bool flag = true;
// for(int i = 0; i < paths.size(); i++)
// {
// flag = true;
// for(int j = 0; j < paths.size(); j++)
// {
// if(paths[i][1] == paths[j][0])
// flag = false;
// }
// if(flag)
// return paths[i][1];
// }
// return "";
unordered_set<string> hash;
for(auto& e : paths)
hash.insert(e[0]);
for(auto& e : paths)
{
if(hash.find(e[1]) == hash.end())
return e[1];
}
return "";
}