Maximum Number of People That Can Be Caught in Tag - Problem
You are playing a game of tag with your friends. In tag, people are divided into two teams: people who are "it", and people who are not "it".
The people who are "it" want to catch as many people as possible who are not "it".
You are given a 0-indexed integer array team containing only zeros (denoting people who are not "it") and ones (denoting people who are "it"), and an integer dist.
A person who is "it" at index i can catch any one person whose index is in the range [i - dist, i + dist] (inclusive) and is not "it".
Return the maximum number of people that the people who are "it" can catch.
Input & Output
Example 1 — Basic Case
$
Input:
team = [0,1,0,1,0], dist = 1
›
Output:
2
💡 Note:
Guard at position 1 can catch person at position 0 or 2. Guard at position 3 can catch person at position 2 or 4. Using greedy strategy: guard 1 catches position 2, guard 3 catches position 4. Total: 2 catches.
Example 2 — Overlapping Ranges
$
Input:
team = [1,0,0,0,1], dist = 2
›
Output:
3
💡 Note:
Guard at position 0 can catch positions 1 or 2. Guard at position 4 can catch positions 2 or 3. Guard 0 catches position 2 (rightmost), guard 4 catches position 3, and position 1 remains. Actually, guard 0 should catch position 1, guard 4 catches positions 2,3. Total: 3 catches.
Example 3 — Single Guard
$
Input:
team = [1,0,0], dist = 1
›
Output:
1
💡 Note:
Only one guard at position 0. Can catch person at position 1 within range [0,1]. Total: 1 catch.
Constraints
- 1 ≤ team.length ≤ 105
- team[i] is either 0 or 1
- 1 ≤ dist ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array with guards (1) and people (0), plus distance range
2
Process
Each guard catches one person within their distance range
3
Output
Maximum total number of people that can be caught
Key Takeaway
🎯 Key Insight: Greedy assignment (rightmost first) prevents conflicts and maximizes total catches
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code