forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2086.java
46 lines (45 loc) · 1.85 KB
/
_2086.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.fishercoder.solutions;
public class _2086 {
public static class Solution1 {
public int minimumBuckets(String street) {
int minBuckets = 0;
int[] buckets = new int[street.length()];
for (int i = 0; i < street.length(); i++) {
if (street.charAt(i) == 'H') {
if (i + 1 < street.length() && street.charAt(i + 1) == '.') {
if (i - 1 >= 0 && buckets[i - 1] == 1) {
continue;
}
buckets[i + 1] = 1;
minBuckets++;
} else if (i + 1 < street.length() && street.charAt(i + 1) == 'H') {
if (i - 1 < 0) {
return -1;
} else if (i - 1 >= 0 && street.charAt(i - 1) == 'H') {
return -1;
} else {
if (buckets[i - 1] == 1) {
continue;
} else {
buckets[i - 1] = 1;
minBuckets++;
}
}
} else if (i + 1 >= street.length() && i - 1 < 0) {
return -1;
} else if (i - 1 >= 0 && street.charAt(i - 1) == '.') {
if (buckets[i - 1] == 1) {
continue;
} else {
minBuckets++;
buckets[i - 1] = 1;
}
} else if (i + 1 >= street.length() && i - 1 >= 0 && street.charAt(i - 1) == 'H') {
return -1;
}
}
}
return minBuckets;
}
}
}