myCode


#include "misc.h"
#include "data.h"
#include <map>
#include <unordered_map>
#include <string>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <climits>

using namespace std;

class Order {
private:
    int _pid;
    int _quantity;
    int _expected_days;
    Region _region;
    double _date;
    
public:
    Order (int pid_, int quantity_, int expected_days_, Region region_, double date_) :
    _pid (pid_),
    _quantity (quantity_),
    _expected_days (expected_days_),
    _region (region_),
    _date (date_) {};
    
    Order (const Order &other_) {
        _pid = other_._pid;
        _quantity = other_._quantity;
        _expected_days = other_._expected_days;
        _region = other_._region;
        _date = other_._date;
    }
    
    const int getPid () const { return _pid; }
    const int getQuantity () const { return _quantity; }
    const Region getDestination () const { return _region; }
};

class ProductInventory {
private:
    int _pid;
    int _quantity;
    Region _region;
    
public:
    ProductInventory (int pid_, int quantity_, Region region_) :
    _pid (pid_),
    _quantity (quantity_),
    _region (region_) {};
    
    ProductInventory (const ProductInventory &other_) {
        _pid = other_._pid;
        _quantity = other_._quantity;
        _region = other_._region;
    }
    
    const int getPid () const { return _pid; }
    const int getQuantity () const { return _quantity; }
    void reduceQuantity (int val) { _quantity -= val; }
    const Region getRegion () const { return _region; }
};

class ShippingCost {
private:
    Region _ship_from;
    Region _ship_to;
    Method _method;
    int _cost_per_item;
    int _days;
    
public:
    ShippingCost (Region ship_from_, Region ship_to_, Method method_, int cost_per_item_, int days_) :
    _ship_from (ship_from_),
    _ship_to (ship_to_),
    _method (method_),
    _cost_per_item (cost_per_item_),
    _days (days_) {};
    
    ShippingCost (const ShippingCost &_other) {
        _ship_from = _other._ship_from;
        _ship_to = _other._ship_to;
        _method = _other._method;
        _cost_per_item = _other._cost_per_item;
        _days = _other._days;
    }
    
    const Region getShipFrom () const { return _ship_from; }
    const Region getShipTo () const { return _ship_to; }
    const int getDays () const { return _days; }
    const int getCost () const { return _cost_per_item; }
};


//data----------------------------------------------------------------
const static vector<Order> orders {
    Order (1, 6, 4, center, 0.3),
    Order (1, 3, 2, west, 0.0),
    Order (1, 4, 0, east, 0.2),
    Order (3, 100, 0, center, 0.1),
    Order (2, 6, 4, center, 0.3)
};

static vector<ProductInventory> productInventoryList {
    ProductInventory (1, 7, north),
    ProductInventory (3, 70, north),
    ProductInventory (3, 20, north),
    ProductInventory (3, 40, east),
    ProductInventory (3, 30, north),
};

const static vector<ShippingCost> shippingCostList {
    ShippingCost (north, west, express, 3, 10),
    ShippingCost (north, west, ground, 1, 15),
    ShippingCost (north, east, ground, 2, 20),
    ShippingCost (north, center, express, 2, 5)
};

//---------------------------------------------------------------

class ProductShippingCost{
public:
    ProductInventory productInventory;
    vector<ShippingCost> shippingCostList;
    
    ProductShippingCost(ProductInventory productInventory, vector<ShippingCost> shippingCostList): productInventory(productInventory), shippingCostList(shippingCostList){}
};


//milestone 1
vector<ProductShippingCost> Solution1(int pid, Region toRegin) {
    
    vector<ProductShippingCost> result;
    
    for(ProductInventory& currentInventory : productInventoryList) {
        
        //if currentInventory contains the product we want
        if (pid == currentInventory.getPid()) {
            
            ProductShippingCost temp(currentInventory,{});
            
            for (ShippingCost currentShippingCost : shippingCostList) {
                if (currentShippingCost.getShipFrom() == currentInventory.getRegion() &&
                    currentShippingCost.getShipTo() == toRegin) {
                    
                    temp.shippingCostList.push_back(currentShippingCost);
                }
            }//for shippingCostList
            
            //if there exist a way that the product can be shipped to the destination
            if(!temp.shippingCostList.empty()) result.push_back(temp);
        }
    }//for productInventoryList
    
    return result;
    
}



