# Leetcode # container with most water (Todo)

Source: Internet
Author: User

Description:

Implementation 1 -- evaluate all possible values, O (N ^ 2), timeout (because the timeout did not run all test cases, so there is no other problem)

Code:

 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: break14 15                 area = abs(sum(minus_lst[i:i+dis]) * dis)16                 if area > max: max = area 17 18             dis += 119             if dis > l: break20 21         return max

Implementation 2:

For each node, take the location of the node longer than him. At this time, Area = Distance Difference * Height of the node, practice complexity O (N ^ 2), timeout

Code:

 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 = area10 11         return result

Optimization-1 still times out

 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 = area12             record = []13 14         return result

Optimization-2 timeout

 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                     break11 12             if t is not None:13                 area_1 = t * height[i]14                 if area_1 > result: result = area_115 16             t = None17             for j in range(i, tmp)[::-1]:18                 if height[j] > height[i]:19                     t = abs(j - i)20                     break21 22             if t is not None:23                 area_2 = t * height[i]24                 if area_2 > result: result = area_225 26         return result

Implementation 3

Starting from the left and right endpoints, greedy, constantly pushing small values, intuition, how to prove that greedy can be used

When J is on the left and I is on the right, there will be no larger relationship between IJ. What are the two sides?

Todo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.