#39Hospital Resource Allocation
A hospital continuously records the number of medical resources consumed during each time interval. The monitoring system stores these values in an array resourceUsage. The hospital wants to identify two separate, non-overlapping time periods whose total resource usage is exactly equal to targetUsage. Each period must contain one or more consecutive intervals. The two selected periods must not share any interval.
Among all possible pairs, return the minimum combined number of intervals used by the two periods. If no two valid non-overlapping periods can be found, return -1.
Real-World Applications: Hospital Resource Monitoring Medical Supply Planning Healthcare Operations Analysis
Examples
Example 1
Input: resourceUsage = [3,2,2,4,3]
targetUsage = 3
Output: 2
Explanation: The two single-interval periods [3] and [3] both have the required usage. Their combined length is 1 + 1 = 2.
Example 2
Input: resourceUsage = [7,3,4,7]
targetUsage = 7
Output: 2
Explanation: Two non-overlapping periods [7] and [7] can be selected, giving a minimum combined length of 2.
Example 3
Input: resourceUsage = [4,3,2,6,2,3,4]
targetUsage = 6
Output: -1
Explanation: Only one valid period has a total usage of 6, so two non-overlapping periods cannot be selected.
Example 4
Input: resourceUsage = [1,1,1,1,1,1]
targetUsage = 2
Output: 4
Explanation: Two separate periods [1,1] can be selected. Each has length 2, so the combined length is 4.
Constraints
- 1 <= resourceUsage.length <= 100000
- 1 <= resourceUsage[i] <= 1000
- 1 <= targetUsage <= 100000000
- Each selected period must be non-empty.
- The two periods must not overlap.
- Each period must have a sum exactly equal to targetUsage.
- Return the minimum combined length of the two periods.
- Return -1 if fewer than two valid non-overlapping periods exist.
