Skip to content

Commit d38d588

Browse files
committed
fd
1 parent 47b1823 commit d38d588

File tree

3 files changed

+79
-17
lines changed

3 files changed

+79
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@
397397
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Java](leetcode/solution/src/MostCommonWord.java)|85||
398398
|843|[Guess the Word](https://leetcode.com/problems/guess-the-word/)|[Java](leetcode/solution/src/GuessTheWord.java)|90||
399399
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Java](leetcode/solution/src/BackspaceStringCompare.java)|100||
400+
|849|[Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/)|[Java](leetcode/solution/src/MaximizeDistanceToClosestPerson.java)|80||
400401
|857|[Minimum Cost to Hire K Workers](https://leetcode.com/problems/minimum-cost-to-hire-k-workers/)|[Java](leetcode/solution/src/MinimumCostToHireKWorkers.java|70||
401402
|904|[Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/)|[Java](leetcode/solution/src/FruitIntoBaskets.java)|90||
402403
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Java](leetcode/solution/src/UniqueEmailAddresses.java)|90||
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.Arrays;
2+
3+
public class MaximizeDistanceToClosestPerson {
4+
5+
/**
6+
* 这题要注意两个边界的情况,因为如果是非边界的情况,要坐在中间的
7+
*/
8+
public int maxDistToClosest(int[] seats) {
9+
int max = 0;
10+
11+
int[] zone = new int[2];
12+
for (int index = 0; index < seats.length; ) {
13+
Arrays.fill(zone, -1);
14+
index = nextFree(seats, index, zone);
15+
if (zone[0] == 0 || zone[1] == seats.length - 1) {
16+
max = Math.max(max, zone[1] - zone[0] + 1);
17+
} else {
18+
max = Math.max(max, (zone[1] - zone[0] + 2) / 2);
19+
}
20+
}
21+
22+
return max;
23+
}
24+
25+
/**
26+
* 返回从start开始的连续的0区间
27+
*/
28+
private int nextFree(int[] seats, int start, int[] zone) {
29+
boolean enter = false;
30+
for (int i = start, j = 0; i <= seats.length; i++) {
31+
if (i < seats.length && seats[i] == 0) {
32+
if (!enter) {
33+
enter = true;
34+
j = i;
35+
}
36+
} else {
37+
if (enter) {
38+
zone[0] = j;
39+
zone[1] = i - 1;
40+
return i + 1;
41+
}
42+
}
43+
}
44+
return seats.length;
45+
}
46+
}

leetcode/src/Main.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,46 @@ public class Main {
66

77
public static class Solution {
88

9-
10-
public int candy(int[] ratings) {
11-
int[] candys = new int[ratings.length];
12-
13-
Arrays.fill(candys, 1);
14-
15-
for (int i = 1; i < ratings.length; i++) {
16-
if (ratings[i] > ratings[i - 1]) {
17-
candys[i] = candys[i - 1] + 1;
9+
public int maxDistToClosest(int[] seats) {
10+
int max = 0;
11+
12+
int[] zone = new int[2];
13+
for (int index = 0; index < seats.length; ) {
14+
Arrays.fill(zone, -1);
15+
index = nextFree(seats, index, zone);
16+
if (zone[0] == 0 || zone[1] == seats.length - 1) {
17+
max = Math.max(max, zone[1] - zone[0] + 1);
18+
} else {
19+
max = Math.max(max, (zone[1] - zone[0] + 2) / 2);
1820
}
1921
}
20-
int sum = candys[ratings.length - 1];
21-
for (int i = ratings.length - 2; i >= 0; i--) {
22-
if (ratings[i] > ratings[i + 1]) {
23-
candys[i] = Math.max(candys[i], candys[i + 1] + 1);
22+
23+
return max;
24+
}
25+
26+
private int nextFree(int[] seats, int start, int[] zone) {
27+
boolean enter = false;
28+
for (int i = start, j = 0; i <= seats.length; i++) {
29+
if (i < seats.length && seats[i] == 0) {
30+
if (!enter) {
31+
enter = true;
32+
j = i;
33+
}
34+
} else {
35+
if (enter) {
36+
zone[0] = j;
37+
zone[1] = i - 1;
38+
return i + 1;
39+
}
2440
}
25-
sum += candys[i];
2641
}
27-
return sum;
42+
return seats.length;
2843
}
2944
}
3045

3146
public static void main(String[] args) {
3247
Solution solution = new Solution();
33-
System.out.println(solution.candy(new int[] {1, 3, 2, 2, 1}));
34-
// System.out.println(solution.candy(new int[] {1, 2, 2}));
48+
int s = solution.maxDistToClosest(new int[]{1,0,0,0,1,0,1});
49+
System.out.println(s);
3550
}
3651
}

0 commit comments

Comments
 (0)