---
# System prepended metadata

title: 1989. Maximum Number of People That Can Be Caught in Tag
tags: [Leetcode, Two Pointers, Medium, Greedy]

---

# 1989. Maximum Number of People That Can Be Caught in Tag
###### tags: `Leetcode` `Two Pointers` `Medium` `Greedy`
Link: https://leetcode.com/problems/maximum-number-of-people-that-can-be-caught-in-tag/
## 思路 $O(N)$ $O(1)$
非典型的双指针问题
用p1找每个1出现的位置 p2找要catch哪一个0
greedy体现在always找最左边的那一个
## Code
```java=
class Solution {
    public int catchMaximumAmountofPeople(int[] team, int dist) {
        int p1=0, p2=0, res=0;
        for(p1=0; p1<team.length; p1++){
             if(team[p1]==0) continue;
            while(p2<p1-dist) p2++;
            while(p2<p1+dist && p2<team.length && team[p2]==1) p2++;
            if(p2<team.length && team[p2]==0){
                res++;
                p2++;
            }
        }
        return res;
    }
}
```
```python=
class Solution:
    def catchMaximumAmountofPeople(self, team: List[int], dist: int) -> int:
        i, j = 0, 0
        ans = 0
        for i in range(len(team)):
            if team[i]!=1: continue
            while j<i-dist: j += 1
            while j<=i+dist and j<len(team):
                if team[j]==0: 
                    ans += 1
                    j += 1
                    break
                j += 1
        return ans
```