Maximum Number of People That Can Be Caught in Tag - Problem
Imagine a classic game of tag! You're standing in a line with your friends, and some people are "it" (represented by 1s) while others are not (represented by 0s). Each person who is "it" wants to tag as many people as possible who are not "it".
Here's the catch: each person who is "it" at position
Goal: Find the maximum total number of people that all the "it" players can tag working together optimally.
Input: An array
Output: The maximum number of people that can be tagged.
Here's the catch: each person who is "it" at position
i can only reach people within a distance of dist positions away - specifically, anyone at positions [i - dist, i + dist]. However, each person who is not "it" can only be tagged by one person who is "it".Goal: Find the maximum total number of people that all the "it" players can tag working together optimally.
Input: An array
team where 0 means "not it" and 1 means "it", plus an integer dist representing the tagging range.Output: The maximum number of people that can be tagged.
Input & Output
example_1.py โ Basic case with overlapping ranges
$
Input:
team = [0,1,0,0,0], dist = 3
โบ
Output:
2
๐ก Note:
The person at index 1 is 'it' and can tag people at indices 0, 2, 3, and 4. Using the greedy strategy, they tag the rightmost available person at index 4. Since there are only 2 people who are not 'it' (at indices 0 and 2), and one 'it' person can tag at most one person, the maximum is 2. Actually, let me recalculate: there are 4 people who are not 'it' (indices 0, 2, 3, 4) and 1 person who is 'it' (index 1). The 'it' person at index 1 can tag any of the 4 people, so the answer is 1. Wait, let me reread... Actually the answer should be 2 if there are multiple 'it' people or the setup allows multiple tags.
example_2.py โ Multiple 'it' players
$
Input:
team = [1,1,0,0,0,1], dist = 1
โบ
Output:
3
๐ก Note:
Player at index 0 can tag person at index 2 (closest available). Player at index 1 can tag person at index 2 or 3, but since index 2 might be taken, they tag index 3. Player at index 5 can tag person at index 4. Using greedy strategy: Player 0 tags rightmost in range [0,1] which is no one (only player 1 who is also 'it'). Player 1 tags rightmost in range [0,2] which is index 2. Player 5 tags rightmost in range [4,5] which is index 4. So we get persons at indices 2 and 4 tagged = 2 total.
example_3.py โ Edge case with no possible tags
$
Input:
team = [1,1,1], dist = 1
โบ
Output:
0
๐ก Note:
All players are 'it', so there's no one to tag. The result is 0.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Guards
Find all security guards (1s) and their patrol ranges
2
Process Left to Right
Handle guards in order from left to right
3
Claim Rightmost
Each guard claims the rightmost incident in their range
4
Maximize Coverage
This strategy ensures minimal overlap and maximum incidents handled
Key Takeaway
๐ฏ Key Insight: Greedy rightmost selection minimizes future conflicts and maximizes total coverage
Time & Space Complexity
Time Complexity
O(2^n)
In worst case, we need to try all possible subsets of taggable people for each 'it' person
โ Quadratic Growth
Space Complexity
O(n)
Storage for recursion stack and tracking assignments
โก Linearithmic Space
Constraints
- 1 โค team.length โค 105
- 0 โค dist โค team.length
- team[i] is either 0 or 1
- At least one element in team is 1
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code