struct OrderQuantityComparator {
    bool operator () (const Order &order1, const Order &order2) {
        return order1.getQuantity() < order2.getQuantity();
    }
} orderQuantityComparator;

struct ShippingCostTimeComparator {
    bool operator () (ShippingCost shippingCost1, ShippingCost shippingCost2) {
        return shippingCost1.getDays() < shippingCost2.getDays();
    }
}shippingCostTimeComparator;


//void Solution2() {
//    vector<Order> orderList = orders;
//    sort(orderList.begin(),orderList.end(),orderQuantityComparator);
//    
//    for(Order& order : orderList) {
//        
//        vector<ProductShippingCost> orderShippingCostList = Solution1(order.getPid(), order.getDestination());
//        //orderShippingCostList -> { productInventory, vector<shippingCost> }
//        
//        unordered_map<ShippingCost,ProductInventory> productInventoryToMinTime;
//        
//        
//        
//        int inventoryQuantitySum = 0;
//        
//        //for one specific inventory
//        for(ProductShippingCost& orderShippingCost : orderShippingCostList) {
//            inventoryQuantitySum += orderShippingCost.productInventory.getQuantity();
//            
//            int minShppingTime = 99999;
//            ShippingCost minTimeShipping = orderShippingCost.shippingCostList[0];
//
//            //get current inventory's shippingCost with least shipping time
//            for (ShippingCost& shippingCost : orderShippingCost.shippingCostList) {
//                if (shippingCost.getDays() < minShppingTime) {
//                    minShppingTime = shippingCost.getDays();
//                    minTimeShipping = shippingCost;
//                }
//            }
//            //productInventoryToMinTime[minTimeShipping] = orderShippingCost.productInventory;
//            
//            //Now we have productInventoryToMinTime<ProductInventory,ShippingCost> ordered by shippingTime
//        }
//        
////        if(inventoryQuantitySum < order.getQuantity()) continue;
////        
////        int unfinishedQuantity = order.getQuantity();
////        while(unfinishedQuantity > 0)
////        {
////            ProductInventory currentInventory = productInventoryToMinTime.begin()->first;
////            currentInventory.reduceQuantity(min(currentInventory.getQuantity(), unfinishedQuantity));
////            unfinishedQuantity -= min(currentInventory.getQuantity(), unfinishedQuantity);
////            cout << "from Inventory " << currentInventory.getRegion() << " ship " << min(currentInventory.getQuantity(), unfinishedQuantity) << endl;
////            if(currentInventory.getQuantity() == 0) productInventoryToMinTime.erase(currentInventory);
////        }
//    
//    }
//    
//
//}
//

typedef pair<ProductInventory *, int> InventoryMinTimePair;

struct InventoryMinTimePairComparator {
    bool operator()(const InventoryMinTimePair & InventoryMinTimePair1,const InventoryMinTimePair & InventoryMinTimePair2) {
        return InventoryMinTimePair1.second < InventoryMinTimePair2.second;
    
    }

};

void Solution22(vector<Order> orderList) {
    sort(orderList.begin(),orderList.end(),orderQuantityComparator);
    for(Order& order : orderList) {
        cout << "Order " << order.getPid() << endl;
        

//Order shipping plan selection part:
        vector<ProductShippingCost> productShippingCostList = Solution1(order.getPid(), order.getDestination());
        priority_queue<InventoryMinTimePair,vector<InventoryMinTimePair>, InventoryMinTimePairComparator> productInventoryPQ;
        
        //iterator in this loop has form { productInventory, vector<shippingCost> }
        for(ProductShippingCost & productShippingCost : productShippingCostList) {
        
            //there is no way the product can be shipped
            if(productShippingCost.shippingCostList.empty()) continue;
            
            int inventoryQuantitySum = 0;
            
            //GOAL: select the least time shippingCost from vector<shippingCost>
            //      and save it to a pq of pair <procuetInventory, shippingCostMinTime>
            int minTime = productShippingCost.shippingCostList[0].getDays();
            InventoryMinTimePair productInventoryWithMinTimeShipping;
            
            //O(N), N = size of shippingCostList, Linear search
            for(ShippingCost &shippingCost : productShippingCost.shippingCostList) {
                minTime = min(minTime,shippingCost.getDays());
            } //for shippingCost
            
            productInventoryWithMinTimeShipping.first = &productShippingCost.productInventory;
            productInventoryWithMinTimeShipping.second = minTime;
            
            //O(logN), N = #inventories contains current product, PQ push in C++
            //Now we get a pq contains all the inventorys sorted by the shipping time
            productInventoryPQ.push(productInventoryWithMinTimeShipping);
            
            inventoryQuantitySum += productShippingCost.productInventory.getQuantity();
            
            //Order can NOT be fulfilled
            if(inventoryQuantitySum < order.getQuantity()) continue;
            
            //Inventory shipping part:
            int unShippedCount = order.getQuantity();
            while(unShippedCount > 0 ) {
                //inventory can at most ship all its products
                int shipQuaitity = min(productInventoryPQ.top().first->getQuantity(),unShippedCount);
                
                productInventoryPQ.top().first->reduceQuantity(shipQuaitity);//O(1)
                
                cout << "Ship " << shipQuaitity << " from inventory " << productInventoryPQ.top().first->getRegion() << endl;
                
                productInventoryPQ.pop();//O(logN), N = size of PQ
                
                unShippedCount -= shipQuaitity;
            
            }
            
            
        } //for productShippingCost
        
        
    }

}

struct InventoryMinCostPairComparator {
    bool operator()(const InventoryMinTimePair & InventoryMinCostPair1,const InventoryMinTimePair & InventoryMinCostPair2) {
        return InventoryMinCostPair1.second < InventoryMinCostPair2.second;
        
    }
    
};

void Solution3(vector<Order> orderList) {
    sort(orderList.begin(),orderList.end(),orderQuantityComparator);
    for(Order& order : orderList) {
        cout << "Order " << order.getPid() << endl;
        
        
        //Order shipping plan selection part:
        vector<ProductShippingCost> productShippingCostList = Solution1(order.getPid(), order.getDestination());
        priority_queue<InventoryMinTimePair,vector<InventoryMinTimePair>, InventoryMinTimePairComparator> productInventoryPQ;
        
        //iterator in this loop has form { productInventory, vector<shippingCost> }
        for(ProductShippingCost & productShippingCost : productShippingCostList) {
            
            //there is no way the product can be shipped
            if(productShippingCost.shippingCostList.empty()) continue;
            
            int inventoryQuantitySum = 0;
            
            //GOAL: select the least time shippingCost from vector<shippingCost>
            //      and save it to a pq of pair <procuetInventory, shippingCostMinTime>
            int minCost = productShippingCost.shippingCostList[0].getCost();
            InventoryMinTimePair productInventoryWithMinTimeShipping;
            
            //O(N), N = size of shippingCostList, Linear search
            for(ShippingCost &shippingCost : productShippingCost.shippingCostList) {
                minCost = min(minCost,shippingCost.getDays());
            } //for shippingCost
            
            productInventoryWithMinTimeShipping.first = &productShippingCost.productInventory;
            productInventoryWithMinTimeShipping.second = minCost;
            
            //O(logN), N = #inventories contains current product, PQ push in C++
            //Now we get a pq contains all the inventorys sorted by the shipping time
            productInventoryPQ.push(productInventoryWithMinTimeShipping);
            
            inventoryQuantitySum += productShippingCost.productInventory.getQuantity();
            
            //Order can NOT be fulfilled
            if(inventoryQuantitySum < order.getQuantity()) continue;
            
            //Inventory shipping part:
            int unShippedCount = order.getQuantity();
            while(unShippedCount > 0 ) {
                //inventory can at most ship all its products
                int shipQuaitity = min(productInventoryPQ.top().first->getQuantity(),unShippedCount);
                
                productInventoryPQ.top().first->reduceQuantity(shipQuaitity);//O(1)
                
                cout << "Ship " << shipQuaitity << " from inventory " << productInventoryPQ.top().first->getRegion() << endl;
                
                productInventoryPQ.pop();//O(logN), N = size of PQ
                
                unShippedCount -= shipQuaitity;
                
            }
            
            
        } //for productShippingCost
        
        
    }
    
}


