编辑代码

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});

readline.on('line', (input) => {
  // 将输入的字符串转换为停车场数组
  const inputArray = input.split(',');
  const inputString = inputArray.join('');
  const parking_slots = inputString.split('0');

  // 初始化停车场最少停车数目为0
  let min_cars = 0;

  // 遍历停车场数组,统计每个连续的1的长度
  for (const slot of parking_slots) {
    // 计算当前连续1的长度
    const occupied_length = slot.length;

    // 如果当前连续1的长度为0,不做任何操作
    if (occupied_length === 0) {
      min_cars = min_cars;
    }
    // 如果当前连续1的长度能被3整除,说明可以完全放置卡车
    else if (occupied_length % 3 === 0 && occupied_length !== 0) {
      // 将当前连续1的长度除以3,得到卡车数量,并累加到最少停车数目
      min_cars += Math.floor(occupied_length / 3);
    }
    // 如果当前连续1的长度不能被3整除,说明需要放置小车或货车
    else if (occupied_length % 3 !== 0) {
      // 计算可以放置的卡车数量,并累加到最少停车数目
      min_cars += Math.floor((occupied_length - occupied_length % 3) / 3);
      // 由于还有剩余的车位,需要放置一个小车或货车,所以最少停车数目加1
      min_cars += 1;
    }
  }

  // 输出停车场最少停车数目
  console.log(min_cars);
  readline.close();
});