当前位置:网站首页>Question 1: the container that holds the most water
Question 1: the container that holds the most water
2022-06-24 08:10:00 【Larks have been barking all day】
1. subject : Container for the most water
Here you are. n Nonnegative integers a1,a2,...,an, Each number represents a point in the coordinate (i, ai) . Draw in coordinates n Bar vertical line , vertical Line i The two endpoints of are (i, ai) and (i, 0) . Find two of them , Make them x A container composed of shafts can hold the most More water . explain : You can't tilt the container .
2. answer : Violence law
Fixed starting point , Traverse every possible destination , Calculate the maximum area .
Cyclomatic complexity :n^2
class Solution:
def maxArea(self, height: List[int]) -> int:
maxArea = 0
for i, x in enumerate(height):
k = 0
area = 0
for y in height[i + 1::]:
k += 1
area = max(area, k * min(x, y))
maxArea = max(maxArea, area)
return maxAreaExecution timeout , Through use cases :49/60
3. Optimize : Double finger needling
Find the maximum width first , That is, the two pointers point to the beginning and end of the container respectively ;
Then find the pointer to move to the middle , At this time, the width will decrease no matter moving the left or right 1, You must keep the higher points to get a larger area ( Move the lower point out ), Update the maximum area with the current width and height
class Solution:
def maxArea(self, height: List[int]) -> int:
left = 1
right = len(height)
maxArea = 0
while right > left:
if height[left - 1] < height[right - 1]:
area = (right - left) * height[left - 1]
left += 1
else:
area = (right - left) * height[right - 1]
right -= 1
maxArea = max(maxArea, area)
return maxArea边栏推荐
- Decltype usage introduction
- 1-4metaploitable2 introduction
- "Adobe international certification" about Adobe Photoshop, creating and modifying brush tutorials?
- Leetcode 515 find the leetcode path of the maximum [bfs binary tree] heroding in each row
- Live wire, neutral wire and ground wire. Do you know the function of these three wires?
- Swift 基础 闭包/Block的使用(源码)
- Specify IP when calling feign interface
- Introduction to software engineering - Chapter 2 - feasibility study
- Coordinate transformation of graphic technology
- 热赛道上的冷思考:乘数效应才是东数西算的根本要求
猜你喜欢

Live wire, neutral wire and ground wire. Do you know the function of these three wires?

Selenium IDE的安装以及使用

Part 1: building OpenGL environment

Swift Extension NetworkUtil(网络监听)(源码)

Oracle advanced SQL qualified query

Synchronous FIFO

Application of JDBC in performance test

AWTK 最新动态:Grid 控件新用法

Model effect optimization, try a variety of cross validation methods (system operation)

OC extension detects whether an app is installed on the mobile phone (source code)
随机推荐
MySQL source and target table row count check
Unity culling related technologies
How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect
Tuple remarks
Easyplayerpro win configuration full screen mode can not be full screen why
[data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board
Atguigu---16-custom instruction
Swift 基础 闭包/Block的使用(源码)
Methods of vector operation and coordinate transformation
Leetcode 174 Dungeon games (June 23, 2022)
Leetcode 515 find the leetcode path of the maximum [bfs binary tree] heroding in each row
GraphMAE----論文快速閱讀
Upgrade Mysql to the latest version (mysql8.0.25)
Thread considerations
SCM stm32f103rb, BLDC DC motor controller design, schematic diagram, source code and circuit scheme
Case examples of corpus data processing (cases related to sentence retrieval)
Coordinate transformation of graphic technology
[run the script framework in Django and store the data in the database]
More than observation | Alibaba cloud observable suite officially released
[teacher zhaoyuqiang] use the Oracle tracking file