leetcode1124

 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的解。算法

看了一下別人的方案,也沒弄明白,這題很迷。curl

14%的Acceptance,我估計很多參賽的選手都遇到了障礙。url

最近幾期的leetcode周賽和雙週賽,題目質量整體來講不是很好,有些題目簡單的沒有算法思想,有些題目又很怪很偏。spa

對於大神們來說,「什麼妖魔鬼怪什麼美女畫皮,什麼刀山火海什麼陷阱詭計」,都擋不住大神們的火眼金睛和如意棒。code

對於像我這樣的菜鳥,跟這樣的妖題怪題難題動手,未可以建功立業,反誤了卿卿性命。orm

降妖除魔的任務就交給大神們了,我等凡夫俗子,仍是作好圍觀羣衆的本職工做吧。blog

 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-669leetcode

相關文章
相關標籤/搜索