Approaches

💡

Intuition

Time O(n²)Space O(1)

The brute force approach involves checking every possible subarray to determine if it is a well-performing interval. This means we will count the tiring and non-tiring days for each subarray and check if the tiring days are greater.

⚙️

Algorithm

5 steps
  1. 1Step 1: Initialize a variable to store the maximum length of well-performing intervals.
  2. 2Step 2: Use two nested loops to generate all subarrays of the input array.
  3. 3Step 3: For each subarray, count the number of tiring days (hours > 8) and non-tiring days (hours <= 8).
  4. 4Step 4: If the number of tiring days is greater than non-tiring days, update the maximum length.
  5. 5Step 5: Return the maximum length found.
solution.py14 lines
1def longestWPI(hours):
2    max_length = 0
3    n = len(hours)
4    for i in range(n):
5        tiring_days = 0
6        non_tiring_days = 0
7        for j in range(i, n):
8            if hours[j] > 8:
9                tiring_days += 1
10            else:
11                non_tiring_days += 1
12            if tiring_days > non_tiring_days:
13                max_length = max(max_length, j - i + 1)
14    return max_length

Complexity note: The time complexity is O(n²) because we are using two nested loops to check all subarrays. The space complexity is O(1) as we are using a constant amount of extra space.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.