zoukankan      html  css  js  c++  java
  • 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的解。

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

    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

  • 相关阅读:
    [atARC100F]Colorful Sequences
    [atARC103D]Robot Arms
    [atARC107F]Sum of Abs
    [atAGC047F]Rooks
    [loj3278]收获
    [cf809E]Surprise me
    [cf997E]Good Subsegments
    [cf603E]Pastoral Oddities
    Codeforces Round #453
    Educational Codeforces Round 32
  • 原文地址:https://www.cnblogs.com/asenyang/p/11183938.html
Copyright © 2011-2022 走看看