首页 > 解决方案 > C++ 中的咖啡馆订单检查器

问题描述

给定所有三个向量,编写一个函数来检查我的服务是先到先得的。所有食物都应该按照顾客要求的顺序出来。

   bool isFirstComeFirstServed(const vector<int>& takeOutOrders,
                                    const vector<int>& dineInOrders,
                                    const vector<int>& servedOrders)


   {
        auto takeOutOrdersIter = takeOutOrders.cbegin();
        auto dineInOrdersIter = dineInOrders.cbegin();

        for (int order : servedOrders) {

            // if we still have orders in takeOutOrders
            // and the current order in takeOutOrders is the same
            // as the current order in servedOrders
            if (takeOutOrdersIter != takeOutOrders.cend() && order == *takeOutOrdersIter) {
                ++takeOutOrdersIter;
            }

            // if we still have orders in dineInOrders
            // and the current order in dineInOrders is the same
            // as the current order in servedOrders
            else if (dineInOrdersIter != dineInOrders.cend() && order == *dineInOrdersIter) {
                ++dineInOrdersIter;
            }

            // if the current order in servedOrders doesn't match the current
            // order in takeOutOrders or dineInOrders, then we're not serving first-come,
            // first-served
            else {
                return false;
            }
        }

        // check for any extra orders at the end of takeOutOrders or dineInOrders
        if (dineInOrdersIter != dineInOrders.cend() || takeOutOrdersIter != takeOutOrders.cend()) {
            return false;
        }

        // all orders in servedOrders have been "accounted for"
        // so we're serving first-come, first-served!
        return true;
    }

我的疑问是我必须检查此解决方案是否适用于潜在的重复元素或订单,我检查了各种重复情况并且解决方案是否有效。你怎么看?我想百分百确定。如果那里有任何缺陷,请指导我。

资料来源:https ://www.interviewcake.com/question/python/cafe-order-checker?course=fc1§ion=array-and-string-manipulation

标签: c++data-structures

解决方案


推荐阅读