python 停车场车辆统计

‘’’
2024 Q2 100
停车场车辆统计
特定大小的停车场,数组cars[]表示,其中1表示有车,0表示没车。车辆大小不一,小车占一个车位(长度1),货车占两个车位(长度2),卡车占三个车位(长度3),统计停车场最少可以停多少辆车,返回具体的数目。
输入描述:
整型字符串数组cars[],其中1表示有车,0表示没车,数组长度小于1000。
输出描述:
整型数字字符串,表示最少停车数目。
补充说明:
示例1
输入:
1,0,1
输出:
2
说明:
1个小车占第1个车位
第二个车位空
1个小车占第3个车位
最少有两辆车
示例2
输入:
1,1,0,0,1,1,1,0,1
输出:
3
说明:
1个货车占第1、2个车位
第3、4个车位空
1个卡车占第5、6、7个车位
第8个车位空
1个小车占第9个车位
最少3辆车
‘’’

def tingchechangtongji(tongji:[]):
    status=1
    ans=0
    count=0
    for i in range(1,len(tongji)-1):
        if str(tongji[i-1])=='0' and str(tongji[i])=='1':
            status=1
        if str(tongji[i-1])=='1' and str(tongji[i])=='0':
            status=0
            ans+=count//3+count%3//2+count%3%2
            # print(i,count, ans)
            count = 0
        if status:
            count+=1
    if tongji[-1] :
        if status:
            count += 1
            ans += count // 3 + count % 3 // 2 + count % 3 % 2
        else:
            ans +=1
    return ans
print(tingchechangtongji([0,1,1,0,1,0,1,0,1]))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值