struct testComp {
    bool operator()(const string &a ,const string &b) const {
        return a < b;
    }
    
}comp;

int main(int argc, const char * argv[]) {
    vector<ProductShippingCost> result = Solution1(3, west);
    
    //cout << result.size() << endl;
    
    //Solution2(orderList);
    
//    map<string,int,testComp> test;
//    test["hjq"] = 3;
//    test["12"] = 4;
//    test["kkk"] = 2;
//    test["asdf"] = 1;
//    test["34234"] = 5;
//    test["asdfg"] = 9;
    
//    for(auto it : test) {
//        cout << it.second << endl;
//    }
    
    Solution22(orders);
    
    return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末油湖,一起剝皮案震驚了整個(gè)濱河市涝滴,隨后出現(xiàn)的幾起案子朝蜘,更是在濱河造成了極大的恐慌搬瑰,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件踏幻,死亡現(xiàn)場離奇詭異枷颊,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)该面,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進(jìn)店門夭苗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人隔缀,你說我怎么就攤上這事题造。” “怎么了猾瘸?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵界赔,是天一觀的道長。 經(jīng)常有香客問我牵触,道長淮悼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任揽思,我火速辦了婚禮袜腥,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘钉汗。我一直安慰自己羹令,他們只是感情好锡宋,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著特恬,像睡著了一般执俩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上癌刽,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天役首,我揣著相機(jī)與錄音,去河邊找鬼显拜。 笑死衡奥,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的远荠。 我是一名探鬼主播矮固,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼譬淳!你這毒婦竟也來了档址?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤邻梆,失蹤者是張志新(化名)和其女友劉穎守伸,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體浦妄,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡尼摹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了剂娄。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蠢涝。...
    茶點(diǎn)故事閱讀 39,727評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖阅懦,靈堂內(nèi)的尸體忽然破棺而出和二,到底是詐尸還是另有隱情,我是刑警寧澤故黑,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布儿咱,位于F島的核電站,受9級特大地震影響场晶,放射性物質(zhì)發(fā)生泄漏混埠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一诗轻、第九天 我趴在偏房一處隱蔽的房頂上張望钳宪。 院中可真熱鬧,春花似錦、人聲如沸吏颖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽半醉。三九已至疚俱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間缩多,已是汗流浹背呆奕。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留衬吆,地道東北人梁钾。 一個(gè)月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像逊抡,于是被迫代替她去往敵國和親姆泻。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 你是不是經(jīng)歷過這些痛苦冒嫡? 大齡單身未嫁周邊還沒有雄性生物出現(xiàn)拇勃; 父母日夜逼婚讓你極度懷疑自己是不是撿來的; 各方面...
    明初的日記本閱讀 632評論 0 0
  • “累成狗樣”蛔琅,好像已經(jīng)成為許多人的 口頭禪胎许,如果今天多走了幾步,發(fā)個(gè)自拍加句“今天累成狗樣”罗售;如果今天多跑...
    混混蛋閱讀 620評論 0 2
  • NO.1 深夜買煙的故事 朋友來訪辜窑,和我講了他的故事,我感覺挺有意思寨躁。不知道這樣的故事穆碎,是否也曾經(jīng)發(fā)生在你身上?...
    陽光Sunflower閱讀 1,424評論 0 5