当前位置: 面试刷题>> 最大订单 (经典算法题500道)


完整题目描述

题目名称: 最大订单金额问题

题目描述: 给定一个订单列表,每个订单包含订单ID、顾客ID、订单金额等信息。现在需要编写一个程序,找出哪个顾客的订单总金额最大,并返回该顾客的ID以及他们的订单总金额。

输入

  • 一个包含多个订单的数组,每个订单是一个包含orderId(订单ID,字符串类型)、customerId(顾客ID,字符串类型)、amount(订单金额,数字类型)等字段的对象。

输出

  • 一个包含顾客ID和该顾客订单总金额的对象,如果有多个顾客的总金额相同且最大,则返回其中任意一个的结果即可。

示例输入

[
    {"orderId": "1", "customerId": "A", "amount": 100},
    {"orderId": "2", "customerId": "B", "amount": 150},
    {"orderId": "3", "customerId": "A", "amount": 200},
    {"orderId": "4", "customerId": "C", "amount": 120}
]

示例输出

{"customerId": "A", "totalAmount": 300}

PHP 示例代码

<?php

function findMaxOrderCustomer($orders) {
    $customerSums = [];
    $maxAmount = 0;
    $maxCustomerId = '';

    foreach ($orders as $order) {
        if (!isset($customerSums[$order['customerId']])) {
            $customerSums[$order['customerId']] = 0;
        }
        $customerSums[$order['customerId']] += $order['amount'];

        if ($customerSums[$order['customerId']] > $maxAmount) {
            $maxAmount = $customerSums[$order['customerId']];
            $maxCustomerId = $order['customerId'];
        }
    }

    return ["customerId" => $maxCustomerId, "totalAmount" => $maxAmount];
}

// 示例输入
$orders = [
    ["orderId" => "1", "customerId" => "A", "amount" => 100],
    ["orderId" => "2", "customerId" => "B", "amount" => 150],
    ["orderId" => "3", "customerId" => "A", "amount" => 200],
    ["orderId" => "4", "customerId" => "C", "amount" => 120]
];

// 调用函数并打印结果
$result = findMaxOrderCustomer($orders);
echo json_encode($result);

?>

Python 示例代码

def find_max_order_customer(orders):
    customer_sums = {}
    max_amount = 0
    max_customer_id = ''

    for order in orders:
        customer_id = order['customerId']
        if customer_id not in customer_sums:
            customer_sums[customer_id] = 0
        customer_sums[customer_id] += order['amount']

        if customer_sums[customer_id] > max_amount:
            max_amount = customer_sums[customer_id]
            max_customer_id = customer_id

    return {"customerId": max_customer_id, "totalAmount": max_amount}

# 示例输入
orders = [
    {"orderId": "1", "customerId": "A", "amount": 100},
    {"orderId": "2", "customerId": "B", "amount": 150},
    {"orderId": "3", "customerId": "A", "amount": 200},
    {"orderId": "4", "customerId": "C", "amount": 120}
]

# 调用函数并打印结果
result = find_max_order_customer(orders)
print(result)

JavaScript 示例代码

function findMaxOrderCustomer(orders) {
    let customerSums = {};
    let maxAmount = 0;
    let maxCustomerId = '';

    orders.forEach(order => {
        const customerId = order.customerId;
        if (!customerSums[customerId]) {
            customerSums[customerId] = 0;
        }
        customerSums[customerId] += order.amount;

        if (customerSums[customerId] > maxAmount) {
            maxAmount = customerSums[customerId];
            maxCustomerId = customerId;
        }
    });

    return { customerId: maxCustomerId, totalAmount: maxAmount };
}

// 示例输入
const orders = [
    { orderId: "1", customerId: "A", amount: 100 },
    { orderId: "2", customerId: "B", amount: 150 },
    { orderId: "3", customerId: "A", amount: 200 },
    { orderId: "4", customerId: "C", amount: 120 }
];

// 调用函数并打印结果
const result = findMaxOrderCustomer(orders);
console.log(JSON.stringify(result));

码小课网站中有更多相关内容分享给大家学习,包括算法设计、数据结构、编程语言进阶等多方面的知识和实战案例。

推荐面试题