Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 345e6dd

Browse files
committed
Update: Maximize Distance to Closest Person
1 parent 3b8b453 commit 345e6dd

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed
Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
package maximize_distance_to_closest_person
22

33
func maxDistToClosest(seats []int) int {
4-
ans, prev, future, l:= 0, -1, 0, len(seats)
4+
ans, ec:= 0, 0
55
for i, v := range seats {
6-
if v == 1 {
7-
prev = i
8-
} else {
9-
for future < l && seats[future] == 0 || future < i {
10-
future++
11-
}
12-
left, right := l, l
13-
if prev != -1 {
14-
left = i - prev
15-
}
16-
if future != l {
17-
right = future - i
18-
}
19-
if right < left {
20-
left = right
21-
}
22-
if left > ans {
23-
ans = left
6+
if v == 0 {
7+
ec++
8+
} else if ec > 0 {
9+
if i == ec { // 001
10+
ans = ec
11+
} else if (ec+1)/2 > ans { // 101
12+
ans = (ec + 1) / 2
2413
}
14+
ec = 0
2515
}
2616
}
17+
if ec > ans { // 100
18+
ans = ec
19+
}
2720
return ans
2821
}

‎problems/maximize-distance-to-closest-person/maximize_distance_to_closest_person_test.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,26 @@ func TestMaxDistToClosest(t *testing.T) {
1717
input: []int{1, 0, 0, 0},
1818
expected: 3,
1919
},
20-
2120
{
2221
input: []int{0, 0, 0, 1},
2322
expected: 3,
2423
},
24+
{
25+
input: []int{0, 0, 0, 0, 0, 1, 0},
26+
expected: 5,
27+
},
28+
{
29+
input: []int{0, 1, 0, 0, 0, 1, 0},
30+
expected: 2,
31+
},
32+
{
33+
input: []int{0, 1, 0, 0, 0, 0, 0},
34+
expected: 5,
35+
},
36+
{
37+
input: []int{1, 0, 1},
38+
expected: 1,
39+
},
2540
}
2641
for _, tc := range tests {
2742
output := maxDistToClosest(tc.input)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /