1 class Solution:
 2     def longestWPI(self, hours: 'List[int]') -> int:
 3         longest = 0
 4         hours = [0] + hours
 5         n = len(hours)
 6         for i in range(n):
 7             if i == 0:
 8                 continue
 9             pre = hours[i-1]
10             if pre > 8:
11                 continue
12             act = 0
13             neg = 0
14             curlong = 0
15             borderlong = 0
16             begin,end = 1,n-1
17             for j in range(i,n):
18                 if hours[j] > 8:
19                     act += 1
20                 else:
21                     neg += 1
22                 if act > neg:
23                     borderlong = j - i + 1
24                     if borderlong > longest:
25                         longest = max(longest,borderlong)
26                         begin = i
27                         end = j
28                         if end == n - 1:
29                             return longest
30                         else:
31                             i = begin
32                 
33         return longest

这道题目没太看懂,尝试了几次,只能做出TLE的解。

看了一下别人的方案,也没弄明白,这题很迷。

14%的Acceptance,我估计不少参赛的选手都遇到了障碍。

最近几期的leetcode周赛和双周赛,题目质量总体来说不是很好,有些题目简单的没有算法思想,有些题目又很怪很偏。

对于大神们来讲,“什么妖魔鬼怪什么美女画皮,什么刀山火海什么陷阱诡计”,都挡不住大神们的火眼金睛和如意棒。

对于像我这样的菜鸟,跟这样的妖题怪题难题动手,未能够建功立业,反误了卿卿性命。

降妖除魔的任务就交给大神们了,我等凡夫俗子,还是做好围观群众的本职工作吧。

 1 class Solution:
 2     def longestWPI(self, hours: List[int]) -> int:
 3         res = score = 0
 4         seen = {}
 5         for i, h in enumerate(hours):
 6             score = score + 1 if h > 8 else score - 1
 7             if score > 0:
 8                 res = i + 1
 9             seen.setdefault(score, i)
10             if score - 1 in seen:
11                 res = max(res, i - seen[score - 1])
12         return res

参考:https://leetcode.com/problems/longest-well-performing-interval/discuss/334565/JavaC%2B%2BPython-O(N)-Solution-Life-needs-996-and-669

posted on 2019-07-14 12:59  Sempron2800+  阅读(414)  评论(0编辑  收藏  举报