aboutsummaryrefslogtreecommitdiff
path: root/destination_city.c
blob: 1c48eec6a4f7c305832a8eaa0ea7803b873f5028 (plain)
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
// 1436. Destination City
#include "leetcode.h"

/*
 * given the array 'paths' where 'paths[i] = [citya[i], cityb[i]]' means there
 * exists a direct path going from 'citya[i]' to 'cityb[i]'. return the
 * destination city that is the city without any path outgoing to another city.
 * it is guaranteed that the graph of paths forms a line without any loop,
 * therefore there will be exactly one destination.
 */

char *destCity(char ***paths, int paths_size, int *paths_col_size) {
  char *dest = paths[0][1];
  for (int i = 1; i < paths_size; i++)
    if (!strcmp(paths[i][0], dest)) {
      dest = paths[i][1];
      i = 0;
    }
  return dest;
}

int main() {
  char ***p1 = {
      {"London", "New York"}, {"New York", "Lima"}, {"Lima", "Sao Paulo"}};
  char ***p2 = {{"B", "C"}, {"D", "B"}, {"C", "A"}};
  printf("%s\n", destCity(p1, 3, NULL));
  printf("%s\n", destCity(p2, 3, NULL));
}