★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝()➤GitHub地址:➤原文地址: ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★We are given hours
, a list of the number of hours worked per day for a given employee.
A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8
.
A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days.
Return the length of the longest well-performing interval.
Example 1:
Input: hours = [9,9,6,0,6,6,9]Output: 3Explanation: The longest well-performing interval is [9,9,6].
Constraints:
1 <= hours.length <= 10000
0 <= hours[i] <= 16
给你一份工作时间表 hours
,上面记录着某一位员工每天的工作小时数。
我们认为当员工一天中的工作小时数大于 8
小时的时候,那么这一天就是「劳累的一天」。
所谓「表现良好的时间段」,意味在这段时间内,「劳累的天数」是严格 大于「不劳累的天数」。
请你返回「表现良好时间段」的最大长度。
示例 1:
输入:hours = [9,9,6,0,6,6,9]输出:3解释:最长的表现良好时间段是 [9,9,6]。
提示:
1 <= hours.length <= 10000
0 <= hours[i] <= 16
140ms
1 class Solution { 2 func longestWPI(_ hours: [Int]) -> Int { 3 var maxInterval = 0 4 var wellDays = 0 5 var dict = [Int: Int]() 6 7 for i in 0..8 ? 1 : -1 9 if(wellDays >= 1) {10 maxInterval = max(maxInterval, i + 1);11 }12 13 if dict[wellDays] != nil {14 guard let maxSum = dict[wellDays-1] else { continue }15 maxInterval = max(maxInterval, i-maxSum)16 } else {17 dict[wellDays] = i18 }19 }20 if maxInterval == 0 {21 maxInterval = hours.filter{$0 > 8}.count >= 1 ? 1 : 022 }23 return maxInterval24 }25 }
148ms
1 class Solution { 2 func longestWPI(_ hours: [Int]) -> Int { 3 var result = 0 4 var score = 0 5 var n = hours.count 6 var seen = [Int: Int]() 7 8 for i in 0..8 ? 1 : -110 if score > 0 {11 result = i + 112 } else {13 if seen[score] == nil {14 seen[score] = i 15 }16 if let idx = seen[score - 1] {17 result = max(result, i - idx)18 }19 }20 }21 return result22 }23 }
164ms
1 class Solution { 2 func longestWPI(_ hours: [Int]) -> Int { 3 var memo : [Int:Int] = [0:-1] 4 var maxLength = 0 5 var countAboveTiringDays = 0 6 for i in 0..8 { 8 countAboveTiringDays += 1 9 } else {10 countAboveTiringDays -= 111 }12 13 // if the count is positive, then it is just the number of days14 if countAboveTiringDays > 0 {15 maxLength = i + 116 } else {17 18 if memo[countAboveTiringDays - 1] != nil {19 maxLength = max(maxLength, i - memo[countAboveTiringDays - 1]!)20 }21 22 if memo[countAboveTiringDays] == nil23 {24 memo[countAboveTiringDays] = i25 }26 27 }28 }29 return maxLength30 }31 }
1900ms
1 class Solution { 2 func longestWPI(_ hours: [Int]) -> Int { 3 var ans = 0 4 let size = hours.count 5 let hoursCopy = hours.map { (val) -> Int in 6 val > 8 ? 1 : -1 7 } 8 9 var sum = Array (repeating: 0, count: size + 1)10 for i in 0..i && j - i > ans {16 if sum[i] < sum[j] {17 ans = j - i18 }19 j -= 120 }21 }22 return ans23 }24 }
Runtime: 3896 ms
Memory Usage: 21.2 MB
1 class Solution { 2 func longestWPI(_ hours: [Int]) -> Int { 3 var hours = hours 4 for i in 0..8 {hours[i] = 1} 7 else {hours[i] = -1} 8 } 9 var ans:Int = 010 for i in 0.. 018 {19 cur = j20 }21 }22 ans = max(ans, cur - i + 1)23 }24 return ans 25 }26 }