Skip to content

Instantly share code, notes, and snippets.

@chygoz2
Last active May 28, 2021 17:03
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 chygoz2/aadd848512e20879538d480631b26200 to your computer and use it in GitHub Desktop.
Save chygoz2/aadd848512e20879538d480631b26200 to your computer and use it in GitHub Desktop.
Algorithm week 7 challenge
using System;
using System.Linq;
namespace minimumDifference
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Program.getMinimumDifference(new string[] { "16:15", "16:00", "12:20" }));
}
public static int getMinimumDifference(string[] times)
{
if (times.Length == 0)
{
return 0;
}
var validTimes = times
.Where(time => Program.getTimeInMinutes(time).HasValue)
.Select(time => Program.getTimeInMinutes(time).Value)
.ToArray();
if (validTimes.Length == 0)
{
throw new InvalidOperationException("No valid input exists");
}
if (validTimes.Length == 1)
{
return validTimes[0];
}
Array.Sort(validTimes);
int? minimumDifference = null;
for (var i = 0; i < validTimes.Length - 1; i++)
{
var diff = validTimes[i+1] - validTimes[i];
if (!minimumDifference.HasValue) {
minimumDifference = diff;
continue;
}
if (diff < minimumDifference)
{
minimumDifference = diff;
}
}
return minimumDifference.GetValueOrDefault();
}
private static int? getTimeInMinutes(string time)
{
var parts = time.Split(":");
if (parts.Length != 2)
{
return null;
}
try
{
var hour = Convert.ToInt32(parts[0]);
var mins = Convert.ToInt32(parts[1]);
if (hour < 0 || hour > 23 || mins < 0 || mins > 59)
{
return null;
}
return hour * 60 + mins;
}
catch (FormatException)
{
return null;
}
}
}
}
@meekg33k
Copy link

Hello @chygoz2, congratulations 🎉 your solution has been selected as one of the winning solutions in Week 7 of #AlgorithmFridays. Your solution is clean and readable and passed the test cases.

Out of the many winning solutions, only 3 will be selected for the $20 prize in a raffle draw. The raffle draw will hold today, Friday May 28 at 3.00pm WAT (7.00 am PST)

If you are interested in being a part of the raffle draw, please send me a DM on Twitter @meekg33k so I can share the event invite with you.

NB: Only solutions of participants who indicated interest in the raffle draw will be considered.

Thanks once again for participating and see you later today for Week 8 of #AlgorithmFridays.

@chygoz2
Copy link
Author

chygoz2 commented May 28, 2021

Thank you @meekg33k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment