【LintCode 简单】749. John's backyard garden

砖墙高度组合问题
本文探讨了一个关于使用两种不同高度的砖块(3dm和7dm)来搭建任意高度(x dm)墙壁的问题,并提供了一个递归解决方案。文章通过具体实例说明了如何判断是否能用这两种砖块组合出目标高度。

1.问题描述:

John wants to build a back garden on the empty space behind his home. There are two kinds of bricks now, one is 3 dm high and the other is 7 dm high. John wants to enclose a high x dm wall. If John can do this, output YES, otherwise NO.


2.样例:

Give x = 10,return YES.

Explanation:
x = 3 + 7:That is, you need one batch of 3 dm height bricks and one batch of 7 dm height bricks.

Give x = 5,return NO.

Explanation:
John can not enclose a high 5 dm wall with 3 dm height bricks and 7 dm height bricks.

Give x = 13,return YES.

Explanation:
x = 2 * 3 + 7:That is, you need two batch of 3 dm height bricks and one batch of 7 dm height bricks.


3.代码:

class Solution:
    """
    @param x: the wall's height
    @return: YES or NO
    """
    def isBuild(self, x):
        # Write your code here
        if x<0:
            return "NO"
        if x%3 ==0 or x %7 ==0 :
            return "YES"
        else:
            a=x-3
            b=x-7
            return self.isBuild(a) and self.isBuild(b)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值