id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<string> FilterLetters(string digits) {\n unordered_map<char, string> mp = {\n {'2', \"abc\"}, {'3', \"def\"}, {'4', \"ghi\"}, {'5', \"jkl\"},\n {'6', \"mno\"}, {'7', \"pqrs\"}, {'8', \"tuv\"}, {'9', \"wxyz\"}};\n\n vector<string> ret_strings;\n for (char digit : digits) {\n ret_strings.push_back(mp[digit]);\n }\n return ret_strings;\n }\n\n vector<string> GetCombinations(vector<string> strings) {\n vector<string> ret_combination;\n if (strings.size() == 0) {\n return ret_combination;\n }\n if (strings.size() == 1) {\n for(char c:strings[0]) {\n string char_string(1, c);\n ret_combination.push_back(char_string);\n }\n return ret_combination;\n }\n\n for(char c:strings[0]) {\n std::vector<string> rest_of_vec(strings.begin() + 1, strings.end());\n auto combinations = GetCombinations(rest_of_vec);\n for (string combination : combinations) {\n string char_string(1, c);\n ret_combination.push_back(char_string + combination);\n }\n }\n return ret_combination;\n }\n\n vector<string> letterCombinations(string digits) {\n vector<string> filtered_strings = FilterLetters(digits);\n\n return GetCombinations(filtered_strings);\n }\n};", "memory": "9000" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<string> letterCombinations(string digits) {\n if(digits.empty()){ return {};}\n vector<string> res = phonePad(\"\", digits);\n return res;\n \n \n }\n public:\n vector<string>phonePad(string p, string up){\n vector<string> ans;\n if(up.empty()){\n ans.push_back(p);\n return ans;\n }\n int digits =up[0]-'0';\n int start = (digits-2)*3;\n int end = (digits-1)*3;\n if(digits == 8 ){\n start = start+1;\n end = end+1;\n }\n if(digits == 7){\n end = end+1;\n }\n if(digits == 9){\n start = start+1;\n end = end+2;\n }\n\n\n for(int i = start; i<end; i++){\n char ch = ('a'+i);\n vector<string> comb = phonePad(p+ch, up.substr(1));\n ans.insert(ans.end(), comb.begin(), comb.end());\n }\n return ans;\n\n }\n};", "memory": "9100" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n map<int,string> buttons;\n Solution(){\n buttons[2]=\"abc\";\n buttons[3]=\"def\";\n buttons[4]=\"ghi\";\n buttons[5]=\"jkl\";\n buttons[6]=\"mno\";\n buttons[7]=\"pqrs\";\n buttons[8]=\"tuv\";\n buttons[9]=\"wxyz\";\n }\n\n void solve(vector<string>& out, string digits,int index,string comb){\n if(index==digits.size()) {\n out.push_back(comb);\n }\n int num = (int)digits[index] - 48;\n\n for(int i=0;i<buttons[num].size();i++){\n comb+=buttons[num][i];\n solve(out,digits,index+1,comb);\n comb.pop_back();\n }\n }\n\n vector<string> letterCombinations(string digits) {\n if(!digits.size()) return {};\n\n vector<string> out;\n string comb=\"\";\n solve(out,digits,0,comb);\n\n return out;\n }\n};\n", "memory": "9100" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n \n void solve(int ind,vector<string>st,vector<string>&ans,string res){\n \n if(ind == st.size()){\n if(res!=\"\")\n ans.push_back(res);\n return;\n }\n \n for(int i = 0;i<st[ind].size();i++){\n res+=st[ind][i];\n solve(ind+1,st,ans,res);\n res.pop_back();\n \n }\n \n }\n vector<string> letterCombinations(string digits) {\n \n \n unordered_map<int,string>mp;\n \n mp[2] = \"abc\";\n mp[3] = \"def\";\n mp[4] = \"ghi\";\n mp[5] = \"jkl\";\n mp[6] = \"mno\";\n mp[7] = \"pqrs\";\n mp[8] = \"tuv\";\n mp[9] = \"wxyz\";\n \n vector<string>st;\n \n for(int i = 0;i<digits.size();i++){\n st.push_back(mp[digits[i] - '0']);\n }\n vector<string>ans;\n \n solve(0,st,ans,\"\");\n return ans;\n \n }\n};", "memory": "9200" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n map<char,string> mp = {{'2',\"abc\"},{'3',\"def\"}, {'4',\"ghi\"}, {'5',\"jkl\"}, {'6',\"mno\"}, {'7',\"pqrs\"}, {'8',\"tuv\"}, {'9',\"wxyz\"}};\n vector<string> ans;\n void solve (string pro, string digit){\n if(digit.size()==0) {\n ans.push_back(pro);\n return;\n }\n char ch = digit[0];\n string unpro = mp[ch];\n digit.erase(digit.begin());\n for(int i = 0 ; i<unpro.size();i++){\n string op = pro + unpro[i];\n solve(op,digit);\n }\n }\n vector<string> letterCombinations(string digits) {\n if(digits==\"\") return ans;\n solve(\"\",digits);\n return ans;\n }\n};", "memory": "9300" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void bruteforce(int idx, string digits, string currCombination, vector<string>& allCombinations) {\n if (idx == digits.length()) {\n // список превращаем в строку\n allCombinations.push_back(currCombination);\n return;\n }\n unordered_map<char, string> phone;\n phone['2'] = \"abc\";\n phone['3'] = \"def\";\n phone['4'] = \"ghi\";\n phone['5'] = \"jkl\"; \n phone['6'] = \"mno\"; \n phone['7'] = \"pqrs\"; \n phone['8'] = \"tuv\"; \n phone['9'] = \"wxyz\";\n\n\n char digit = digits[idx];\n for (char letter : phone[digit]) {\n // добавили новую букву\n currCombination.push_back(letter);\n // перебрали все варианты для текущей буквы\n bruteforce(idx + 1, digits, currCombination, allCombinations);\n // убрали букву\n currCombination.pop_back();\n }\n }\n\n vector<string> letterCombinations(string digits) {\n // без if-a на входные данные \"\" вернем [\"\"], а должно быть []\n if (digits.length() == 0) {\n return {};\n }\n vector<string> ans;\n bruteforce(0, digits, \"\", ans);\n return ans;\n }\n};", "memory": "9400" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void solve(string digits,string a,vector<string> s,vector<string> &ans,int i)\n {\n if(i==digits.length())\n {\n ans.push_back(a);\n return;\n }\n for(int j=0;j<s[digits[i]-'2'].length();j++)\n {\n solve(digits,a+s[digits[i]-'2'][j],s,ans,i+1);\n }\n }\n vector<string> letterCombinations(string digits) \n {\n if (digits.empty()) return {};\n vector<string> s = {\"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\" };\n vector<string>ans;\n string a; \n solve(digits,a,s,ans,0);\n return ans; \n }\n};", "memory": "9400" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n //std::string phone_map[] = {\"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"};\n\n void backtrack(vector<string>& ans, string digits, string comb, int idx) {\n std::string phone_map[] = {\"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"};\n if (idx >= digits.size()) {\n ans.push_back(comb);\n return ;\n }\n int num = digits[idx] - '2';\n for (int i = 0;i < phone_map[num].size();i++) {\n backtrack(ans, digits, comb + phone_map[num][i], idx + 1);\n }\n }\n\n vector<string> letterCombinations(string digits) {\n if (digits == \"\") {\n return {};\n }\n vector<string> ans;\n backtrack(ans, digits, \"\", 0);\n return ans;\n }\n\n};", "memory": "9500" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<string> mapping(char c){\n if(c=='2') return {\"a\", \"b\", \"c\"};\n else if(c=='3') return {\"d\", \"e\", \"f\"};\n else if(c=='4') return {\"g\", \"h\", \"i\"};\n else if(c=='5') return {\"j\", \"k\", \"l\"};\n else if(c=='6') return {\"m\", \"n\", \"o\"};\n else if(c=='7') return {\"p\", \"q\", \"r\",\"s\"};\n else if(c=='8') return {\"t\", \"u\", \"v\"};\n else if(c=='9') return {\"w\", \"x\", \"y\", \"z\"};\n else return {};\n }\n void helper(int i, string temp, vector<string> &res, string digits){\n if(i==digits.size())res.push_back(temp);\n else{\n vector<string> map = mapping(digits[i]);\n for(int j=0; j<map.size(); j++){\n temp.push_back(map[j].front());\n helper(i+1, temp, res, digits);\n temp.pop_back();\n }\n }\n return;\n }\n vector<string> letterCombinations(string digits) {\n vector<string> res;\n if(digits==\"\") return res;\n helper(0, \"\", res, digits);\n return res;\n }\n};", "memory": "9500" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void recursion(string digits, vector<string> mappings, int index, string & temp, vector<string> & ans) {\n\n if(index == digits.size()) {\n ans.push_back(temp);\n return;\n }\n\n string letters = mappings[digits[index] - '2'];\n\n for(char letter : letters) {\n temp.push_back(letter);\n recursion(digits, mappings, index+1, temp, ans);\n temp.pop_back();\n }\n }\n vector<string> letterCombinations(string digits) {\n if(digits.size() == 0) {\n return {};\n }\n vector<string> ans;\n\n vector<string> mappings = {\"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"};\n\n int index = 0;\n string temp = \"\";\n recursion(digits, mappings, index, temp, ans);\n\n return ans;\n }\n};\n\n/*\n\nrecursion(digits, index, vector<string> mapp, temp, ans)\n\nif(digits size == index) {\n ans.push (temp);\n return;\n}\nindex = 0\nstring letters = \"abc\"\nfor (char : letters)\ntemp+=char;\nrecursion(digits, index+1, mapp, )\ntemp.pop_back();\n\n3^n == output size\n\nn : \n\ntemp string = \"ad\";\n\n23\n3 : def\n2 : abc\nad\n*/", "memory": "9600" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<string> letterCombinations(string digits) {\n std::vector<std::string> result;\n if(digits.size() > 0)\n {\n std::queue<std::string> strQueue;\n int firstDigit = digits[0] - '0';\n pushNumber(strQueue, firstDigit);\n int index = 1;\n int size = strQueue.size();\n while(index < digits.size())\n {\n int digit = digits[index] - '0';\n pushNumberWithExtra(strQueue, digit, std::move(strQueue.front()));\n strQueue.pop();\n size--;\n if(size == 0)\n {\n size = strQueue.size();\n index++;\n }\n }\n while(!strQueue.empty())\n {\n result.push_back(std::move(strQueue.front()));\n strQueue.pop();\n }\n }\n return result;\n }\n\n void pushNumberWithExtra(std::queue<std::string>& strQueue, int num, std::string str)\n {\n if(num == 2)\n {\n strQueue.push(std::move(str+'a'));\n strQueue.push(std::move(str+'b'));\n strQueue.push(std::move(str+'c'));\n }else if(num == 3)\n {\n strQueue.push(std::move(str+'d'));\n strQueue.push(std::move(str+'e'));\n strQueue.push(std::move(str+'f'));\n }else if(num == 4)\n {\n strQueue.push(std::move(str+'g'));\n strQueue.push(std::move(str+'h'));\n strQueue.push(std::move(str+'i'));\n }else if(num == 5)\n {\n strQueue.push(std::move(str+'j'));\n strQueue.push(std::move(str+'k'));\n strQueue.push(std::move(str+'l'));\n }else if(num == 6)\n {\n strQueue.push(std::move(str+'m'));\n strQueue.push(std::move(str+'n'));\n strQueue.push(std::move(str+'o'));\n }else if(num == 7)\n {\n strQueue.push(std::move(str+'p'));\n strQueue.push(std::move(str+'q'));\n strQueue.push(std::move(str+'r'));\n strQueue.push(std::move(str+'s'));\n }else if(num == 8)\n {\n strQueue.push(std::move(str+'t'));\n strQueue.push(std::move(str+'u'));\n strQueue.push(std::move(str+'v'));\n }else if(num == 9)\n {\n strQueue.push(std::move(str+'w'));\n strQueue.push(std::move(str+'x'));\n strQueue.push(std::move(str+'y'));\n strQueue.push(std::move(str+'z'));\n }\n }\n\n void pushNumber(std::queue<std::string>& strQueue, int num)\n {\n if(num == 2)\n {\n strQueue.push(\"a\");\n strQueue.push(\"b\");\n strQueue.push(\"c\");\n }else if(num == 3)\n {\n strQueue.push(\"d\");\n strQueue.push(\"e\");\n strQueue.push(\"f\");\n }else if(num == 4)\n {\n strQueue.push(\"g\");\n strQueue.push(\"h\");\n strQueue.push(\"i\");\n }else if(num == 5)\n {\n strQueue.push(\"j\");\n strQueue.push(\"k\");\n strQueue.push(\"l\");\n }else if(num == 6)\n {\n strQueue.push(\"m\");\n strQueue.push(\"n\");\n strQueue.push(\"o\");\n }else if(num == 7)\n {\n strQueue.push(\"p\");\n strQueue.push(\"q\");\n strQueue.push(\"r\");\n strQueue.push(\"s\");\n }else if(num == 8)\n {\n strQueue.push(\"t\");\n strQueue.push(\"u\");\n strQueue.push(\"v\");\n }else if(num == 9)\n {\n strQueue.push(\"w\");\n strQueue.push(\"x\");\n strQueue.push(\"y\");\n strQueue.push(\"z\");\n }\n }\n};\n", "memory": "9600" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<string> letterCombinations(string digits) {\n vector <string>ans;\n string output = \"\";\n int index = 0;\n vector<string> mapping = {\"\",\"\",\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n if(digits.size() == 0){\n return ans;\n }\n solve(digits,mapping,output,index,ans);\n return ans;\n }\n void solve(string digits,vector<string> mapping, string& output,int index,vector<string> &ans){\n if(index>= digits.size()){\n ans.push_back(output);\n return;\n } \n int number = digits[index]-'0';\n string value = mapping[number];\n for(int i=0;i<value.size();i++){\n output.push_back(value[i]);\n solve(digits,mapping,output,index+1,ans);\n output.pop_back();\n }\n }\n};", "memory": "9700" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void helper(int k, int n, string digits, vector<string>v, vector<string> &ans, string &temp){\n if(k==n){\n ans.push_back(temp);\n return;\n }\n int key=digits[k]-'0';\n int l=v[key].length();\n for(int i=0;i<l;i++){\n temp.push_back(v[key][i]);\n helper(k+1,n,digits,v,ans,temp);\n temp.pop_back();\n }\n }\n vector<string> letterCombinations(string digits) {\n int n=digits.length();\n if(n==0) return {};\n vector<string>v={\"\",\"\",\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n vector<string>ans;\n string temp;\n helper(0,n,digits,v,ans,temp);\n return ans;\n }\n};", "memory": "9700" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<string> letterCombinations(string digits) {\n vector<string> ans;\n string output;\n if(digits.length()==0){\n return ans;\n }\n int i=0;\n vector<string> mapping={\"\",\"\",\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n helper(digits,mapping,i,ans,output);\n return ans;\n }\n\n void helper(string digits,vector<string> mapping,int i,vector<string> &ans,string& output){\n if(i>=digits.length()){\n ans.push_back(output);\n return;\n }\n\n int num=digits[i]-'0';\n string value=mapping[num];\n\n for(int j=0;j<value.length();j++){\n output.push_back(value[j]);\n helper(digits,mapping,i+1,ans,output);\n output.pop_back();\n }\n }\n};", "memory": "9800" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\n void combination(vector<string> & result,string word,string digits,vector<string> values,int itr)\n {\n if(word.size()==digits.size())\n result.push_back(word);\n else\n {\n for(int i=0;i<values[digits[itr]-48].size();i++)\n combination(result,word+values[digits[itr]-48][i],digits,values,itr+1);\n }\n }\npublic:\n vector<string> letterCombinations(string digits) {\n vector<string> result;\n if(digits.size()==0)\n return result;\n // string values={\"\",\"\",\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n vector<string> values;\n values.push_back(\"\");\n values.push_back(\"\");\n values.push_back(\"abc\");\n values.push_back(\"def\");\n values.push_back(\"ghi\");\n values.push_back(\"jkl\");\n values.push_back(\"mno\");\n values.push_back(\"pqrs\");\n values.push_back(\"tuv\");\n values.push_back(\"wxyz\");\n\n combination(result,\"\",digits,values,0);\n return result;\n \n }\n};", "memory": "9900" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void solve(vector<string>s,string temp,string digits,int index,vector<string>&ans){\n if(index==digits.size() && temp!=\"\"){\n ans.push_back(temp);\n return;\n }\n for(int i=0;i<s.size();i++){\n if(digits[index]-'0'==i){\n for(int j=0;j<s[i].size();j++){\n temp.push_back(s[i][j]);\n solve(s,temp,digits,index+1,ans);\n temp.pop_back();\n }\n }\n }\n }\n vector<string> letterCombinations(string digits) {\n vector<string>s={\"\",\"1\",\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n vector<string>ans;\n solve(s,\"\",digits,0,ans);\n return ans;\n }\n};", "memory": "9900" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void fun(int size,int idx,string& digits,vector<string>& ans,string& substr,unordered_map<int,vector<char>> mpp){\n if(idx >= size){\n ans.push_back(substr);\n return;\n }\n int digit = digits[idx] - '0';\n int sizeoftheloop = mpp[digit].size();\n vector<char> characters = mpp[digit];\n\n for(int i=0;i<sizeoftheloop;i++){\n char ch = characters[i];\n substr.push_back(ch);\n fun(size,idx+1,digits,ans,substr,mpp);\n substr.pop_back();\n }\n return; \n }\n vector<string> letterCombinations(string digits) {\n int size = digits.size();\n vector<string> ans;\n if(size == 0){\n return ans;\n }\n unordered_map<int,vector<char>> mpp;\n mpp[2]={'a','b','c'}; \n mpp[3]={'d','e','f'}; \n mpp[4]={'g','h','i'}; \n mpp[5]={'j','k','l'}; \n mpp[6]={'m','n','o'}; \n mpp[7]={'p','q','r','s'}; \n mpp[8]={'t','u','v'}; \n mpp[9]={'w','x','y','z'}; \n \n string substr =\"\";\n int idx = 0;\n fun(size,idx,digits,ans,substr,mpp);\n return ans;\n }\n};", "memory": "10000" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void helper(vector<string> keypad,string ans,int index,vector<string>& result,string digits)\n {\n if(ans.size()==digits.size())\n {\n result.push_back(ans);\n return;\n }\n int digit=digits[index]-'0';\n string letter=keypad[digit];\n for(char c:letter)\n {\n helper(keypad,ans+c,index+1,result,digits);\n }\n\n }\n vector<string> letterCombinations(string digits) \n {\n vector<string> keypad = {\"\", \"\", \"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"};\n vector<string>result;\n if(digits.size()==0)\n {\n return {};\n }\n helper(keypad,\"\",0,result,digits);\n return result;\n \n }\n};", "memory": "10000" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void solve(vector<string>&ans,string output,int i,vector<string>mapping, string digits){\n \n if(i==digits.size()){\n ans.push_back(output);\n return;\n }\n int num = digits[i]-'0';\n\n string digit = mapping[num];\n \n for(auto it:digit){\n solve(ans ,output+it,i+1, mapping, digits );\n }\n}\n vector<string> letterCombinations(string digits) {\n vector<string>mapping{10};\n mapping[2] = \"abc\";\n mapping[3] = \"def\";\n mapping[4] = \"ghi\";\n mapping[5] = \"jkl\";\n mapping[6] = \"mno\";\n mapping[7] = \"pqrs\";\n mapping[8] = \"tuv\";\n mapping[9] = \"wxyz\";\n vector<string>ans;\n if(digits == \"\"){\n return ans;\n }\n string output;\n int i=0;\n solve(ans,output,i,mapping,digits);\n\n return ans;\n}\n};", "memory": "10100" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void findcombi(string digits,string&ans,vector<string>&v,unordered_map<int,string>mp,int i){\n if(ans.size()==digits.size()){\n v.push_back(ans);\n return;\n }\n \n int digit=digits[i]-'0';\n for(auto letter:mp[digit]){\n ans.push_back(letter);\n findcombi(digits,ans,v,mp,i+1);\n ans.pop_back();\n }\n }\n vector<string> letterCombinations(string digits) {\n if(digits.empty()){\n return {};\n }\n unordered_map<int,string>mp;\n mp[2]=\"abc\";\n mp[3]=\"def\";\n mp[4]=\"ghi\";\n mp[5]=\"jkl\";\n mp[6]=\"mno\";\n mp[7]=\"pqrs\";\n mp[8]=\"tuv\";\n mp[9]=\"wxyz\";\n vector<string>v;\n string ans;\n findcombi(digits,ans,v,mp,0);\n return v;\n }\n};", "memory": "10100" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void f(string& digits, vector<string>& ans, string temp, int idx, unordered_map<int,vector<char> > mp){\n // Base case.\n if(idx == digits.size()){\n if(temp.length() >= 1) ans.push_back(temp);\n return;\n }\n\n int num = digits[idx] - '0';\n\n for(int i = 0; i < mp[num].size(); i++){\n temp.push_back(mp[num][i]); \n f(digits, ans, temp, idx+1, mp);\n\n // Pruning call.\n temp.pop_back();\n }\n }\n vector<string> letterCombinations(string digits) {\n if(digits == \"\") return {};\n\n unordered_map<int, vector<char> > mp;\n mp[2] = {'a','b','c'};\n mp[3] = {'d', 'e', 'f'};\n mp[4] = {'g', 'h', 'i'};\n mp[5] = {'j', 'k', 'l'};\n mp[6] = {'m', 'n', 'o'};\n mp[7] = {'p', 'q', 'r', 's'};\n mp[8] = {'t', 'u', 'v'};\n mp[9] = {'w', 'x', 'y', 'z'};\n\n vector<string> ans;\n f(digits, ans, \"\", 0, mp);\n\n return ans;\n }\n};", "memory": "10200" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\n private:\n void solve(vector<string>record,string digits,int i,string output,vector<string>& ans){\n if(i>=digits.size()){\n if(output. length()!=0)\n ans.push_back (output);\n return;\n }\n int num=digits[i]-'0';\n string val=record[num];\n for(int j=0;j<val.length();j++){\n output.push_back(val[j]);\n solve (record, digits,i+1,output,ans);\n output.pop_back();\n }\n }\npublic:\n vector<string> letterCombinations(string digits) {\n vector<string> record={\"\",\"\",\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n string output;\n vector<string>ans;\n solve(record, digits,0,output,ans);\n return ans;\n }\n};", "memory": "10200" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void printVector(vector<string> res)\n {\n for(string str :res)\n {\n std::cout<<str<<std::endl;\n }\n std::cout<<\"----------------------------------------------\"<<std::endl;\n }\n vector<string> dfsPattern(std::unordered_map<char, vector<char>>& hmap, string digit, int index, string partial) \n {\n vector<string> result;\n if (index >= digit.size()) {\n result.push_back(partial);\n // printVector(result);\n return result;\n }\n\n vector<char> curr_vec = hmap[digit[index]];\n\n for (char c : curr_vec) {\n vector<string> temp_result = dfsPattern(hmap, digit, index + 1, partial + c);\n result.insert(result.end(), temp_result.begin(), temp_result.end());\n printVector(temp_result);\n }\n return result;\n }\n\n vector<string> letterCombinations(string digits) {\n std::unordered_map<char, vector<char>> hmap;\n\n hmap['2'] = {'a', 'b', 'c'};\n hmap['3'] = {'d', 'e', 'f'};\n hmap['4'] = {'g', 'h', 'i'}; // Corrected to 'g', 'h', 'i'\n hmap['5'] = {'j', 'k', 'l'}; // Added missing digit '5'\n hmap['6'] = {'m', 'n', 'o'};\n hmap['7'] = {'p', 'q', 'r', 's'};\n hmap['8'] = {'t', 'u', 'v'};\n hmap['9'] = {'w', 'x', 'y', 'z'};\n\n if (digits.empty()) return {};\n\n return dfsPattern(hmap, digits, 0, \"\");\n }\n};", "memory": "10300" }
17
<p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p> <p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p> <img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> digits = &quot;23&quot; <strong>Output:</strong> [&quot;ad&quot;,&quot;ae&quot;,&quot;af&quot;,&quot;bd&quot;,&quot;be&quot;,&quot;bf&quot;,&quot;cd&quot;,&quot;ce&quot;,&quot;cf&quot;] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> digits = &quot;&quot; <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> digits = &quot;2&quot; <strong>Output:</strong> [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= digits.length &lt;= 4</code></li> <li><code>digits[i]</code> is a digit in the range <code>[&#39;2&#39;, &#39;9&#39;]</code>.</li> </ul>
3
{ "code": "class Solution {\n void solve(string digits, string output, int i, vector<string>& answer, unordered_map<int, string> mapping){\n\n if(i >= digits.size())\n {\n answer.push_back(output);\n return;\n }\n\n string value = mapping[int(digits[i] - 48)];\n\n for(int j = 0; j < value.size(); j++)\n {\n output.push_back(value[j]);\n solve(digits, output, i + 1, answer, mapping);\n output.pop_back();\n }\n }\n\npublic:\n vector<string> letterCombinations(string digits) {\n \n vector<string> answer;\n unordered_map<int, string> aux;\n string output;\n\n aux[2] = \"abc\";\n aux[3] = \"def\";\n aux[4] = \"ghi\";\n aux[5] = \"jkl\";\n aux[6] = \"mno\";\n aux[7] = \"pqrs\";\n aux[8] = \"tuv\";\n aux[9] = \"wxyz\";\n\n if(digits.size() == 0) return {};\n \n solve(digits, output, 0, answer, aux);\n\n return answer;\n }\n};", "memory": "10400" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, n_str; getline(cin, str) && getline(cin, n_str); cout << '\\n') {\n const int n = stoi(n_str);\n auto end = str.end() - 2;\n for (int commas = 0; end != str.begin() && commas != n - 1; --end) if (*end == ',') ++commas;\n auto start = find(reverse_iterator(end), str.rend() - 2, ',').base() - 1;\n str.erase(start, end + 1);\n if (str[1] == ',') str.erase(1, 1);\n cout << str;\n }\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n return nullptr;\n }\n};", "memory": "7300" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nstatic const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, n_str; getline(cin, str) && getline(cin, n_str); cout << '\\n') {\n const int n = stoi(n_str);\n auto end = str.end() - 2;\n for (int commas = 0; end != str.begin() && commas != n - 1; --end) if (*end == ',') ++commas;\n auto start = find(reverse_iterator(end), str.rend() - 2, ',').base() - 1;\n str.erase(start, end + 1);\n if (str[1] == ',') str.erase(1, 1);\n cout << str;\n }\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n return nullptr;\n }\n};", "memory": "7400" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nstatic const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, n_str; getline(cin, str) && getline(cin, n_str); cout << '\\n') {\n const int n = stoi(n_str);\n auto end = str.end() - 2;\n for (int commas = 0; end != str.begin() && commas != n - 1; --end) if (*end == ',') ++commas;\n auto start = find(reverse_iterator(end), str.rend() - 2, ',').base() - 1;\n str.erase(start, end + 1);\n if (str[1] == ',') str.erase(1, 1);\n cout << str;\n }\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n return nullptr;\n }\n};", "memory": "7500" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nstatic const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, n_str; getline(cin, str) && getline(cin, n_str); cout << '\\n') {\n const int n = stoi(n_str);\n auto end = str.end() - 2;\n for (int commas = 0; end != str.begin() && commas != n - 1; --end) if (*end == ',') ++commas;\n auto start = find(reverse_iterator(end), str.rend() - 2, ',').base() - 1;\n str.erase(start, end + 1);\n if (str[1] == ',') str.erase(1, 1);\n cout << str;\n }\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n return nullptr;\n }\n};", "memory": "7600" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nstatic const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, n_str; getline(cin, str) && getline(cin, n_str); cout << '\\n') {\n const int n = stoi(n_str);\n auto end = str.end() - 2;\n for (int commas = 0; end != str.begin() && commas != n - 1; --end) if (*end == ',') ++commas;\n auto start = find(reverse_iterator(end), str.rend() - 2, ',').base() - 1;\n str.erase(start, end + 1);\n if (str[1] == ',') str.erase(1, 1);\n cout << str;\n }\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n return nullptr;\n }\n};", "memory": "7700" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n ListNode *current = head;\n int length = 0;\n while (current){\n length +=1;\n current = current -> next;\n }\n\n if (n == length) {\n head = head->next;\n return head;\n }\n\n int reverseNumber = length - n;\n ListNode *newCurrent = head; \n\n for (int i = 0 ; i<reverseNumber-1;i++){\n newCurrent = newCurrent -> next;\n }\n\n newCurrent->next = newCurrent -> next -> next;\n\n return head;\n\n }\n};", "memory": "14300" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) { \n ListNode* fast = head;\n ListNode* slow = head;\n\n for(int i=0; i<=n; i++){\n if(fast==nullptr)\n return head->next;\n \n fast=fast->next;\n }\n \n while(fast != nullptr ){\n fast=fast->next;\n slow=slow->next;\n }\n\n slow->next =slow->next->next;\n return head;\n \n }\n};", "memory": "14400" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n\n ListNode* slow = head;\n ListNode* fast = head;\n \n while(n)\n {\n fast= fast-> next; \n n--;\n }\n\n if(fast == NULL)\n return head->next;\n\n while( fast !=NULL && fast->next != NULL )\n {\n fast= fast -> next;\n slow= slow -> next;\n }\n\n // cout<<count;\n\nListNode* del = slow->next;\n\n slow->next= slow->next->next;\n\n delete del;\n\n \n return head;\n\n }\n\n \n};", "memory": "14400" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n if (n == 1 && head->next == NULL) {\n delete head;\n return NULL;\n }\n ListNode* fast = head;\n for (int i = 0; i < n; i++)\n fast = fast->next;\n if (fast == NULL) {\n ListNode* tmp = head->next;\n delete head;\n return tmp;\n }\n ListNode* slow = head;\n while (fast->next != NULL) {\n fast = fast->next;\n slow = slow->next;\n }\n ListNode* tmp = slow->next;\n slow->next = slow->next->next;\n delete tmp;\n return head;\n }\n};", "memory": "14500" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n if(head == NULL || head->next == NULL) return NULL;\n ListNode* temp = head;\n int size = 0;\n while(temp != NULL){\n temp = temp->next;\n size++;\n }\n temp = head;\n if(n == size){\n ListNode* delNode = head;\n head= head->next;\n delete delNode;\n return head;\n }\n for(int i=1; i<(size-n); i++){\n temp = temp->next;\n }\n ListNode* del= temp->next;\n temp->next = temp->next->next;\n delete del;\n return head;\n }\n};", "memory": "14500" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n if(!head){\n return NULL;\n }\n if(head->next==NULL){\n return NULL;\n }\n ListNode*slow=head;\n ListNode*fast=head;\n while(n--){\n fast=fast->next;\n if(fast==NULL){\n return head->next;\n }\n }\n while(fast!=NULL && fast->next!=NULL){\n fast=fast->next;\n slow=slow->next;\n }\n ListNode*current=slow->next;\n ListNode*prev=slow;\n prev->next=current->next;\n current->next=NULL;\n delete current;\n return head;\n\n\n }\n};", "memory": "14600" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n\n ListNode* slow = new ListNode();\n ListNode* fast = new ListNode();\n fast=head;\n\n for(int i=0; i<n; i++)\n {\n fast=fast->next;\n }\n\n if(fast==NULL)\n {\n return head->next;\n }\n\n slow=head;\n while(fast->next!=NULL )\n {\n fast=fast->next;\n slow=slow->next;\n }\n \n slow->next=slow->next->next;\n return head;\n\n }\n};", "memory": "14600" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "class Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n ListNode* dummy = new ListNode(0);\n dummy->next = head;\n ListNode* first = dummy;\n ListNode* second = dummy;\n\n for (int i = 0; i <= n; ++i) {\n first = first->next;\n }\n\n while (first != nullptr) {\n first = first->next;\n second = second->next;\n }\n\n ListNode* temp = second->next;\n second->next = second->next->next;\n delete temp;\n\n return dummy->next;\n }\n};", "memory": "14700" }
19
<p>Given the <code>head</code> of a linked list, remove the <code>n<sup>th</sup></code> node from the end of the list and return its head.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:</strong> head = [1,2,3,4,5], n = 2 <strong>Output:</strong> [1,2,3,5] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> head = [1], n = 1 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,2], n = 1 <strong>Output:</strong> [1] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is <code>sz</code>.</li> <li><code>1 &lt;= sz &lt;= 30</code></li> <li><code>0 &lt;= Node.val &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= sz</code></li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Could you do this in one pass?</p>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n ListNode* slow= head;\n ListNode* fast= head;\n for (int i=0; i<n; i++){\n fast= fast->next;\n }\n if(fast==NULL){\n return head->next;\n }\n while(fast->next!=NULL){\n slow= slow->next;\n fast= fast->next;\n }\n ListNode* node= slow->next;\n slow->next= slow->next->next;\n delete(node);\n return head;\n }\n};", "memory": "14700" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
0
{ "code": "#include <string>\n\nclass Solution {\npublic:\n bool isValid(std::string s) {\n std::string stack;\n\n auto match_char = [&](char expected){\n if (empty(stack) || stack.back() != expected) return false;\n stack.pop_back();\n return true;\n };\n\n for (auto c: s) {\n switch (c) {\n case '(': case '{': case '[': stack += c; break;\n case ')': if (!match_char('(')) return false; else break;\n case '}': if (!match_char('{')) return false; else break;\n default: if (!match_char('[')) return false; else break;\n }\n }\n return empty(stack);\n }\n};", "memory": "7300" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n int n = s.size();\n\n // OPTIMAL\n // stack<char> st;\n\n // for (int i = 0; i < n; i++) {\n // if (s[i] == '(' || s[i] == '{' || s[i] == '[') {\n // st.push(s[i]);\n // } else if (st.empty()) {\n // return false;\n // } else if ((s[i] == ')' && st.top() != '(') ||\n // (s[i] == ']' && st.top() != '[') ||\n // (s[i] == '}' && st.top() != '{')) {\n // return false;\n // } else {\n // st.pop();\n // }\n // }\n\n // return st.empty();\n\n // BRUTE\n while (s.size()) {\n int roundPos = s.find(\"()\");\n if (roundPos != string::npos) {\n s.erase(s.find(\"()\"), 2);\n }\n int curlyPos = s.find(\"{}\");\n if (curlyPos != string::npos) {\n s.erase(s.find(\"{}\"), 2);\n }\n int squarePos = s.find(\"[]\");\n if (squarePos != string::npos) {\n s.erase(s.find(\"[]\"), 2);\n }\n if (s.size() != 0 && roundPos == string::npos &&\n curlyPos == string::npos && squarePos == string::npos) {\n return false;\n }\n }\n // if (s.size() != 0) {\n // return false;\n // }\n return true;\n }\n};", "memory": "7400" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n string stk;\n for (char c : s) {\n if (c == '(' || c == '{' || c == '[')\n stk.push_back(c);\n else if (stk.empty() || !match(stk.back(), c))\n return false;\n else\n stk.pop_back();\n }\n return stk.empty();\n }\n\n bool match(char l, char r) {\n return (l == '(' && r == ')') || (l == '[' && r == ']') || (l == '{' && r == '}');\n }\n};", "memory": "7500" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char> sta;\n for (char c : s){\n if (c == '(' || c == '[' || c == '{') sta.push(c);\n else{\n if (sta.empty()) return false;\n char t = sta.top();\n if ((c == ')' && t == '(') || (c == ']' && t == '[') || (c == '}' && t == '{')) sta.pop();\n else return false;\n }\n }\n return sta.empty();\n }\n};", "memory": "7500" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n vector<char> tmp;\n int len = s.size();\n\n for(char ch:s) {\n if (ch=='(' || ch=='[' || ch == '{') {\n tmp.push_back(ch);\n }\n else {\n if (tmp.empty()) return false;\n\n auto it = tmp.end() - 1;\n if (ch==')') {\n if (*it != '(') return false;\n }\n else if (ch == '}') {\n if (*it != '{') return false;\n }\n else if (ch == ']') {\n if (*it != '[') return false;\n }\n else \n return false;\n tmp.pop_back();\n }\n }\n\n if (tmp.size() > 0) return false;\n\n return true;\n }\n};", "memory": "7600" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char> stackA;\n int n=s.size();\n for(int i=0;i<n;i++){\n char ch=s[i];\n // if openeing brackets are there\n if(ch=='(' || ch=='{' || ch=='['){\n stackA.push(ch);\n }\n else{\n // for closing brackets\n if(!stackA.empty()){\n char top=stackA.top();\n if( (ch==')' && top=='(') || (ch=='}' && top=='{') || (ch==']' && top=='[')){\n stackA.pop();\n } else{\n return false;\n }\n }\n else{\n return false;\n }\n } \n }\n if(stackA.empty()){\n return true; // ie stack has got emptied after all operations means all were valid\n }else{\n return false;\n }\n } \n};", "memory": "7600" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char>st;\n \n string a=\"()\";\n string b=\"[]\";\n string d=\"{}\";\n for(char c : s)\n {\n if(c=='(' || c=='[' || c=='{')\n {\n st.push(c);\n }\n else\n {\n if (st.empty()) {\n return false;\n }\n char top = st.top();\n // Check if the top of the stack matches the closing bracket\n if ((c == ')' && top == '(') || \n (c == '}' && top == '{') || \n (c == ']' && top == '[')) {\n st.pop(); \n }\n else{\n return false;\n }\n }\n }\n \n return st.empty();\n }\n};", "memory": "7700" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char>st;\n for(char c:s){\n if(c== '('|| c== '{'|| c== '['){\n st.push(c);\n }\n else{\n if(st.empty()||(c==')' && st.top() !='(')||(c=='}' && st.top() !='{')||(c==']' && st.top() !='[')){\n return false;\n }\n st.pop();\n }\n }\nreturn st.empty();\n }\n};", "memory": "7700" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\n char GetPairBracket(char bracket) {\n if (bracket == '(') {\n return ')';\n }\n if (bracket == '{') {\n return '}';\n }\n return ']';\n }\n\n bool isValid(string brackets) {\n std::stack<char> stack_brackets;\n int alpha = 0;\n\n for (auto bracket : brackets) {\n if (bracket == '(' || bracket == '[' || bracket == '{') {\n stack_brackets.push(bracket);\n alpha += 1;\n } else if (!stack_brackets.empty() && bracket == GetPairBracket(stack_brackets.top())) {\n stack_brackets.pop();\n alpha += 1;\n } else {\n break;\n }\n }\n\n return stack_brackets.empty() && alpha == brackets.size();\n }\n};\n\n\n\n\n\n\n\n", "memory": "7800" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n char OpenPare(char par){\n char open_pare;\n if(par == ']'){\n open_pare = '[';\n }\n if(par == '}'){\n open_pare = '{';\n }\n if(par == ')'){\n open_pare = '(';\n }\n return open_pare;\n }\n bool isValid(string s) {\n stack<char> stack;\n for(auto sym: s){\n if(sym == '{' || sym == '[' || sym == '(' ){\n stack.push(sym);\n }\n else {\n if(stack.empty() || OpenPare(sym) != stack.top()){\n return false;\n }\n stack.pop();\n }\n }\n return stack.size() == 0;\n }\n};", "memory": "7800" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char> st ; \n for (int i = 0 ; i< s.length() ; i++)\n {\n char ch = s[i];\n\n\n if (ch == '(' || ch == '{' || ch == '[')\n {\n st.push(ch) ; \n }\n\n else {\n\n if (!st.empty())\n {\n char top = st.top() ;\n if ((ch == ')' && top == '(') || \n (ch == '}' && top == '{') ||\n (ch == ']' && top == '[')) \n {\n \n st.pop() ;\n }\n else \n {\n return false ; \n }\n }\n else \n {\n\n return false ;\n }\n }\n }\n\n \n if (st.empty())\n {\n return true ; \n }\n return false ;\n }\n};\n ", "memory": "7900" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char> st;\n for(int i=0; i<s.length(); i++) {\n char ch = s[i];\n\n // If it's an opening bracket, push onto the stack\n if(ch == '(' || ch == '[' || ch == '{') {\n st.push(ch);\n }\n else {\n // If it's a closing bracket, check if the top of the stack matches\n if(!st.empty()) {\n char top = st.top();\n if((ch == ')' && top == '(') || (ch == ']' && top == '[') || (ch == '}' && top == '{')) {\n st.pop();\n }\n else {\n return false;\n }\n }\n else {\n return false;\n }\n }\n }\n\n // If the stack is empty, all brackets are matched\n return st.empty();\n }\n};", "memory": "7900" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char> st;\n int n = s.length();\n if(n%2 != 0) return false;\n for(int i=0;i<n;i++){\n if(s[i] == '(' || s[i]== '[' || s[i]== '{'){\n st.push(s[i]);\n }\n else if(s[i] == ')'){\n if(st.size()== 0 || st.top() != '(') return false;\n st.pop();\n }\n else if(s[i] == ']'){\n if(st.size()== 0 || st.top() != '[') return false;\n st.pop();\n }\n else if(s[i] == '}'){\n if(st.size()== 0 || st.top() != '{') return false;\n st.pop();\n } \n }\n if(st.size() == 0) return true;\n else return false;\n \n }\n};", "memory": "8000" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char> st; \n for (char c : s) { \n if (c == '(' || c == '{' || c == '[') {\n st.push(c); \n } else { \n if (st.empty() || \n (c == ')' && st.top() != '(') || \n (c == '}' && st.top() != '{') ||\n (c == ']' && st.top() != '[')) {\n return false; \n }\n st.pop(); \n }\n }\n return st.empty(); \n }\n};", "memory": "8000" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n int n = s.size();\n stack<int> st;\n for(int i =0;i<n;i++){\n if(s[i] == '('|| s[i] == '{'|| s[i] == '['){\n st.push(s[i]);\n } else {\n if(st.empty()) return false;\n char ch = st.top();\n st.pop();\n if((s[i] == ')' && ch != '(')||(s[i] == '}' && ch != '{')|| (s[i] == ']' && ch != '[')){\n return false;\n }\n \n }\n }\n if(st.empty()) return true;\n return false;\n }\n};", "memory": "8100" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char> st;\n unordered_map<char, char> charMap = {\n {')', '('},\n {'}', '{'},\n {']', '['}\n };\n\n for(char c : s){\n if(c == '(' || c == '{' || c == '[') {\n st.push(c);\n } else if (c == ')' || c == '}' || c == ']') {\n if(st.empty() || st.top() != charMap[c]) {\n return false;\n }\n st.pop();\n }\n }\n \n return st.empty();\n }\n};\n", "memory": "8100" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n\n stack<char> open;\n unordered_map<char, char> parens = {\n {')', '('},\n {']', '['},\n {'}', '{'},\n };\n for (const auto& c : s) {\n if (parens.find(c) != parens.end()) {\n if (open.empty()) {\n return false;\n }\n if (open.top() != parens[c]) {\n return false;\n }\n open.pop();\n } else {\n open.push(c);\n }\n }\n return open.empty();\n }\n};", "memory": "8200" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char> stack1;\n\n for(int i = 0;i<s.size();i++){\n if(s[i]=='('){\n stack1.push(')');\n }\n else if(s[i]=='{'){\n stack1.push('}');\n } \n else if(s[i]=='['){\n stack1.push(']');\n }\n else if(stack1.empty() || stack1.top()!=s[i]){\n return false;\n }else{\n stack1.pop();\n }\n }\n return stack1.empty();\n }\n};", "memory": "8200" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char> st;\n unordered_map<char, char> m;\n\n m['(']=')';\n m['{']='}';\n m['[']=']';\n\n char prev='a';\n\n if(s.length()==1)return false;\n for(int i=0; i<s.length(); i++){\n if(s[i]=='(' || s[i]=='{' || s[i]=='['){\n st.push(s[i]);\n prev=s[i];\n }\n else{\n if(!st.empty() and s[i]==m[prev]){\n st.pop();\n if(!st.empty())\n prev=st.top();\n }\n else\n return false;\n }\n }\n\n if(!st.empty())return false;\n return true;\n \n }\n};", "memory": "8300" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n bool isOpen(char c) {\n return c == '(' or c == '{' or c == '[';\n }\n bool isClose(char c) {\n return c == ')' or c == '}' or c == ']';\n }\n bool ArePair(char open, char close) {\n if (open == '(') {\n return close == ')';\n }\n else if (open == '{') {\n return close == '}';\n }\n else if (open == '[') {\n return close == ']';\n }\n\n return false;\n }\npublic:\n bool isValid(string s) {\n stack<int> stck;\n\n for (char c : s) {\n if (isOpen(c)) {\n stck.push(c);\n }\n else if (isClose(c)) {\n if (stck.empty()) {\n return false;\n }\n\n if (ArePair(stck.top(), c)) {\n stck.pop();\n }\n else {\n return false;\n }\n }\n }\n\n return stck.empty();\n }\n};\n\n// {[}]", "memory": "8300" }
20
<p>Given a string <code>s</code> containing just the characters <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;{&#39;</code>, <code>&#39;}&#39;</code>, <code>&#39;[&#39;</code> and <code>&#39;]&#39;</code>, determine if the input string is valid.</p> <p>An input string is valid if:</p> <ol> <li>Open brackets must be closed by the same type of brackets.</li> <li>Open brackets must be closed in the correct order.</li> <li>Every close bracket has a corresponding open bracket of the same type.</li> </ol> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;()[]{}&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;(]&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;([])&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s</code> consists of parentheses only <code>&#39;()[]{}&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n std::stack<char> q;\n for(char c : s) {\n if(c=='(') { q.push(')'); continue; }\n if(c=='{') { q.push('}'); continue; }\n if(c=='[') { q.push(']'); continue; } \n if(q.empty() || c != q.top()) return false;\n q.pop();\n } return q.empty();\n }\n};", "memory": "8400" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "\n#include <iostream>\nclass Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int s = boxTypes.size();\n int temp;\n int unitsum=0;\n for(int i=0;i<s;i++){\n temp =boxTypes[i][0];\n boxTypes[i][0]=boxTypes[i][1];\n boxTypes[i][1]=temp;\n \n }\n\n \n\n sort(boxTypes.begin(), boxTypes.end());\n int i=s-1;\n int b=1;\n int boxsum=0;\n while(boxsum <= truckSize){\n if(i < 0){\n break;\n }\n int b =1;\n //cout <<\"i is \"<<i<<endl;\n while(b<=boxTypes[i][1]){\n //cout<<\" unitium is\"<<unitsum<<endl;\n unitsum+=boxTypes[i][0];\n //cout<<\"b is \"<<b<<\" unitsum + \"<<boxTypes[i][0]<<\" = \"<<unitsum<<endl;\n boxsum++;\n //cout<<\"we have \"<<boxsum<<\"boxes\"<<endl;\n if(boxsum >= truckSize){\n \n break;\n }\n b++;\n \n }\n if(boxsum > truckSize){\n unitsum-=boxTypes[i][0];\n boxsum-=1;\n break;\n }\n else if(boxsum == truckSize){\n break;\n }\n i--;\n }\n //cout<<unitsum;\n return unitsum;\n }\n};", "memory": "19500" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(), boxTypes.end(),\n [](const vector<int>& a, const vector<int>& b) {\n return a[1] > b[1];\n });\n int totalUnits = 0;\n for (const auto& boxType : boxTypes) {\n int numberOfBoxes = boxType[0];\n int unitsPerBox = boxType[1];\n int boxesToTake = min(numberOfBoxes, truckSize);\n totalUnits += boxesToTake * unitsPerBox;\n truckSize -= boxesToTake;\n if (truckSize == 0) {\n break;\n }\n }\n return totalUnits;\n }\n};", "memory": "19500" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "bool cmp(vector<int>& b1, vector<int>& b2) { return b1[1] > b2[1]; }\n\nclass Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(), boxTypes.end(), cmp);\n int max_unit = 0;\n for (int i = 0; i < boxTypes.size(); i++) {\n if (boxTypes[i][0] <= truckSize ) {\n max_unit += (boxTypes[i][0] * boxTypes[i][1]);\n truckSize -= boxTypes[i][0];\n } else {\n max_unit += truckSize * boxTypes[i][1];\n truckSize = 0;\n }\n if (truckSize == 0) break;\n }\n return max_unit;\n }\n};", "memory": "19600" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int sum=0,max=0,index=0;\n\n while(truckSize>0){\n max=0;index=0;\n for(int i=0;i<boxTypes.size();i++){\n if(boxTypes[i][1]>max){\n max=boxTypes[i][1];\n index=i;\n }\n }\n if(max==0){\n break;\n }\n if(truckSize>boxTypes[index][0]){\n truckSize-=boxTypes[index][0];\n sum+=boxTypes[index][0]*boxTypes[index][1];\n //boxTypes[index][1]=0;\n boxTypes.erase(boxTypes.begin()+index);\n }\n else{\n sum+=truckSize*boxTypes[index][1];\n break;\n }\n }\n \n return sum;\n }\n};", "memory": "19600" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "bool cmp(vector<int>& a,vector<int>& b){\n return a[1]>b[1];\n}\nclass Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(),boxTypes.end(),cmp);\n int profit=0;\n for(int i=0;i<boxTypes.size();i++){\n if(boxTypes[i][0]<=truckSize){\n profit+=boxTypes[i][0]*boxTypes[i][1];\n truckSize-=boxTypes[i][0];\n }\n else{\n profit+=truckSize*boxTypes[i][1];\n truckSize=0;\n }\n if(truckSize==0) break;\n }\n return profit;\n }\n};", "memory": "19700" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int sum=0,max=0,index=0;\n\n while(truckSize>0){\n max=0;index=0;\n for(int i=0;i<boxTypes.size();i++){\n if(boxTypes[i][1]>max){\n max=boxTypes[i][1];\n index=i;\n }\n }\n if(max==0){\n break;\n }\n if(truckSize>boxTypes[index][0]){\n truckSize-=boxTypes[index][0];\n sum+=boxTypes[index][0]*boxTypes[index][1];\n boxTypes[index][1]=0;\n }\n else{\n sum+=truckSize*boxTypes[index][1];\n break;\n }\n }\n \n return sum;\n }\n};", "memory": "19700" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\n static bool sortfn(const vector<int>& v1,const vector<int>& v2)\n {\n return (v1[1]>v2[1]);\n }\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n if(boxTypes.size()==0)\n return 0;\n int ans = 0;\n sort(boxTypes.begin(),boxTypes.end(),sortfn);\n for(int i=0;i<boxTypes.size();i++)\n {\n if(truckSize>=boxTypes[i][0])\n {\n ans+=boxTypes[i][0]*boxTypes[i][1];\n truckSize-=boxTypes[i][0];\n }\n else\n {\n ans+=truckSize*boxTypes[i][1];\n truckSize=0;\n break;\n }\n }\n return ans;\n }\n};", "memory": "19800" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "bool cmp(vector<int>& a,vector<int>& b){\n return a[1]>b[1];\n}\nclass Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(),boxTypes.end(),cmp);\n int profit=0;\n for(int i=0;i<boxTypes.size();i++){\n if(boxTypes[i][0]<=truckSize){\n profit+=boxTypes[i][0]*boxTypes[i][1];\n truckSize-=boxTypes[i][0];\n }\n else{\n profit+=truckSize*boxTypes[i][1];\n truckSize=0;\n }\n if(truckSize==0) break;\n }\n return profit;\n }\n};", "memory": "19800" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "struct BoxTypesComparer {\n bool operator()(const vector<int> &lh, const vector<int> &rh) const {\n return lh[1] < rh[1];\n }\n};\nclass Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int ret = 0;\n sort(boxTypes.begin(), boxTypes.end(), BoxTypesComparer());\n while (boxTypes.size() && boxTypes.back()[0] <= truckSize) {\n vector<int> &boxType = boxTypes.back();\n ret += boxType[0] * boxType[1];\n truckSize -= boxType[0];\n boxTypes.pop_back();\n }\n if (boxTypes.size() && truckSize) {\n vector<int> &boxType = boxTypes.back();\n int number_of_boxes = truckSize;\n ret += boxType[1] * number_of_boxes;\n }\n return ret;\n }\n};", "memory": "19900" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& nums, int count) {\n vector <pair <int, int> > v(nums.size());\n for(int i=0; i<nums.size(); ++i) {\n pair <int, int> p;\n p.first = nums[i][1];\n p.second = nums[i][0];\n v[i] = p;\n }\n sort(v.begin(), v.end());\n reverse(v.begin(), v.end());\n int sum = 0;\n int j = 0;\n while(j<nums.size() && count > 0) {\n if(v[j].second <= count) {\n sum = sum + (v[j].first * v[j].second);\n count = count - v[j].second;\n j++;\n }\n else {\n sum = sum + (v[j].first * count);\n break;\n }\n }\n return sum;\n }\n};", "memory": "20000" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& nums, int count) {\n vector <pair <int, int> > v(nums.size());\n for(int i=0; i<nums.size(); ++i) {\n pair <int, int> p;\n p.first = nums[i][1];\n p.second = nums[i][0];\n v[i] = p;\n }\n sort(v.begin(), v.end());\n reverse(v.begin(), v.end());\n int sum = 0;\n int j = 0;\n while(j<nums.size() && count > 0) {\n if(v[j].second <= count) {\n sum = sum + (v[j].first * v[j].second);\n count = count - v[j].second;\n j++;\n }\n else {\n sum = sum + (v[j].first * count);\n break;\n }\n }\n return sum;\n }\n};", "memory": "20100" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& b, int t) {\n vector<int> a;\n int ans = 0;\n\n for (int i = 0; i < b.size(); i++) {\n a.push_back(b[i][1]);\n }\n\n sort(a.begin(), a.end());\n\n for (int i = a.size() - 1; i >= 0 && t > 0; i--) {\n for (int j = 0; j < b.size(); j++) {\n if (b[j][1] == a[i] && b[j][0] > 0) { \n int units= min(b[j][0], t);\n ans += units* b[j][1];\n t -= units;\n b[j][0] -= units;\n if (t == 0) break;\n }\n }\n }\n\n return ans;\n }\n};\n", "memory": "20200" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& b, int t) {\n vector<int> a;\n int ans = 0;\n\n for (int i = 0; i < b.size(); i++) {\n a.push_back(b[i][1]);\n }\n\n sort(a.begin(), a.end());\n\n for (int i = a.size() - 1; i >= 0 && t > 0; i--) {\n for (int j = 0; j < b.size(); j++) {\n if (b[j][1] == a[i] && b[j][0] > 0) { \n int units= min(b[j][0], t);\n ans += units* b[j][1];\n t -= units;\n b[j][0] -= units;\n if (t == 0) break;\n }\n }\n }\n\n return ans;\n }\n};\n", "memory": "20200" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n //THE COMMENTED APPORACH IS CORRECT BUT IS TOO HUGE TO USE IN THIS \n //QUESTION\n\n // int solve(vector<vector<int>>& arr, int target, int i, vector<vector<int>> &dp){\n // //base case\n // if(target==0){\n // return 0;\n // }\n // if(i>=arr.size()){\n // return 0;\n // }\n // if(dp[i][target]!=-1){\n // return dp[i][target];\n // }\n\n // int a=solve(arr,target,i+1,dp);\n // int b=0;\n\n // for(int j=1;j<=arr[i][0];j++){\n // if(j<=target){\n // int maxi=(j*arr[i][1])+solve(arr,target-j,i+1,dp);\n // b=max(b,maxi);\n // }\n // }\n // return dp[i][target] = max(a,b);\n // }\n\n static bool comp(pair<int,int>a , pair<int,int>b){\n return a.second>b.second;\n }\n\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n\n vector<pair<int,int>>mp;\n\n for(int i=0;i<boxTypes.size();i++){\n mp.push_back({boxTypes[i][0],boxTypes[i][1]});\n }\n\n sort(mp.begin(),mp.end(),comp);\n int ans=0;\n for(auto x:mp){\n if(truckSize==0){\n break;\n }\n if(truckSize>=x.first){\n cout<<\"1\"<<\" \"<<truckSize<<endl;\n ans+=(x.first*x.second);\n truckSize-=x.first;\n }\n else{\n cout<<\"2\"<<\" \"<<truckSize<<endl;\n ans+=(truckSize*x.second);\n truckSize=0;\n }\n }\n return ans;\n\n // vector<vector<int>>dp(boxTypes.size(),vector<int>(truckSize+1,-1));\n // return solve(boxTypes, truckSize, 0, dp);\n }\n};", "memory": "20300" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\nstatic bool cmp(pair<int,int> a, pair<int,int> b)\n{\n return a.second>b.second;\n}\n\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n vector<pair<int,int> > v;\n\n for(int i=0;i<boxTypes.size();i++)\n {\n pair<int,int> p= make_pair(boxTypes[i][0],boxTypes[i][1]);\n v.push_back(p);\n }\n\n sort(v.begin(),v.end(),cmp);\n\n int ans=0;\n\n for(int i=0;i<v.size();i++)\n {\n if(v[i].first>truckSize)\n {\n ans+=(truckSize*v[i].second);\n truckSize=0;\n }else{\n ans+=(v[i].first*v[i].second);\n truckSize-=v[i].first;\n }\n }\n\n return ans;\n }\n};", "memory": "20400" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n static bool fn(vector<int>& a, vector<int>& b) {\n if(a[1]!=b[1]) return a[1]>b[1];\n return a[0]>b[0]; \n }\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(),boxTypes.end(),fn);\n for(auto x:boxTypes) cout<<x[0]<<\" \"<<x[1]<<endl;\n int ans=0;\n int i=0;\n while(truckSize>0 && i<boxTypes.size()){\n if(boxTypes[i][0]<=truckSize){\n ans+=boxTypes[i][0]*boxTypes[i][1];\n truckSize-=boxTypes[i][0];\n i++;\n }\n else{\n ans+=truckSize*boxTypes[i][1];\n break;\n }\n }\n return ans;\n }\n};", "memory": "21100" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n \n int res = 0, n = boxTypes.size();\n\n sort(boxTypes.begin(), boxTypes.end(), \n [](auto & a, auto & b) \n { \n return a[1] > b[1]; \n });\n\n for(auto i : boxTypes) cout << i[0] << \" \" << i[1] << endl;\n\n for(int i = 0; i < n; i++)\n {\n while(truckSize && boxTypes[i][0]) \n {\n truckSize--;\n boxTypes[i][0]--;\n res += boxTypes[i][1];\n }\n }\n return res;\n }\n};", "memory": "21200" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(),boxTypes.end(),\n [](vector<int> &v1,vector<int> &v2){\n return v1[1] > v2[1];\n });\n int maxunits = 0;\n for(auto i: boxTypes){\n int maxboxes=min(i[0],truckSize);\n maxunits += maxboxes * i[1];\n truckSize -=maxboxes;\n }\n return maxunits;\n }\n};", "memory": "21300" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) \n {\n int n = boxTypes.size();\n int count = 0; \n map<int,int> mp;\n for(int i = 0; i < n; ++i)\n {\n mp[boxTypes[i][1]] += boxTypes[i][0];\n }\n\n for(auto iter = mp.rbegin(); iter != mp.rend() && truckSize > 0; ++iter)\n {\n if(iter->second <= truckSize)\n {\n truckSize -= iter->second;\n count += iter->first * iter->second;\n }\n else\n {\n count += iter->first * truckSize;\n truckSize = 0;\n }\n }\n\n return count;\n }\n};", "memory": "21400" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "#include <vector>\n#include <map>\nusing namespace std;\n\nclass Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int a = 0;\n map<int, int, greater<int>> box; \n for(int i = 0; i < boxTypes.size(); i++) {\n box[boxTypes[i][1]] += boxTypes[i][0];\n }\n\n for(auto itr = box.begin(); itr != box.end(); itr++) {\n if(truckSize <= 0) {\n break;\n } else {\n int b = itr->first; // Units per box\n int c = itr->second; // Number of boxes\n if(c <= truckSize) {\n a += (b * c);\n truckSize -= c;\n } else {\n a += (truckSize * b);\n truckSize = 0;\n }\n }\n }\n return a;\n }\n};\n", "memory": "21500" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int ans = 0;\n vector<int> units(1001, 0);\n for (auto box : boxTypes) {\n units[box[1]] += box[0];\n }\n for (int i = 1000; i >= 1 && truckSize; i--) {\n int n = min(units[i], truckSize);\n ans += n*i;\n truckSize -= n;\n }\n return ans;\n }\n};", "memory": "21600" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n priority_queue<pair<int, int>> valpq;\n \n for (int i = 0; i < boxTypes.size(); ++i) {\n vector<int> curtype = boxTypes[i];\n \n int numboxes = curtype[0];\n int numunits = curtype[1];\n \n valpq.push({numunits, numboxes});\n }\n \n int totalunits = 0;\n int spaceleft = truckSize;\n while (spaceleft > 0) {\n if (valpq.empty()) {\n break;\n }\n \n int bestboxes = valpq.top().second;\n int bestunits = valpq.top().first;\n valpq.pop();\n \n int numboxes = min(spaceleft, bestboxes);\n totalunits += numboxes * bestunits;\n spaceleft -= numboxes;\n }\n \n return totalunits;\n }\n};", "memory": "21700" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n priority_queue<pair<int, int>> pq;\n int count=0;\n for(vector<int> x: boxTypes){\n pq.push({x[1], x[0]});\n }\n\n int units=0;\n while(!pq.empty() && count<truckSize){\n if(count+pq.top().second < truckSize){\n count = count+pq.top().second;\n units+= pq.top().second * pq.top().first;\n pq.pop(); //remove the element once it is considered\n }\n else{\n int quantity = truckSize - count; //leftover quantity we need\n count+= quantity;\n units+= quantity*pq.top().first;\n pq.pop();\n }\n }\n return units;\n }\n};", "memory": "21800" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n vector<pair<int,int>> B;\n for (auto bt : boxTypes) {\n B.push_back(make_pair(bt[1], bt[0]));\n }\n sort(B.begin(), B.end());\n reverse(B.begin(), B.end());\n int output = 0;\n for (int i = 0; i < B.size(); i++) {\n int boxes = min(B[i].second, truckSize);\n truckSize -= boxes;\n output += boxes * B[i].first;\n }\n return output;\n }\n};", "memory": "21900" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n struct Node {\n int left;\n int right;\n\n bool operator < (const Node& t) const {\n return right < t.right;\n }\n };\n\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n vector<Node> arr;\n for (const auto box : boxTypes) arr.push_back({box[0], box[1]});\n sort(arr.begin(), arr.end());\n\n int ans = 0;\n for (int i = arr.size() - 1; truckSize > 0 && i >= 0; --i) {\n ans += min(arr[i].left, truckSize) * arr[i].right;\n truckSize -= min(arr[i].left, truckSize);\n }\n\n return ans;\n }\n};", "memory": "22000" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int n) {\n priority_queue<pair<int,int>> pq;\n for(auto it:boxTypes){\n pq.push({it[1],it[0]});\n }\n int cnt = 0;\n int ans = 0;\n while(!pq.empty() && cnt < n){\n auto it = pq.top();\n pq.pop();\n if((n-cnt)>=it.second){\n ans += it.first*it.second;\n cnt += it.second;\n }\n else{\n ans += (n-cnt)*it.first;\n cnt += (n-cnt);\n }\n }\n return ans;\n }\n};", "memory": "22100" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(),boxTypes.end(),[](const vector<int>& a, const\n vector<int>& b) {\n return a[1] > b[1];\n });\n for(auto it:boxTypes)cout<<it[0]<<\" \"<<it[1]<<endl;\n int k=truckSize;\n int ans=0;\n for(auto it:boxTypes){\n \n if(it[0]<k){\n k-=it[0];\n ans+=it[0]*it[1];\n }\n else{\n ans+=k*it[1];\n k=0;\n }\n\n if(k==0)break;\n }\n return ans;\n }\n};", "memory": "22200" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n sort(boxTypes.begin(),boxTypes.end(),[](const vector<int>& a, const\n vector<int>& b) {\n return a[1] > b[1];\n });\n for(auto it:boxTypes)cout<<it[0]<<\" \"<<it[1]<<endl;\n int k=truckSize;\n int ans=0;\n for(auto it:boxTypes){\n \n if(it[0]<k){\n k-=it[0];\n ans+=it[0]*it[1];\n }\n else{\n ans+=k*it[1];\n k=0;\n }\n\n if(k==0)break;\n }\n return ans;\n }\n};", "memory": "22200" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "\nbool cmp(vector<int>&a,vector<int>&b){\n return a[1]>b[1];\n}\nclass Solution {\npublic:\n int maximumUnits(vector<vector<int>>& box, int size) {\n sort(box.begin(),box.end(),cmp);\n for(auto it:box){\n for(int j:it){\n cout<<j<<\" \"; \n }\n }\n int ans=0;\n for(auto it:box){\n if(it[0]<=size){\n size-=it[0];\n ans+=it[0]*it[1];\n }\n else{\n ans+=size*it[1];\n size=0;\n }\n if(size==0)break;\n }\n return ans;\n }\n};", "memory": "22300" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n multimap<int, int> m;\n for (int i = 0; i < boxTypes.size(); i++) {\n m.insert({boxTypes[i][1], boxTypes[i][0]});\n }\n int res = 0;\n for (auto it = m.rbegin(); it != m.rend(); it++) {\n if (truckSize > 0) {\n int b = min(truckSize, it->second);\n truckSize -= b;\n res += b * it->first;\n }\n }\n \n return res;\n }\n};", "memory": "22400" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int ans = 0;\n\n multimap<int, int> m;\n\n for(int i = 0; i < boxTypes.size(); i++){\n m.insert({boxTypes[i][1], boxTypes[i][0]});\n }\n\n for(auto it = m.rbegin(); it != m.rend(); ++it){\n if(truckSize == 0) break;\n\n //if(it->second > truckSize){\n ans = ans + min(it->second, truckSize) * (it->first);\n truckSize = truckSize - min(it->second, truckSize);\n //}\n //else{\n // ans = ans + (it->first) * (it->second);\n //truckSize = truckSize - it->second;\n //}\n }\n return ans;\n }\n};", "memory": "22500" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n std::map<int, int, std::greater<int>> M;\n for(auto boxType: boxTypes)\n M[boxType[1]] += boxType[0];\n\n int ans = 0;\n for(auto x: M) {\n ans += min(truckSize, x.second) * x.first;\n truckSize -= x.second;\n if(truckSize <= 0)\n break;\n }\n return ans;\n }\n};", "memory": "22700" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& arr, int s) {\n vector<pair<double,int>> temp;\n int i = 0 ;\n for(auto it : arr){\n temp.push_back({(double)(it[1]*it[0])/(double)(it[0]),i});\n i++;\n }\n sort(temp.begin() , temp.end());\n for(auto it : temp){\n cout<<it.first<<\" \"<<it.second<<endl;\n }\n i = temp.size()-1;\n int ans = 0 ;\n while(s>0 && i>=0){\n int ind = temp[i].second;\n int box = arr[ind][0];\n int unit = arr[ind][1];\n if(s>=box){\n ans = ans + box*unit;\n s = s-box;\n }\n else{\n ans = ans + unit*s;\n s = 0;\n }\n cout<<ans<<\" \"<<s<<endl;\n i--;\n\n\n }\n return ans;\n }\n};", "memory": "22800" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n for (int i = 0; i < boxTypes.size(); i++) {\n int temp = boxTypes[i][0];\n boxTypes[i][0] = boxTypes[i][1];\n boxTypes[i][1] = temp;\n }\n\n sort(boxTypes.rbegin(), boxTypes.rend());\n\n int load = 0;\n int units = 0;\n int i = 0;\n\n while (load < truckSize && i < boxTypes.size()) {\n int boxes = boxTypes[i][1];\n int unit = boxTypes[i][0];\n if (truckSize - load >= boxes) {\n load += boxes;\n units += boxes * unit;\n } else {\n units += (truckSize - load) * unit;\n load = truckSize;\n }\n i++;\n }\n\n return units;\n }\n};", "memory": "22900" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int w) {\n vector<vector<int>>unit;\n for (int i = 0; i < boxTypes.size(); i++) {\n unit.push_back({boxTypes[i][1],boxTypes[i][0]});\n }\n sort(unit.begin(), unit.end(), greater<vector<int>>());\n int curr = 0;\n int ans = 0;\n for (int idx = 0; idx < boxTypes.size(); idx++) {\n \n if (curr +unit[idx][1] <= w) {\n ans += unit[idx][0] * unit[idx][1];\n curr +=unit[idx][1];\n } else {\n \n ans +=unit[idx][0] * (w - curr);\n break;\n \n }\n }\n\n return ans;\n }\n};\n\n\n// class Solution {\n// public:\n// int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n// sort(boxTypes.begin(), boxTypes.end(), [](const vector<int>& a, const vector<int>& b) {\n// return a[1] > b[1];\n// });\n\n// int totalUnits = 0;\n\n// for (auto& box : boxTypes) {\n// int boxCount = box[0];\n// int unitsPerBox = box[1];\n \n// if (truckSize >= boxCount) {\n// totalUnits += boxCount * unitsPerBox;\n// truckSize -= boxCount;\n// } else {\n// totalUnits += truckSize * unitsPerBox;\n// break;\n// }\n// }\n\n// return totalUnits;\n// }\n// };", "memory": "23000" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\n\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n\n int n = boxTypes.size();\n int tar = 0, ans = 0;\n\n for(int i=0;i<n;i++){\n swap(boxTypes[i][0],boxTypes[i][1]);\n }\n\n sort(boxTypes.rbegin(), boxTypes.rend());\n\n for (const auto& boxes : boxTypes) {\n if (tar + boxes[1] <= truckSize) {\n tar += boxes[1];\n ans += boxes[1] * boxes[0];\n } else {\n ans += (truckSize - tar) * boxes[0];\n break;\n }\n }\n return ans;\n }\n};", "memory": "23100" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n for (int i = 0; i < boxTypes.size(); i++) {\n int temp = boxTypes[i][0];\n boxTypes[i][0] = boxTypes[i][1];\n boxTypes[i][1] = temp;\n }\n\n sort(boxTypes.rbegin(), boxTypes.rend());\n\n int load = 0;\n int units = 0;\n int i = 0;\n\n while (load < truckSize && i < boxTypes.size()) {\n int boxes = boxTypes[i][1];\n int unit = boxTypes[i][0];\n if (truckSize - load >= boxes) {\n load += boxes;\n units += boxes * unit;\n } else {\n units += (truckSize - load) * unit;\n load = truckSize;\n }\n i++;\n }\n\n return units;\n }\n};", "memory": "23200" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int ans = 0;\n vector<vector<int>> list(boxTypes.size());\n for(int i = 0; i < boxTypes.size(); i++)\n {\n list[i].push_back(boxTypes[i][1]);\n list[i].push_back(boxTypes[i][0]);\n }\n sort(list.begin(), list.end(), greater<vector<int>>());\n for(int i = 0; i < list.size(); i++)\n {\n if(truckSize >= list[i][1])\n {\n ans += list[i][1] * list[i][0];\n truckSize -= list[i][1];\n }\n else\n {\n ans += truckSize * list[i][0];\n break;\n }\n }\n return ans;\n }\n};", "memory": "23300" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int ans = 0;\n vector<vector<int>> list(boxTypes.size());\n for(int i = 0; i < boxTypes.size(); i++)\n {\n list[i].push_back(boxTypes[i][1]);\n list[i].push_back(boxTypes[i][0]);\n }\n sort(list.begin(), list.end(), greater<vector<int>>());\n for(int i = 0; i < list.size(); i++)\n {\n if(truckSize >= list[i][1])\n {\n ans += list[i][1] * list[i][0];\n truckSize -= list[i][1];\n }\n else\n {\n ans += truckSize * list[i][0];\n truckSize = 0;\n }\n }\n return ans;\n }\n};", "memory": "23400" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& bT, int num) {\n int n=bT.size();\n vector<pair<int,int>>v;\n for(int a=0;a<n;a++){\n v.push_back({bT[a][1],bT[a][0]});\n }\n sort(v.rbegin(),v.rend());\n for(int a=0;a<n;a++){\n cout<<v[a].first<<\" \"<<v[a].second<<endl;\n }\n int sum=0;\n for(int a=0;a<n;a++){\n if(num>=v[a].second){\n sum+=v[a].second*v[a].first;\n num-=v[a].second;\n }\n else if(a<n){\n sum+=num*v[a].first;\n break;\n }\n }\n return sum;\n }\n};", "memory": "23600" }
1,829
<p>You are assigned to put some amount of boxes onto <strong>one truck</strong>. You are given a 2D array <code>boxTypes</code>, where <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code>:</p> <ul> <li><code>numberOfBoxes<sub>i</sub></code> is the number of boxes of type <code>i</code>.</li> <li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>is the number of units in each box of the type <code>i</code>.</li> </ul> <p>You are also given an integer <code>truckSize</code>, which is the <strong>maximum</strong> number of <strong>boxes</strong> that can be put on the truck. You can choose any boxes to put on the truck as long as the number&nbsp;of boxes does not exceed <code>truckSize</code>.</p> <p>Return <em>the <strong>maximum</strong> total number of <strong>units</strong> that can be put on the truck.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 <strong>Output:</strong> 8 <strong>Explanation:</strong> There are: - 1 box of the first type that contains 3 units. - 2 boxes of the second type that contain 2 units each. - 3 boxes of the third type that contain 1 unit each. You can take all the boxes of the first and second types, and one box of the third type. The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 <strong>Output:</strong> 91 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= boxTypes.length &lt;= 1000</code></li> <li><code>1 &lt;= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> &lt;= 1000</code></li> <li><code>1 &lt;= truckSize &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {\n int d=truckSize;\n vector<pair<int,int>>s;\n for(int i=0;i<boxTypes.size();i++)\n {\n s.push_back({boxTypes[i][1],boxTypes[i][0]});\n\n }\n\n sort(s.rbegin(),s.rend());\n for(auto y:s)\n {\n cout<<y.first<<y.second<<endl;\n }\n int ans=0;\n int first=0;int second=0;\n int tbox=0;\n bool flag=false;\n for(auto x:s)\n {\n second=x.second;\n first=x.first;\n tbox++;\n if(x.second>truckSize)\n {\n flag=true;\n break;\n }\n truckSize-=x.second;\n \n ans+=x.first*x.second;\n \n }\n if(flag==false)\n {\n return ans;\n }\n while(truckSize>0 )\n {\n if(second==0)\n {\n return ans;\n }\n ans+=first;\n truckSize--;\n second--;\n }\n\n return ans;\n }\n};", "memory": "23700" }