Skip to content

Commit dc11bd3

Browse files
committed
Add solution #1124
1 parent 64cdbb3 commit dc11bd3

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,141 LeetCode solutions in JavaScript
1+
# 1,142 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -887,6 +887,7 @@
887887
1111|[Maximum Nesting Depth of Two Valid Parentheses Strings](./solutions/1111-maximum-nesting-depth-of-two-valid-parentheses-strings.js)|Medium|
888888
1122|[Relative Sort Array](./solutions/1122-relative-sort-array.js)|Easy|
889889
1123|[Lowest Common Ancestor of Deepest Leaves](./solutions/1123-lowest-common-ancestor-of-deepest-leaves.js)|Medium|
890+
1124|[Longest Well-Performing Interval](./solutions/1124-longest-well-performing-interval.js)|Medium|
890891
1137|[N-th Tribonacci Number](./solutions/1137-n-th-tribonacci-number.js)|Easy|
891892
1143|[Longest Common Subsequence](./solutions/1143-longest-common-subsequence.js)|Medium|
892893
1161|[Maximum Level Sum of a Binary Tree](./solutions/1161-maximum-level-sum-of-a-binary-tree.js)|Medium|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 1124. Longest Well-Performing Interval
3+
* https://leetcode.com/problems/longest-well-performing-interval/
4+
* Difficulty: Medium
5+
*
6+
* We are given hours, a list of the number of hours worked per day for a given employee.
7+
*
8+
* A day is considered to be a tiring day if and only if the number of hours worked is
9+
* (strictly) greater than 8.
10+
*
11+
* A well-performing interval is an interval of days for which the number of tiring days
12+
* is strictly larger than the number of non-tiring days.
13+
*
14+
* Return the length of the longest well-performing interval.
15+
*/
16+
17+
/**
18+
* @param {number[]} hours
19+
* @return {number}
20+
*/
21+
var longestWPI = function(hours) {
22+
let result = 0;
23+
let score = 0;
24+
const seen = new Map();
25+
26+
for (let i = 0; i < hours.length; i++) {
27+
score += hours[i] > 8 ? 1 : -1;
28+
29+
if (score > 0) {
30+
result = i + 1;
31+
} else {
32+
if (!seen.has(score)) {
33+
seen.set(score, i);
34+
}
35+
if (seen.has(score - 1)) {
36+
result = Math.max(result, i - seen.get(score - 1));
37+
}
38+
}
39+
}
40+
41+
return result;
42+
};

0 commit comments

Comments
 (0)