मान लीजिए हमारे पास orders नामक एक ऐरे है, जो रेस्टोरेंट में ग्राहकों द्वारा किए गए ऑर्डर्स को दर्शाता है। तो, orders[i]=[cust_namei, table_numi, food_itemi] जहां cust_namei ग्राहक का नाम है, table_numi ग्राहक का टेबल नंबर है, और food_itemi वह आइटम है जो ग्राहक ऑर्डर करता है।
हमें रेस्टोरेंट की "डिस्प्ले टेबल" लौटानी है। यहाँ "डिस्प्ले टेबल" एक टेबल है जिसकी रो एंट्री दर्शाती है कि प्रत्येक टेबल ने प्रत्येक फूड आइटम को कितनी बार ऑर्डर किया। पहला कॉलम टेबल नंबर होगा और शेष कॉलम वर्णमाला क्रम में प्रत्येक फूड आइटम से संबंधित होंगे। पहली रो एक हेडर होनी चाहिए जिसका पहला कॉलम "Table" हो, उसके बाद फूड के नाम हों।
इसलिए, यदि इनपुट है orders = [["Amal","3","Paratha"],["Bimal","10","Biryni"],["Amal","3","Fried Chicken"],["Raktim","5","Water"],["Raktim","5","Paratha"],["Deepak","3","Paratha"]], तो आउटपुट होगा [["Table","Biryni","Fried Chicken","Paratha","Water"],["3","0","1","2","0"],["5","0","0","1","1"],["10","1","0","0","0"]]
इसे हल करने के लिए, हम निम्न चरणों का पालन करेंगे:
एक मैप m परिभाषित करें
एक सेट names परिभाषित करें
एक सेट t परिभाषित करें
ऑर्डर लिस्ट के प्रत्येक एलिमेंट it के लिए
it[1] को t में डालें
ok := true
यदि ok false है, तो -
v = खाली स्थान का उपयोग करके it[2] को विभाजित करने के बाद एक ऐरे
v के प्रत्येक एलिमेंट x के लिए, करें
(m[it[1], x] को 1 से बढ़ाएं)
x को names में डालें
अन्यथा
(m[it[1], it[2]] को 1 से बढ़ाएं)
it[2] को names में डालें
एक 2D ऐरे ret परिभाषित करें
names से एलिमेंट कॉपी करके एक ऐरे temp परिभाषित करें
"Table" को temp में पहले एलिमेंट के रूप में डालें
temp को ret के अंत में डालें
t के प्रत्येक एलिमेंट it के लिए, करें
एक ऐरे te परिभाषित करें
it को te के अंत में डालें
names के प्रत्येक एलिमेंट x के लिए, करें
m[it, x] को स्ट्रिंग के रूप में te के अंत में डालें
te को ret के अंत में डालें
ret ऐरे को सॉर्ट करें
ret लौटाएं
उदाहरण
बेहतर समझ के लिए निम्न कार्यान्वयन देखें:
#include <bits/stdc++.h> using namespace std; void print_vector(vector<vector<string> > v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << "["; for(int j = 0; j <v[i].size(); j++){ cout << v[i][j] << ", "; } cout << "],"; } cout << "]"<<endl; } typedef long long int lli; class Solution { public: vector <string> split(string& s, char delimiter){ vector <string> tokens; string token; istringstream tokenStream(s); while(getline(tokenStream, token, delimiter)){ tokens.push_back(token); } return tokens; } static bool cmp(vector <string>& a, vector <string>& b){ lli an = stol(a[0]); lli bn = stol(b[0]); return an < bn; } vector<vector<string>> displayTable(vector<vector<string>>& o) { map <string, map < string, int> >m; set <string> names; set <string> t; for(auto &it : o){ t.insert(it[1]); bool ok = true; if(!ok){ vector <string> v = split(it[2], ' '); for(auto& x : v){ m[it[1]][x]++; names.insert(x); } } else{ m[it[1]][it[2]]++; names.insert(it[2]); } } vector < vector <string> > ret; vector <string> temp(names.begin(), names.end()); temp.insert(temp.begin(), "Table"); ret.push_back(temp); for(auto& it : t){ vector <string> te; te.push_back(it); for(auto& x : names){ te.push_back(to_string(m[it][x])); } ret.push_back(te); } sort(ret.begin() + 1, ret.end(), cmp); return ret; } }; main(){ Solution ob; vector<vector<string>> v = {{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak"," 3","Paratha"}}; print_vector(ob.displayTable(v)); }इनपुट
{{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak"," 3","Paratha"}}आउटपुट
[[Table, Biryni, Fried Chicken, Paratha, Water, ],[3, 0, 1, 2, 0, ],[5, 0, 0, 1, 1, ],[10, 1, 0, 0, 0, ],]