编程题 第八周第一节课作业

本文提供了一种解决LeetCode第11题“盛最多水的容器”问题的Python实现方法。该问题要求从一组垂直线中找出两线,使得这两条线与x轴构成的容器能盛最多的水。文中通过双指针技巧实现了高效的求解算法。

题目来自leetcode.com

11Container With Most Water

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 and n is at least 2.

class Solution():
    def __init__(self, _1 = 0, _2 = 0, area = 0):
        self._1 = _1
        self._2 = _2
        self.area = 0
    def update(self,_1,_2,area):
        self._1 = _1
        self._2 = _2
        self.area = area

def _area(_1,_2,an):
	_2 = _2 - _1
	if an[_1] >= an[_2]:
		return an[_2]*_2
	else:
		return an[_1]*_2

ans = Solution()

n = input("Input the number ")
n = int(n)
an = []
a = 0
while a<n:
	b = input()
	b = int(b)
	an.append(b)
	a += 1

a = 0
ma = 0
while a<n-1:
    b = a+1
    while b<n:
        c = _area(a,b,an)
        if ma < c:
            ma = c
            ans.update(a,b,c)
        b += 1
    a += 1
print("zui da zhi zuo biao :")
print('('+str(ans._1+1)+','+str(an[ans._1])+')'+'\n'+'('+str(ans._2+1)+','+str(an[ans._2])+')')
print("area :")
print(str(ans.area))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值