↓Skip to main content
  1. LeetCode/

LeetCode 1989: Maximum Number of People That Can Be Caught in Tag

·3 mins· ·
LeetCode Medium Two-Pointers Greedy Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-27

  • Submissions: Wrong Answer → Accepted (2026-01-28)

Approach
#

Greedy: process the “it” players left to right, and each catches the leftmost uncaught person in [i - dist, i + dist]. People on the left can only be caught by this player or players further left, so saving them for later is useless; people on the right are left for later players, who have more options.

A pointer j points to the next person who might be caught:

  • Skip all “it” players and people with j < i - dist (too far left; later players cannot reach them either).
  • If j <= i + dist, catch them and advance j.

My Attempts
#

Attempt 1 was driven by the people: left found the next person and right found the first “it” who could catch them. The problem: if nobody can catch that person, right keeps moving past every “it” player until n, when the person should be skipped, not the players.

For team = [0, 0, 1], dist = 1, the person at 0 cannot be caught, but the “it” at 2 can catch the person at 1, so the answer is 1. Searching for someone to catch the person at 0, right passed the “it” at 2 and returned 0.

Driving the loop by the “it” players and letting the people pointer skip people that are too far left passed.

Solution
#

class Solution:
    def catchMaximumAmountofPeople(self, team: List[int], dist: int) -> int:
        n = len(team)
        caught = 0
        j = 0                                      # next candidate person
        for i in range(n):
            if team[i] != 1:
                continue
            while j < n and (team[j] == 1 or j < i - dist):
                j += 1                             # skip "it" players and people too far left
            if j < n and j <= i + dist:
                caught += 1
                j += 1
        return caught

Complexity
#

Time O(n): i and j only move right. Space O(1).

Mistakes to Avoid
#

  • Skip the person who cannot be caught; do not skip the “it” players for them.
  • Each “it” should catch the leftmost person in range, not the nearest.

Interview Explanation
#

I process each “it” player left to right and let them catch the leftmost uncaught person in range, since later players are even less able to reach people on the left. A right-only pointer tracks the next candidate, skipping “it” players and people too far left, and if it is within range the person is caught. Both pointers only move right, so time is O(n).

Related

LeetCode 948: Bag of Tokens
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 948 note: after sorting, buy score with the cheapest token and buy power with the most expensive one; trade for power only when score > 0, and stop when neither move works
LeetCode 611: Valid Triangle Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Greedy
LeetCode 611 note: sort, fix the longest side k, and use two pointers for the shorter sides; when the sum is large enough every i in between works, adding j - i at once
LeetCode 11: Container With Most Water
·2 mins
LeetCode Medium Array Two-Pointers Greedy
LeetCode 11 note: start pointers at both ends and always move the shorter side; moving the taller side only narrows the width without raising the height
LeetCode 1877: Minimize Maximum Pair Sum in Array
·1 min
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 1877 note: after sorting, pair the smallest with the largest, the second smallest with the second largest, and take the largest pair sum with converging pointers
LeetCode 2410: Maximum Matching of Players With Trainers
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 2410 note: sort both sides, then same-direction pointers match the weakest player with the smallest trainer that is good enough
LeetCode 1471: The k Strongest Values in an Array
·2 mins
LeetCode Medium Two-Pointers Sorting Array
LeetCode 1471 note: after sorting, the value farthest from the median is always at one of the ends, so converging pointers pick the stronger end k times