在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ We are given A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 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: 3 Explanation: The longest well-performing interval is [9,9,6]. Constraints:
给你一份工作时间表 我们认为当员工一天中的工作小时数大于 所谓「表现良好的时间段」,意味在这段时间内,「劳累的天数」是严格 大于「不劳累的天数」。 请你返回「表现良好时间段」的最大长度。 示例 1: 输入:hours = [9,9,6,0,6,6,9] 输出:3 解释:最长的表现良好时间段是 [9,9,6]。 提示:
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..<hours.count { 8 wellDays += hours[i] > 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] = i 18 } 19 } 20 if maxInterval == 0 { 21 maxInterval = hours.filter{$0 > 8}.count >= 1 ? 1 : 0 22 } 23 return maxInterval 24 } 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..<n { 9 score += hours[i] > 8 ? 1 : -1 10 if score > 0 { 11 result = i + 1 12 } 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 result 22 } 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..<hours.count { 7 if hours[i] > 8 { 8 countAboveTiringDays += 1 9 } else { 10 countAboveTiringDays -= 1 11 } 12 13 // if the count is positive, then it is just the number of days 14 if countAboveTiringDays > 0 { 15 maxLength = i + 1 16 } else { 17 18 if memo[countAboveTiringDays - 1] != nil { 19 maxLength = max(maxLength, i - memo[countAboveTiringDays - 1]!) 20 } 21 22 if memo[countAboveTiringDays] == nil 23 { 24 memo[countAboveTiringDays] = i 25 } 26 27 } 28 } 29 return maxLength 30 } 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<Int>(repeating: 0, count: size + 1) 10 for i in 0..<size { 11 sum[i + 1] = sum[i] + hoursCopy[i] 12 } 13 for i in 0..<size { 14 var j = size 15 while j > i && j - i > ans { 16 if sum[i] < sum[j] { 17 ans = j - i 18 } 19 j -= 1 20 } 21 } 22 return ans 23 } 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..<hours.count 5 { 6 if hours[i] > 8 {hours[i] = 1} 7 else {hours[i] = -1} 8 } 9 var ans:Int = 0 10 for i in 0..<hours.count 11 { 12 var bal:Int = 0 13 var cur:Int = -1 14 for j in i..<hours.count 15 { 16 bal += hours[j] 17 if bal > 0 18 { 19 cur = j 20 } 21 } 22 ans = max(ans, cur - i + 1) 23 } 24 return ans 25 } 26 }
|
请发表评论