Skip to content

Commit 5a40ef8

Browse files
authored
feat: add ts solution to lc problem: No.0849 (doocs#1483)
1 parent 34cff77 commit 5a40ef8

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

solution/0800-0899/0849.Maximize Distance to Closest Person/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,29 @@ func max(a, b int) int {
167167
}
168168
```
169169

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+
170193
### **...**
171194

172195
```

solution/0800-0899/0849.Maximize Distance to Closest Person/README_EN.md

+23
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,29 @@ func max(a, b int) int {
147147
}
148148
```
149149

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+
150173
### **...**
151174

152175
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

0 commit comments

Comments
 (0)