Assign Umbrella on Rainy Days Problem
#4116
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 4116.
You are given a binary array rain of length n, where rain[i] == 1 means it rains on day i, and rain[i] == 0 means it does not.
Alice and Bob share a single umbrella over these n days. On each day i:
- Alice needs the umbrella on day
iifrain[i] == rain[i + 1] == 1for0 <= i < n - 1. - Bob needs the umbrella if
rain[i] == 1. - At most one of them can receive the umbrella.
- If only one person needs it, that person receives it.
- If both need it, it goes to whoever has received it fewer times so far. If they are tied, it goes to Alice.
- If neither needs it, no one receives it.
A person's count increases only on a day they receive the umbrella. Initially, both counts are 0.
Return a string array answer of length n, where answer[i] is "Alice", "Bob", or "None", denoting who receives the umbrella on day i.
A binary array is an array that contains only 0 and 1.
Example 1:
Input: rain = [1,1,0,1,1]
Output: ["Alice","Bob","None","Alice","Bob"]
Explanation:
- Day 0: rain[0] == 1 and rain[1] == 1, so both need it. Counts are tied, so Alice receives it.
- Day 1: rain[1] == 1, so Bob needs it, but rain[2] == 0, so Alice does not need it. Therefore Bob receives it.
- Day 2: No rain, so no one receives it.
- Day 3: rain[3] == 1 and rain[4] == 1, so both need it. Counts are tied at 1, so Alice receives it.
- Day 4: Last day, so only Bob needs it, and he receives it.
- Therefore, answer = ["Alice", "Bob", "None", "Alice", "Bob"].
Example 2:
Input: rain = [0,1,0,1]
Output: ["None","Bob","None","Bob"]
Explanation:
- It never rains on two consecutive days, so Alice never needs the umbrella.
- Bob receives it on days 1 and 3, and no one receives it on days 0 and 2.
- Therefore, answer = ["None", "Bob", "None", "Bob"].
Constraints:
1 <= n == rain.length <= 100rain[i]is either 0 or 1.
Solution
Approach: Simulation
Keep a and b, the number of times Alice and Bob have received the umbrella so far. Both start at 0. Scan rain from left to right.
- If
rain[i] == 0, nobody needs the umbrella: the answer is"None". - Otherwise Bob needs it. Alice needs it too exactly when
i < n - 1andrain[i + 1] == 1. In that case Alice gets it ifa <= b(a tie goes to Alice), andagoes up by one. - In every other case only Bob needs it (or Alice has received it more often), so Bob gets it and
bgoes up by one.
Complexity: O(n) time and O(1) extra space.
class Solution:
def assignUmbrella(self, rain: List[int]) -> List[str]:
n = len(rain)
a = b = 0
ans = [None] * n
for i, x in enumerate(rain):
if x == 0:
ans[i] = "None"
elif a <= b and i < n - 1 and rain[i + 1] == 1:
ans[i] = "Alice"
a += 1
else:
ans[i] = "Bob"
b += 1
return ans