File tree 3 files changed +64
-0
lines changed
solution/0800-0899/0849.Maximize Distance to Closest Person
3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -167,6 +167,29 @@ func max(a, b int) int {
167
167
}
168
168
```
169
169
170
+ ### ** TypeScript**
171
+
172
+ ``` ts
173
+ function maxDistToClosest(seats : number []): number {
174
+ let first = - 1 ,
175
+ last = - 1 ;
176
+ let d = 0 ,
177
+ n = seats .length ;
178
+ for (let i = 0 ; i < n ; ++ i ) {
179
+ if (seats [i ] === 1 ) {
180
+ if (last !== - 1 ) {
181
+ d = Math .max (d , i - last );
182
+ }
183
+ if (first === - 1 ) {
184
+ first = i ;
185
+ }
186
+ last = i ;
187
+ }
188
+ }
189
+ return Math .max (Math .floor (d / 2 ), Math .max (first , n - last - 1 ));
190
+ }
191
+ ```
192
+
170
193
### ** ...**
171
194
172
195
```
Original file line number Diff line number Diff line change @@ -147,6 +147,29 @@ func max(a, b int) int {
147
147
}
148
148
```
149
149
150
+ ### ** TypeScript**
151
+
152
+ ``` ts
153
+ function maxDistToClosest(seats : number []): number {
154
+ let first = - 1 ,
155
+ last = - 1 ;
156
+ let d = 0 ,
157
+ n = seats .length ;
158
+ for (let i = 0 ; i < n ; ++ i ) {
159
+ if (seats [i ] === 1 ) {
160
+ if (last !== - 1 ) {
161
+ d = Math .max (d , i - last );
162
+ }
163
+ if (first === - 1 ) {
164
+ first = i ;
165
+ }
166
+ last = i ;
167
+ }
168
+ }
169
+ return Math .max (Math .floor (d / 2 ), Math .max (first , n - last - 1 ));
170
+ }
171
+ ```
172
+
150
173
### ** ...**
151
174
152
175
```
Original file line number Diff line number Diff line change
1
+ function maxDistToClosest ( seats : number [ ] ) : number {
2
+ let first = - 1 ,
3
+ last = - 1 ;
4
+ let d = 0 ,
5
+ n = seats . length ;
6
+ for ( let i = 0 ; i < n ; ++ i ) {
7
+ if ( seats [ i ] === 1 ) {
8
+ if ( last !== - 1 ) {
9
+ d = Math . max ( d , i - last ) ;
10
+ }
11
+ if ( first === - 1 ) {
12
+ first = i ;
13
+ }
14
+ last = i ;
15
+ }
16
+ }
17
+ return Math . max ( Math . floor ( d / 2 ) , Math . max ( first , n - last - 1 ) ) ;
18
+ }
You can’t perform that action at this time.
0 commit comments