Skip to content

Instantly share code, notes, and snippets.

@unilecs
Last active February 14, 2024 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unilecs/78f9a9b725fe26a881f0cab0da531dfe to your computer and use it in GitHub Desktop.
Save unilecs/78f9a9b725fe26a881f0cab0da531dfe to your computer and use it in GitHub Desktop.
Задача: Пункт назначения
using System;
using System.Collections.Generic;
public class Program
{
public static string DestinationCity(IList<IList<string>> paths) {
int len = paths.Count;
// частный случай
if (len == 1) {
return paths[0][1];
}
// запоминаем все города отправления
var set = new HashSet<string>();
for(int i = 0; i < len; i++)
{
set.Add(paths[i][0]);
}
// находим 1й город назначения
for(int i = 0; i < len; i++)
{
if (!set.Contains(paths[i][1])) {
return paths[i][1];
}
}
return "";
}
public static void Main()
{
Console.WriteLine("UniLecs");
// tests
var list1 = new List<IList<string>>()
{
new List<string>() { "B","C" },
new List<string>() { "D", "B" },
new List<string>() { "C", "A" }
};
Console.WriteLine(DestinationCity(list1)); // "A"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment