Similar Problems

Similar Problems not available

Sleep - Leetcode Solution

Companies:

LeetCode:  Sleep Leetcode Solution

Difficulty: Unknown

Topics: unknown  

Problem Statement:

Given a list of activities represented as intervals [start, end], where each interval refers to a sleep period in hours, and a desired minimum hours of sleep per day, return the minimum number of activities necessary to achieve the desired amount of sleep per day.

Example:

Input: intervals = [[1,3],[2,5],[3,7],[4,8],[5,9],[6,10]], hours = 3 Output: 3 Explanation: Using the first, third, and fifth sleep intervals will result in 3 or more hours of sleep each day.

Approach:

We can solve this problem by using the greedy approach. We first sort the intervals based on their end times because we want to select the sleep periods that end the earliest.

Then we iterate from the earliest sleep period to the latest one and start selecting the intervals that end before the start of the next sleep period. We keep track of the amount of sleep we have accumulated so far and stop when we meet or exceed the desired minimum hours of sleep. If we reach the last sleep period and still haven't slept enough, then we return -1 to indicate that it is impossible to reach the desired minimum hours of sleep.

Algorithm:

  1. Sort the intervals based on their end times.
  2. Initialize a count variable to 0.
  3. Iterate over the intervals: a. If the end time of the current interval is less than or equal to the start time of the next interval, and the length of the interval is greater than or equal to the desired minimum hours of sleep, then increment the count by 1. b. If the total number of hours slept so far is greater than or equal to the desired minimum hours of sleep, return the count.
  4. If we reach the end of the intervals and still haven't met the desired minimum hours of sleep, return -1.

Time Complexity:

The time complexity of this algorithm is O(nlogn) where n is the number of intervals. This is because we sort the intervals before iterating over them.

Space Complexity:

The space complexity of this algorithm is O(1) because we don't use any additional space other than some variables to keep track of the count and total number of hours slept.

Code:

Here is the Python code for the above algorithm:

def min_sleep(intervals, hours):
    intervals.sort(key=lambda x: x[1])
    count = 0
    total_hours_slept = 0
    for i in intervals:
        if i[1] <= hours and i[1] - i[0] >= hours:
            count += 1
            total_hours_slept += i[1] - i[0]
        elif i[0] < hours and i[1] - hours >= hours:
            count += 1
            total_hours_slept += hours
        if total_hours_slept >= 24:
            return count
    return -1

This code uses a lambda function to sort the intervals based on their end times. Then it iterates over the intervals and adds up the total number of hours slept and increments the count whenever it finds an interval that satisfies the conditions mentioned above. Finally, it returns either the count or -1 depending on whether the desired minimum hours of sleep were met.

Sleep Solution Code

1