小C点菜问题 | 豆包MarsCode AI刷题

0 阅读1分钟
def solution(m: int, w: list) -> int:
    # 初始化价格计数字典
    price_count = {}
    
    # 遍历每道菜的价格
    for price in w:
        if price <= m:
            # 如果价格在允许范围内,增加计数
            if price in price_count:
                price_count[price] += 1
            else:
                price_count[price] = 1
    
    # 找出数量最多的那种价格的菜的数量
    max_count = 0
    for count in price_count.values():
        if count > max_count:
            max_count = count
    
    return max_count

if __name__ == '__main__':
    print(solution(6, [2, 3, 3, 6, 6, 6, 9, 9, 23]) == 3)
    print(solution(4, [1, 2, 4, 4, 4]) == 3)
    print(solution(5, [5, 5, 5, 5, 6, 7, 8]) == 4)