停车场车辆统计 - 华为OD统一考试 (C卷)

OD统一考试 (C卷)

分值: 100分

题解: Java / Python / C++

alt

题目描述

特定大小的停车场,数组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辆车

题解

停车场的规则是:小车占一个车位(长度1),货车占两个车位(长度2),卡车占三个车位(长度3)。

我们可以通过遍历输入的车辆状态数组 cars[],同时记录当前连续的占用车位数 d

  • 当遇到有车(1)时 d + 1;
  • 当遇到空位时,计算可以停放多少辆车(贪心的选择: 卡车 》 货车 》 小车),并将 d 置零。
  • 最后,再次检查一下是否还有剩余的车位数未计算,进行补充计算。最终得到停车场最少可以停多少辆车。

复杂度分析

时间复杂度:O(n),其中 n 为输入数组 cars[] 的长度。

空间复杂度:O(1),只使用了常数个变量。

Java

import java.util.Scanner;

/**
 * @author code5bug
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] carsStr = scanner.nextLine().split(",");
        int cnt = 0, d = 0;
        for (String st : carsStr) {
            if (st.equals("1")) {
                d += 1;
                continue;
            }

            if (d > 0) cnt += (d - 1) / 3 + 1;
            d = 0;
        }

        if (d > 0) cnt += (d - 1) / 3 + 1;

        System.out.println(cnt);
    }
}

Python

cars = list(input().split(","))
cars.append("0")

cnt, d = 0, 0
for st in cars:
    if st == "1":
        d += 1
        continue

    if d > 0:
        cnt += (d - 1) // 3 + 1
    d = 0
print(cnt)

C++

#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main() {
    string input;
    getline(cin, input);
    stringstream ss(input);

    vector<string> carsStr;
    string car;
    while (getline(ss, car, ',')) {
        carsStr.push_back(car);
    }

    int cnt = 0, d = 0;
    for (const string& st : carsStr) {
        if (st == "1") {
            d += 1;
            continue;
        }

        if (d > 0) cnt += (d - 1) / 3 + 1;
        d = 0;
    }
    if (d > 0) cnt += (d - 1) / 3 + 1;

    cout << cnt << endl;

    return 0;
}

🙏整理题解不易, 如果有帮助到您,请给点个赞 ‍❤️‍ 和收藏 ⭐,让更多的人看到。🙏🙏🙏

#面经##校招##春招##秋招##华为#
全部评论
读半天没明白“停车场最少可以停多少辆车”是什么意思,应该是“统计停车场最少停了多少辆车吧” 统计所有连续为1的段,假设每段的1的长度为n,则该段最少停了(n-1)/3+1辆车,将所有连续为1的段加起来即可
4 回复 分享
发布于 2024-04-26 09:56 浙江
/* 特定大小的停车场,数组cars[]表示 */ const cart = [3, 2, 1]; /** * 寻找数组里边连续出现的1 并计数 * @param {number[]} cars * @param {number} number */ const countReduce = (cars, number) => { let has = 0; let count = 0; for (let i = 0; i < cars.length; i++) { // 记录连续的车位 if (cars[i]) { has++; } else { has = 0; } // 与number对等则说明当前车辆可以使用此车位 if (has === number) { count++; has = 0; // 将已经占用的置为0 for (let j = i; j > i - number; j--) { cars[j] = 0; } } } return count; }; /** * @param {number[]} cars */ const handler = (cars) => { return cart.reduce((t, i) => t + countReduce(cars, i), 0); }; console.log(handler([1, 1, 0, 0, 1, 1, 1, 0, 1]));
点赞 回复 分享
发布于 2025-03-15 00:55 河南
华为od,组内直招,对软硬开发有意向的,可以联系我,Ai算力底座方向,帮改简历。杭州、西安、上海、成都、东莞。c/c /python均可
点赞 回复 分享
发布于 2024-03-19 23:07 浙江

相关推荐

评论
4
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务