Skip to content

Commit e95ff09

Browse files
add 849
1 parent ba80cda commit e95ff09

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Your ideas/fixes/algorithms are more than welcome!
8686
|860|[Lemonade Change](https://leetcode.com/problems/lemonade-change/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_860.java) | O(n) | O(1) | |Easy|
8787
|859|[Buddy Strings](https://leetcode.com/problems/buddy-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_859.java) | O(n) | O(n) | |Easy|
8888
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_852.java) | O(n) | O(1) | |Easy|
89+
|849|[Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_849.java) | O(n^2) | O(1) | |Easy|
8990
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | O(n) | O(1) | |Easy|
9091
|840|[Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_840.java) | O(1) | O(1) | |Easy|
9192
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | O(n) | O(1) | |Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 849. Maximize Distance to Closest Person
5+
*
6+
* In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty.
7+
* There is at least one empty seat, and at least one person sitting.
8+
* Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.
9+
* Return that maximum distance to closest person.
10+
*
11+
* Example 1:
12+
* Input: [1,0,0,0,1,0,1]
13+
* Output: 2
14+
* Explanation:
15+
* If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.
16+
* If Alex sits in any other open seat, the closest person has distance 1.
17+
* Thus, the maximum distance to the closest person is 2.
18+
*
19+
* Example 2:
20+
* Input: [1,0,0,0]
21+
* Output: 3
22+
* Explanation:
23+
* If Alex sits in the last seat, the closest person is 3 seats away.
24+
* This is the maximum distance possible, so the answer is 3.
25+
* Note:
26+
*
27+
* 1 <= seats.length <= 20000
28+
* seats contains only 0s or 1s, at least one 0, and at least one 1.
29+
* */
30+
public class _849 {
31+
public static class Solution1 {
32+
int maxDist = 0;
33+
public int maxDistToClosest(int[] seats) {
34+
for (int i = 0; i < seats.length; i++) {
35+
if (seats[i] == 0) {
36+
extend(seats, i);
37+
}
38+
}
39+
return maxDist;
40+
}
41+
42+
private void extend(int[] seats, int position) {
43+
int left = position - 1;
44+
int right = position + 1;
45+
int leftMinDistance = 1;
46+
while (left >= 0) {
47+
if (seats[left] == 0) {
48+
leftMinDistance++;
49+
left--;
50+
} else {
51+
break;
52+
}
53+
}
54+
int rightMinDistance = 1;
55+
while (right < seats.length) {
56+
if (seats[right] == 0) {
57+
rightMinDistance++;
58+
right++;
59+
} else {
60+
break;
61+
}
62+
}
63+
int maxReach = 0;
64+
if (position == 0) {
65+
maxReach = rightMinDistance;
66+
} else if (position == seats.length - 1) {
67+
maxReach = leftMinDistance;
68+
} else {
69+
maxReach = Math.min(leftMinDistance, rightMinDistance);
70+
}
71+
maxDist = Math.max(maxDist, maxReach);
72+
}
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._849;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _849Test {
10+
11+
private static _849.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _849.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(2, solution1.maxDistToClosest(new int[]{1, 0, 0, 0, 1, 0, 1}));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(3, solution1.maxDistToClosest(new int[]{1, 0, 0, 0}));
26+
}
27+
}

0 commit comments

Comments
 (0)