Skip to content

Commit 9478084

Browse files
authored
Create 1124-longest-well-performing-interval.js
1 parent c833d13 commit 9478084

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} hours
3+
* @return {number}
4+
*/
5+
const longestWPI = function(hours) {
6+
const N = hours.length;
7+
const seen = new Map();
8+
let res = 0;
9+
let score = 0;
10+
for (let i = 0; i < N; i++) {
11+
score += hours[i] > 8 ? 1 : -1;
12+
if (score > 0) {
13+
res = i + 1;
14+
} else {
15+
if (!seen.has(score)) {
16+
seen.set(score, i);
17+
}
18+
if (seen.has(score - 1)) {
19+
res = Math.max(res, i - seen.get(score - 1));
20+
}
21+
}
22+
}
23+
return res;
24+
};

0 commit comments

Comments
 (0)