Skip to content

Commit a2b6e77

Browse files
authored
Create Solution.java
1 parent 75349e0 commit a2b6e77

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int longestWPI(int[] hours) {
3+
int res = 0;
4+
Map<Integer, Integer> map = new HashMap<>();
5+
int s = 0;
6+
for (int i = 0; i < hours.length; ++i) {
7+
s += hours[i] > 8 ? 1 : -1;
8+
if (s > 0) {
9+
res = i + 1;
10+
} else {
11+
int b = map.getOrDefault(s - 1, -1);
12+
if (b != -1) {
13+
res = Math.max(res, i - b);
14+
}
15+
}
16+
map.putIfAbsent(s, i);
17+
}
18+
return res;
19+
}
20+
}

0 commit comments

Comments
 (0)