当前位置:网站首页>leetcode.11 --- 盛最多水的容器
leetcode.11 --- 盛最多水的容器
2022-06-22 18:45:00 【_End丶断弦】
盛最多水的容器

暴力解法:
2层for循环,时间复杂度n方,本题会直接超时
代码如下:
class Solution {
public:
int maxArea(vector<int>& height) {
int res = 0;
for(int i=0;i<height.size();i++)
{
for(int j=i+1;j<height.size();j++)
{
res = max(res,min(height[j],height[i])*(j-i));
}
}
return res;
}
};
解法二:
双指针解法,直接看题解,每次短的向前一步
代码如下:
class Solution {
public:
int maxArea(vector<int>& height) {
int i = 0,j = height.size() - 1,res = 0;
while(i < j)
{
res = max(res,min(height[i],height[j]) * (j - i));
if(height[i] < height[j]) i++;
else j--;
}
return res;
}
};
时间复杂度:O(n)
为什么双指针可以解决,可以看题解给出的证明即可。
边栏推荐
- IDEA写jsp代码报错,但是正常运行解决
- 【深入理解TcaplusDB技术】TcaplusDB 表管理——重建表
- [in depth understanding of tcapulusdb technology] tcapulusdb adds a new business cluster cluster
- [deeply understand tcapulusdb technology] how to initialize and launch tcapulusdb machine
- 树、森林及二叉树的相互转换
- 归并排序(递归和迭代实现)
- [deeply understand tcapulusdb technology] how to start tcapulusdb process
- [petty bourgeoisie database] break down the concept: data, database, database system, database management system, database technology
- 【深入理解TcaplusDB技术】入门Tcaplus-JDBC开发
- 北京大学|通过对比学习实现离线元强化学习的鲁棒任务表示
猜你喜欢

iVX无代码挑战五秒游戏制作
![[in depth understanding of tcaplus DB technology] Introduction to tcaplus JDBC development](/img/2b/3ab5e247ac103728b4d3579c3c5468.png)
[in depth understanding of tcaplus DB technology] Introduction to tcaplus JDBC development

元宇宙怎么就这么火,市场喊起来的10万亿是吹嘘还是真相?

【深入理解TcaplusDB知识库】部署TcaplusDB Local版常见问题

【深入理解TcaplusDB技术】查看TcaplusDB线上运行情况

Nlp-d57-nlp competition D26 & skimming questions D13 & reading papers & finding bugs for more than an hour

【深入理解TcaplusDB技术】入门Tcaplus SQL Driver
![[in depth understanding of tcapulusdb technology] getting started with MySQL driver](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[in depth understanding of tcapulusdb technology] getting started with MySQL driver

商业智能BI数据仓库中的指标、维度和模型到底是什么?
![Summer Challenge [FFH] Hongmeng machine learning journey from scratch NLP emotion analysis](/img/e8/3b6de6445c9a78f33c8e91c2ff5517.png)
Summer Challenge [FFH] Hongmeng machine learning journey from scratch NLP emotion analysis
随机推荐
Merge sort (recursive and iterative Implementation)
[deeply understand tcapulusdb technology] tcapulusdb table management - create a new table
R语言基于h2o包构建二分类模型:使用h2o.glm构建正则化的逻辑回归模型、使用h2o.auc计算模型的AUC值
【深入理解TcaplusDB技术】集群管理操作
socket的connect函数用法
[in depth understanding of tcapulusdb technology] introduction tcapulusdb problem summary
【深入理解TcaplusDB技术】TcaplusDB进程
IVX no code challenge five second game production
自己写了一个telnet命令
[in depth understanding of tcapulusdb technology] getting started with MySQL driver
散列表(哈希表)
Async-profiler介绍
B tree code (C language)
[in depth understanding of tcapulusdb technology] tcapulusdb model
【深入理解TcaplusDB技术】TcaplusDB机型
Huffman tree (C language)
【深入理解TcaplusDB技术】TcaplusDB机器如何下架
【深入理解TcaplusDB技术】入门MySQL Driver
【深入理解TcaplusDB技术】TcaplusDB 新增业务集群cluster
一个支持IPFS的电子邮件——SKIFF
