Python最多颜色的车辆

B站影视 2025-01-16 21:30 3

摘要:def max_vehicle_color(colors, window_size):max_count = 0 # 记录最大颜色数量color_count = [0, 0, 0] # 记录当前窗口内各颜色的数量# 初始化第一个窗口的颜色统计for i in

def max_vehicle_color(colors, window_size):max_count = 0 # 记录最大颜色数量color_count = [0, 0, 0] # 记录当前窗口内各颜色的数量# 初始化第一个窗口的颜色统计for i in range(window_size):color_count[colors[i]] += 1max_count = max(color_count)# 滑动窗口for i in range(window_size, len(colors)):# 移除窗口最左边的颜色color_count[colors[i - window_size]] -= 1# 添加窗口最右边的颜色color_count[colors[i]] += 1# 更新最大颜色数量max_count = max(max_count, max(color_count))return max_count# 输入处理if __name__ == "__main__":# 读取车辆颜色序列colors = list(map(int, input.strip.split))# 读取时间窗口长度window_size = int(input.strip)# 调用函数并输出结果result = max_vehicle_color(colors, window_size)print(result)

来源:白白看汽车

相关推荐