public int FindMostFrequentSleepMinute(out int count) { int maxIndex = GuardShift.GetMaxIndex(); int[] sleepCountArray = new int[maxIndex]; foreach (GuardShift shift in GuardShiftList) { for (int i = 0; i < maxIndex; i++) { if (shift.IsSleep(i)) { sleepCountArray[i]++; } } } int index = -1; int maxSleepCount = -1; // Find index with max sleep for (int i = 0; i < maxIndex; i++) { if (sleepCountArray[i] > maxSleepCount) { maxSleepCount = sleepCountArray[i]; index = i; } } count = maxSleepCount; return(index); }
public void Process() { GuardShift currentGuardShift = null; int startSleepMinute = -1; foreach (GuardRecord rec in GuardRecordList) { if (rec.RecordType == GuardRecord.Type.StartShift) { Guard guard = GetCreateGuard(rec); currentGuardShift = new GuardShift(); currentGuardShift.DateTime = rec.DateTime; guard.AddShift(currentGuardShift); startSleepMinute = -1; } else if (rec.RecordType == GuardRecord.Type.StartSleep) { if (rec.DateTime.Hour == 0) { startSleepMinute = rec.DateTime.Minute; } else { startSleepMinute = 0; // if guard falls asleep before midnight } } else if (rec.RecordType == GuardRecord.Type.EndSleep) { int endSleepMinute = rec.DateTime.Minute - 1; // -1 ref requirement if (startSleepMinute >= 0 && startSleepMinute <= 59 && endSleepMinute >= 0 && endSleepMinute <= 59 && startSleepMinute <= endSleepMinute) { currentGuardShift.SetSleepInterval(startSleepMinute, endSleepMinute, true); } startSleepMinute = -1; } } MostSleepyGuard = null; int maxSleepMinutes = -1; foreach (Guard guard in GuardList) { int countSleepMinutes = guard.CountSleepMinutes(); if (countSleepMinutes > maxSleepMinutes) { maxSleepMinutes = countSleepMinutes; MostSleepyGuard = guard; } } MostFreqSleepGuard = null; int maxFreqSleepCount = -1; foreach (Guard guard in GuardList) { int mostFreqSleepCount; int mostFreqSleepMinute = guard.FindMostFrequentSleepMinute(out mostFreqSleepCount); if (mostFreqSleepCount > maxFreqSleepCount) { maxFreqSleepCount = mostFreqSleepCount; MostFreqSleepGuard = guard; } } }
public void AddShift(GuardShift shift) { GuardShiftList.Add(shift); }