Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write code to achieve the container that holds the most water

Shulou Source: shulou.com Published: 2022-06-03 06:23:34 10月03日 Update

This article mainly explains "how to write code to achieve the container that holds the most water". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn how to write code to achieve the container that holds the most water.

Description of the container with the most water in the title

Difficulty: medium

I will give you n non-negative integers, a _ 1, a _ 2, and a _ 1, where each number represents a point in coordinates (I, ai). Draw n vertical lines in the coordinates. The two endpoints of the vertical line I are (I, ai) and (I, 0), respectively. Find two of these lines so that the container composed of them and the x-axis can hold the most water.

Description: you cannot tilt the container

Input:] output: 49 explanation: the vertical line in the picture represents the input array [1, 8, 6, 2, 5, 4, 4, 3, 7]. In this case, the maximum value that the container can hold water (represented as the blue part) is 49

Example 2:

Input: height = [1jue 1] output: 1

Example 3:

Input: height = [4, 3, 2, 2, 1, 4] output: 16

Example 4:

Input: height = [1, 2, 1] output: 2

Prompt

N = height.length2 0) {y = y-1;} else {/ / the height corresponding to x

Tags: Pointer minimum move maximum area container height vertical line two water complexity coordinates complexity maximum input code output formulas ideas examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi Apple Shulou Tech Info OPPO Reno