[LeetCode] Maximum Number of People That Can Be Caught in Tag

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

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
int catchMaximumAmountofPeople(vector<int>& team, int dist) {
set<int> nit;
for(int i = 0; i < team.size(); i++) {
if(team[i] == 0) nit.insert(i);
}
int res = 0;
for(int i = 0; i < team.size() and !nit.empty(); i++) {
if(team[i] == 0) continue;
auto p = nit.lower_bound(i-dist);
if(p == end(nit)) break;
if(*p <= i + dist) {
res++;
nit.erase(p);
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/12/PS/LeetCode/maximum-number-of-people-that-can-be-caught-in-tag/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.