Leetcode_11 most water containers [Sheng]

Article Directory:

  • topic
  • A script and notes
  • A script logic
  • Two scripts and notes
  • Scripts two logic

topic:

Given n non-negative integers a1, a2, ..., an, a point (i, ai) each represents the number of coordinates. Videos n lines in vertical coordinate, i is a vertical line two end points are (i, AI) and (i, 0). Find out the two lines, which together with the container so that the x-axis configuration can accommodate up water.

Note: You can not tilting the container, and the value of n is at least 2.

 

 

 

 

FIG vertical line represents the input array [1,8,6,2,5,4,8,3,7]. In this case the maximum container capable of holding water (indicated as blue) of 49.

 

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49


And a script Note: [timeout, but the script is to achieve this requirement]

# Coding: GBK 
List1 = [2,3,4,5,18,17,6 ] # define a list 
MAX1 = 0 # define a variable value of zero      
n1 = n1. 1 # defined fragment for 
long1 = len ( list1) # Get a list of the number of elements
 for I in Range (long1): # number of columns the number of list elements is circulated 
    List2 = List1 [N1:] # list of supported fragments, n1 with the for loop will increment 
    n2 # 1 = n2 defined variables to get through the list length between elements, as for loop will increment
     for J in List2: # fragmented list after traversing
         IF List1 [I]> = J: # Analyzing after traversing the original list of elements and the minimum traverse slice element 
            curlJ * = N2 # minimum value multiplied by the length of the two elements
         the else :                    
            curl = List1 [I] * N2 # supra
         IF MAX1 < curl: # variable determines the product is greater than the recorded maximum 
            MAX1 = curl # if it exceeds, then the product imparting maximum variable 
        N2 +. 1 = 
    N1 + =. 1
 Print (MAX1)

A script logic:

  • Into most water containers must be in the list two elements
  • List variable combinations of two elements, they can hold the required amount of water, and then compared with the maximum value, the target value can be achieved

 

And two scripts Note: [If] with 140ms

class Solution:
     DEF maxArea (Self, height: List [int]) -> int: # Title given input and output in the form of 
        I, J, MAX1, long1 = 0, len (height) - 1,0, len (height ) --1 # are assigned to a variable
         the while I < J: # when conditions into the circulation
             iF height [I] <= height [J]: # Analyzing list two elements in the value of the size 
                curl = height [I] * long1 # take a small value and multiplied by the width between the two elements of 
                I + = # If the minimum value is left. 1, i of plus. 1
             the else : 
                curl = height [j] * long1 list elements # If the minimum value is right, product demand 
                j- = 1 #j subtracted. 1
             IF curl> MAX1: comparing the maximum value is determined for # 
                MAX1 = curl 
            long1 - #. 1 = length minus a
         return end (max1) # cycle, recording the maximum return variable

Script two logic:

  • The most important logic of this script is under what circumstances can the elements of the list to weed out the [minimum value multiplied by the maximum length of which can be connected, minimum elements can be removed]
  • Compare the value of the synchronization element size at both ends, wherein the use of a small value is multiplied by the length of the variable and the product of the maximum value is compared with the recording, if the replacement is greater than the maximum
  • After each comparison is completed, the length will be subtracted 1
  • After the completion of each comparison, the comparison element will have a change, a move to the right or the left; left or right to move one end until the end of the cycle, when all the elements have been traversed

 

 

Guess you like

Origin www.cnblogs.com/mailong/p/12008176.html