zoukankan      html  css  js  c++  java
  • #LeetCode# Container With Most Water (todo)

    描述:

    实现1 -- 求所有可能的值,O(N^2),超时了(因为超时没有跑所有的测试用例,所以不确定还有没有其他问题)

    代码:

     1     def maxArea(self, height):
     2         tmp = len(height)
     3         if tmp == 0 or tmp == 1: return 0
     4         if tmp == 2: return abs(height[1] - height[0])
     5 
     6         minus_lst = [height[i] - height[i-1] for i in range(1, len(height))]
     7         # len(minus_lst) >= 2
     8 
     9         dis, max, l = 2, 0, len(minus_lst)
    10 
    11         while True:
    12             for i in range(l):
    13                 if i + dis > l: break
    14 
    15                 area = abs(sum(minus_lst[i:i+dis]) * dis)
    16                 if area > max: max = area 
    17 
    18             dis += 1
    19             if dis > l: break
    20 
    21         return max

    实现2:

    对于每一个节点,取比他长的节点的位置,此时area = 距离差 * 这个节点的高度,实践复杂度 O(N^2),超时

    代码:

     1     def maxArea(self, height):
     2         tmp = len(height)
     3         result = 0
     4 
     5         for i in range(tmp):
     6             for j in range(tmp):
     7                 if height[j] >= height[i]:
     8                     area = height[i] * abs(i - j)
     9                     if area > result: result = area
    10 
    11         return result

    优化-1 依然超时

     1     def maxArea(self, height):
     2         tmp = len(height)
     3         result = 0
     4 
     5         record = []
     6         for i in range(tmp):
     7             for j in range(tmp):
     8                 if height[j] >= height[i]:
     9                     record.append(abs(j - i))
    10             area = height[i] * max(record)
    11             if area > result: result = area
    12             record = []
    13 
    14         return result

    优化-2 超时

     1     def maxArea(self, height):
     2         tmp = len(height)
     3         result = 0
     4 
     5         for i in range(tmp):
     6             t = None
     7             for j in range(i):
     8                 if height[j] > height[i]:
     9                     t = abs(j - i)
    10                     break
    11 
    12             if t is not None:
    13                 area_1 = t * height[i]
    14                 if area_1 > result: result = area_1
    15 
    16             t = None
    17             for j in range(i, tmp)[::-1]:
    18                 if height[j] > height[i]:
    19                     t = abs(j - i)
    20                     break
    21 
    22             if t is not None:
    23                 area_2 = t * height[i]
    24                 if area_2 > result: result = area_2
    25 
    26         return result

    实现3

    从左端点和右端点开始,贪婪,不断取小的值推进,符合直觉,如何证明可以使用贪婪

    当左边是i右边是j时,ij之间不会有更大的,两边?

    todo

  • 相关阅读:
    September 29th 2017 Week 39th Friday
    September 28th 2017 Week 39th Thursday
    September 27th 2017 Week 39th Wednesday
    September 26th 2017 Week 39th Tuesday
    September 25th 2017 Week 39th Monday
    September 24th 2017 Week 39th Sunday
    angular2 学习笔记 ( Form 表单 )
    angular2 学习笔记 ( Component 组件)
    angular2 学习笔记 ( Http 请求)
    angular2 学习笔记 ( Router 路由 )
  • 原文地址:https://www.cnblogs.com/mess4u/p/3927156.html
Copyright © 2011-2022 走看看