File tree 6 files changed +241
-2
lines changed
solution/1900-1999/1989.Maximum Number of People That Can Be Caught in Tag
6 files changed +241
-2
lines changed Original file line number Diff line number Diff line change 58
58
59
59
<!-- 这里可写通用的实现逻辑 -->
60
60
61
+ ** 方法一:双指针**
62
+
63
+ 我们可以用两个指针 $i$ 和 $j$ 指向鬼和非鬼的人,初始时 $i=0$, $j=0$。
64
+
65
+ 然后我们从左到右遍历数组,当遇到鬼时,即 $team[ i] =1$ 时,如果此时 $j \lt n$ 并且 $team[ j] =1$ 或者 $i - j \gt dist$,则指针 $j$ 循环右移,也即是说,我们要找到第一个不是鬼的人,且 $i$ 和 $j$ 之间的距离不超过 $dist$。如果找到了这样的人,则将指针 $j$ 右移一位,表示我们已经抓住了这个人,同时答案加一。继续遍历数组,直到遍历完整个数组。
66
+
67
+ 最后返回答案即可。
68
+
69
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
70
+
61
71
<!-- tabs:start -->
62
72
63
73
### ** Python3**
64
74
65
75
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
76
67
77
``` python
68
-
78
+ class Solution :
79
+ def catchMaximumAmountofPeople (self , team : List[int ], dist : int ) -> int :
80
+ ans = j = 0
81
+ n = len (team)
82
+ for i, x in enumerate (team):
83
+ if x:
84
+ while j < n and (team[j] or i - j > dist):
85
+ j += 1
86
+ if j < n and abs (i - j) <= dist:
87
+ ans += 1
88
+ j += 1
89
+ return ans
69
90
```
70
91
71
92
### ** Java**
72
93
73
94
<!-- 这里可写当前语言的特殊实现逻辑 -->
74
95
75
96
``` java
97
+ class Solution {
98
+ public int catchMaximumAmountofPeople (int [] team , int dist ) {
99
+ int ans = 0 ;
100
+ int n = team. length;
101
+ for (int i = 0 , j = 0 ; i < n; ++ i) {
102
+ if (team[i] == 1 ) {
103
+ while (j < n && (team[j] == 1 || i - j > dist)) {
104
+ ++ j;
105
+ }
106
+ if (j < n && Math . abs(i - j) <= dist) {
107
+ ++ ans;
108
+ ++ j;
109
+ }
110
+ }
111
+ }
112
+ return ans;
113
+ }
114
+ }
115
+ ```
116
+
117
+ ### ** C++**
118
+
119
+ ``` cpp
120
+ class Solution {
121
+ public:
122
+ int catchMaximumAmountofPeople(vector<int >& team, int dist) {
123
+ int ans = 0;
124
+ int n = team.size();
125
+ for (int i = 0, j = 0; i < n; ++i) {
126
+ if (team[ i] ) {
127
+ while (j < n && (team[ j] || i - j > dist)) {
128
+ ++j;
129
+ }
130
+ if (j < n && abs(i - j) <= dist) {
131
+ ++ans;
132
+ ++j;
133
+ }
134
+ }
135
+ }
136
+ return ans;
137
+ }
138
+ };
139
+ ```
76
140
141
+ ### **Go**
142
+
143
+ ```go
144
+ func catchMaximumAmountofPeople(team []int, dist int) (ans int) {
145
+ n := len(team)
146
+ for i, j := 0, 0; i < n; i++ {
147
+ if team[i] == 1 {
148
+ for j < n && (team[j] == 1 || i-j > dist) {
149
+ j++
150
+ }
151
+ if j < n && abs(i-j) <= dist {
152
+ ans++
153
+ j++
154
+ }
155
+ }
156
+ }
157
+ return
158
+ }
159
+
160
+ func abs(x int) int {
161
+ if x < 0 {
162
+ return -x
163
+ }
164
+ return x
165
+ }
77
166
```
78
167
79
168
### ** ...**
Original file line number Diff line number Diff line change @@ -57,13 +57,92 @@ There are no people who are not "it" to catch.
57
57
### ** Python3**
58
58
59
59
``` python
60
-
60
+ class Solution :
61
+ def catchMaximumAmountofPeople (self , team : List[int ], dist : int ) -> int :
62
+ ans = j = 0
63
+ n = len (team)
64
+ for i, x in enumerate (team):
65
+ if x:
66
+ while j < n and (team[j] or i - j > dist):
67
+ j += 1
68
+ if j < n and abs (i - j) <= dist:
69
+ ans += 1
70
+ j += 1
71
+ return ans
61
72
```
62
73
63
74
### ** Java**
64
75
65
76
``` java
77
+ class Solution {
78
+ public int catchMaximumAmountofPeople (int [] team , int dist ) {
79
+ int ans = 0 ;
80
+ int n = team. length;
81
+ for (int i = 0 , j = 0 ; i < n; ++ i) {
82
+ if (team[i] == 1 ) {
83
+ while (j < n && (team[j] == 1 || i - j > dist)) {
84
+ ++ j;
85
+ }
86
+ if (j < n && Math . abs(i - j) <= dist) {
87
+ ++ ans;
88
+ ++ j;
89
+ }
90
+ }
91
+ }
92
+ return ans;
93
+ }
94
+ }
95
+ ```
96
+
97
+ ### ** C++**
98
+
99
+ ``` cpp
100
+ class Solution {
101
+ public:
102
+ int catchMaximumAmountofPeople(vector<int >& team, int dist) {
103
+ int ans = 0;
104
+ int n = team.size();
105
+ for (int i = 0, j = 0; i < n; ++i) {
106
+ if (team[ i] ) {
107
+ while (j < n && (team[ j] || i - j > dist)) {
108
+ ++j;
109
+ }
110
+ if (j < n && abs(i - j) <= dist) {
111
+ ++ans;
112
+ ++j;
113
+ }
114
+ }
115
+ }
116
+ return ans;
117
+ }
118
+ };
119
+ ```
66
120
121
+ ### **Go**
122
+
123
+ ```go
124
+ func catchMaximumAmountofPeople(team []int, dist int) (ans int) {
125
+ n := len(team)
126
+ for i, j := 0, 0; i < n; i++ {
127
+ if team[i] == 1 {
128
+ for j < n && (team[j] == 1 || i-j > dist) {
129
+ j++
130
+ }
131
+ if j < n && abs(i-j) <= dist {
132
+ ans++
133
+ j++
134
+ }
135
+ }
136
+ }
137
+ return
138
+ }
139
+
140
+ func abs(x int) int {
141
+ if x < 0 {
142
+ return -x
143
+ }
144
+ return x
145
+ }
67
146
```
68
147
69
148
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int catchMaximumAmountofPeople (vector<int >& team, int dist) {
4
+ int ans = 0 ;
5
+ int n = team.size ();
6
+ for (int i = 0 , j = 0 ; i < n; ++i) {
7
+ if (team[i]) {
8
+ while (j < n && (team[j] || i - j > dist)) {
9
+ ++j;
10
+ }
11
+ if (j < n && abs (i - j) <= dist) {
12
+ ++ans;
13
+ ++j;
14
+ }
15
+ }
16
+ }
17
+ return ans;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ func catchMaximumAmountofPeople (team []int , dist int ) (ans int ) {
2
+ n := len (team )
3
+ for i , j := 0 , 0 ; i < n ; i ++ {
4
+ if team [i ] == 1 {
5
+ for j < n && (team [j ] == 1 || i - j > dist ) {
6
+ j ++
7
+ }
8
+ if j < n && abs (i - j ) <= dist {
9
+ ans ++
10
+ j ++
11
+ }
12
+ }
13
+ }
14
+ return
15
+ }
16
+
17
+ func abs (x int ) int {
18
+ if x < 0 {
19
+ return - x
20
+ }
21
+ return x
22
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int catchMaximumAmountofPeople (int [] team , int dist ) {
3
+ int ans = 0 ;
4
+ int n = team .length ;
5
+ for (int i = 0 , j = 0 ; i < n ; ++i ) {
6
+ if (team [i ] == 1 ) {
7
+ while (j < n && (team [j ] == 1 || i - j > dist )) {
8
+ ++j ;
9
+ }
10
+ if (j < n && Math .abs (i - j ) <= dist ) {
11
+ ++ans ;
12
+ ++j ;
13
+ }
14
+ }
15
+ }
16
+ return ans ;
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def catchMaximumAmountofPeople (self , team : List [int ], dist : int ) -> int :
3
+ ans = j = 0
4
+ n = len (team )
5
+ for i , x in enumerate (team ):
6
+ if x :
7
+ while j < n and (team [j ] or i - j > dist ):
8
+ j += 1
9
+ if j < n and abs (i - j ) <= dist :
10
+ ans += 1
11
+ j += 1
12
+ return ans
You can’t perform that action at this time.
0 commit comments