Leetcode Problem 1989. Maximum Number of People That Can Be Caught in Tag

1989. Maximum Number of People That Can Be Caught in Tag

Leetcode Solutions

Two Pointers Approach

  1. Initialize two pointers: itPointer and notItPointer, both starting at 0.
  2. Initialize a variable result to store the number of people caught.
  3. Iterate through the array with itPointer.
    • If the current person is 'not it', continue to the next iteration.
    • If the current person is 'it', move notItPointer forward until it is within the catching distance of itPointer and points to a 'not it' person.
    • If a 'not it' person is found within the distance, increment result and move notItPointer forward to avoid catching the same person again.
  4. Continue this process until itPointer has traversed the entire array.
  5. Return the result.
UML Thumbnail

Greedy Queue Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...