#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

#LeetCode# Container With Most Water (todo)

时间: 2024-08-27 02:23:14

#LeetCode# Container With Most Water (todo)的相关文章

Container With Most Water(LintCode)

Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together wi

【leetcode】Container With Most Water(middle)

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

LeetCode: Container With Most Water 题解

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

LeetCode "Container With Most Water" - GREEDY?

O(n^2) is a naive solution. As rule of thumb, there must be O(n) soluion. Yes - Greedy. We assume widest container contains the most water, greedily, but it is possible that we have higher heights with narrower range, so we proceed. Of course we disg

C#刷遍Leetcode面试题系列连载(2): No.38 - 报数

目录 前言 题目描述 相关话题 相似题目 解题思路: 运行结果: 代码要点: 参考资料: 文末彩蛋 前言 前文传送门: C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介 上篇文章中我们主要科普了刷 LeetCode 对大家的作用,今天咱们就正式进行 LeetCode 算法题分析.很多人都知道计算机中有种思想叫 递归,相应地也出现了很多算法.解决递归问题的要点有如下几个: 找出递归的关系 比如,给个数列 f(n),常见的递归关系是后面的项 f(n+1)与前面几项之间的关系,比

C#刷遍Leetcode面试题系列连载(5):No.593 - 有效的正方形

上一篇 LeetCode 面试题中,我们分析了一道难度为 Easy 的数学题 - 自除数,提供了两种方法.今天我们来分析一道难度为 Medium 的面试题. 系列教程索引 传送门:https://enjoy233.cnblogs.com/articles/leetcode_csharp_index.html C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - 报数 C# 刷遍 Leetcode 面试题系列连载(3):

leetcode_11题——Container With Most Water(两个指针)

Container With Most Water Total Accepted: 38917 Total Submissions: 121822My Submissions Question Solution Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two

LeetCode: Container With Most Water 解题报告

Container With Most WaterGiven n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together wit

在Azure Container Service创建Kubernetes(k8s)群集运行ASP.NET Core跨平台应用程序

引子 在此前的一篇文章中,我介绍了如何在本地docker环境中运行ASP.NET Core跨平台应用程序(http://www.cnblogs.com/chenxizhang/p/7148657.html),看起来非常不错,不是吗?那么,如果我们希望真正在实际的生产环境去部署和运行这个应用程序,应该怎么做呢? 通常来说,有两种方案可以选择 1. 在目标运行环境(可以是本地的服务器,也可以是云端)申请虚拟机,然后启用docker运行这些应用程序,所有的细节都可以(也必须)由你自己控制. 2. 使用