为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode1124. 表现良好的最长时间段 | Longest Well-Performing Interval

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11179548.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

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.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..<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 }

 

posted @ 2019-07-13 09:58  为敢技术  阅读(694)  评论(0编辑  收藏  举报