博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Container With Most Water】cpp
阅读量:7031 次
发布时间:2019-06-28

本文共 997 字,大约阅读时间需要 3 分钟。

题目:

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container.

代码:

class Solution {public:    int maxArea(vector
& height) { if ( height.size()<2 ) return 0; int max_area = 0; int left = 0; int right = height.size()-1; while ( left

tips:

试图用DP去做,但是没想出来;最后无奈落入了Greedy的俗套solution。

这个greedy的思路也是蛮巧的:从两头开始往中间greedy,头尾两个greedy一起变化才得到greedy的条件。

=======================================

第二次过这道题,这道题题意不清晰:如果选了某两个板子,就当其他板子不存在。

class Solution {public:    int maxArea(vector
& height) { int l = 0; int r = height.size()-1; int ret = 0; while ( l

 

转载于:https://www.cnblogs.com/xbf9xbf/p/4540412.html

你可能感兴趣的文章
Redis 安装
查看>>
HDU 1495 非常可乐
查看>>
Sqlite3+EF6踩的坑
查看>>
【tyvj1860】后缀数组
查看>>
Java多线程(九)之ReentrantLock与Condition
查看>>
Spring源码剖析依赖注入实现
查看>>
使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
查看>>
第九次作业 160809309 朱庆海
查看>>
对百度WebUploader的二次封装,精简前端代码之图片预览上传(两句代码搞定上传)...
查看>>
【leetcode】18 4 sum
查看>>
fiddler工具的使用
查看>>
jquery源码分析(二)——架构设计
查看>>
bzoj1935 [SHOI2007]Tree 园丁的烦恼
查看>>
javascript深入理解js闭包(转)
查看>>
207. Course Schedule
查看>>
如何优化您的 Android 应用 (Go 版)
查看>>
crossplatform---Nodejs in Visual Studio Code 01.简单介绍Nodejs
查看>>
UEditor富文本编辑框学习
查看>>
Trie树实现
查看>>
Opencv无法调用cvCaptureFromCAM无法打开电脑自带摄像头
查看>>