Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CSTest
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- TestCase();
- Console.ReadKey();
- }
- private static readonly char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
- private static string[] AvailableParkingSpaces(int columns, int rows, string[] busyParkingSpaces, TimeSpan[] startTimes,
- TimeSpan[] endTimes, TimeSpan newParkingTime, int newParkingDuration)
- {
- IDictionary<string, List<TimeSpan>> parkingPlaces = new Dictionary<string, List<TimeSpan>>();
- for (int i = 0; i < columns; i++)
- {
- for (int j = 0; j < rows; j++)
- {
- parkingPlaces.Add(alpha[i].ToString() + (j + 1).ToString(), null);
- }
- }
- for (int i = 0; i < busyParkingSpaces.Length; i++)
- {
- if (parkingPlaces[busyParkingSpaces[i]] == null)
- {
- parkingPlaces[busyParkingSpaces[i]] = new List<TimeSpan>();
- }
- parkingPlaces[busyParkingSpaces[i]].Add(startTimes[i]);
- parkingPlaces[busyParkingSpaces[i]].Add(endTimes[i]);
- }
- List<string> availablePlaces = new List<string>();
- foreach (var place in parkingPlaces)
- {
- if (place.Value != null)
- {
- if (IsAllOk(place, newParkingTime, newParkingDuration))
- {
- availablePlaces.Add(place.Key);
- }
- }
- else
- {
- availablePlaces.Add(place.Key);
- }
- }
- return availablePlaces.ToArray();
- }
- private static bool IsAllOk(KeyValuePair<string, List<TimeSpan>> place, TimeSpan parkingTime, int duration)
- {
- for (int j = 0; j < place.Value.Count; j += 2)
- {
- if (place.Value[j] <= parkingTime && place.Value[j + 1] > parkingTime)
- {
- return false;
- }
- if (place.Value[j] > parkingTime)
- {
- if (place.Value[j] < parkingTime + TimeSpan.FromMinutes(duration))
- {
- return false;
- }
- }
- }
- return true;
- }
- public static void TestCase()
- {
- string[] busyIntervals = new string[] { "B1", "C3", "A2", "B3", "C3", "A1", "B1", "C2", "A3" };
- TimeSpan[] startTimes = new TimeSpan[] { TimeSpan.FromHours(10), TimeSpan.FromHours(15.5), TimeSpan.FromHours(6),
- TimeSpan.FromHours(9), TimeSpan.FromHours(8), TimeSpan.FromHours(4), TimeSpan.FromHours(14), TimeSpan.FromHours(10),
- TimeSpan.FromHours(5) };
- TimeSpan[] endTimes = new TimeSpan[] { TimeSpan.FromHours(14), TimeSpan.FromHours(20), TimeSpan.FromHours(12.3),
- TimeSpan.FromHours(10), TimeSpan.FromHours(11.5), TimeSpan.FromHours(16.75), TimeSpan.FromHours(18), TimeSpan.FromHours(22),
- TimeSpan.FromHours(17) };
- string[] outArray = AvailableParkingSpaces(3, 3, busyIntervals, startTimes, endTimes, TimeSpan.FromHours(11), 330);
- Console.WriteLine(string.Join(", ", outArray));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement