id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
228 | <p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p>
<p>A <strong>range</strong> <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive).</p>
<p>Return <em>the <strong>smallest sorted</strong> list of ranges that <strong>cover all the numbers in the array exactly</strong></em>. That is, each element of <code>nums</code> is covered by exactly one of the ranges, and there is no integer <code>x</code> such that <code>x</code> is in one of the ranges but not in <code>nums</code>.</p>
<p>Each range <code>[a,b]</code> in the list should be output as:</p>
<ul>
<li><code>"a->b"</code> if <code>a != b</code></li>
<li><code>"a"</code> if <code>a == b</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,2,4,5,7]
<strong>Output:</strong> ["0->2","4->5","7"]
<strong>Explanation:</strong> The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,2,3,4,6,8,9]
<strong>Output:</strong> ["0","2->4","6","8->9"]
<strong>Explanation:</strong> The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= nums.length <= 20</code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted in ascending order.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> summaryRanges(vector<int>& nums) {\n\n vector<string> ans;\n string temp = \"\";\n int n = nums.size();\n bool flag = 0;\n for (int i = 0; i < n; i++) {\n\n int x = nums[i];\n temp = to_string(nums[i]);\n flag = 0;\n while (i < n - 1 and x + 1 == nums[i + 1]) {\n i++;\n x++;\n flag = 1;\n }\n if (flag) {\n temp += \"->\" + to_string(nums[i]);\n ans.push_back(temp);\n } else {\n ans.push_back(temp);\n }\n }\n return ans;\n }\n};",
"memory": "8400"
} |
228 | <p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p>
<p>A <strong>range</strong> <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive).</p>
<p>Return <em>the <strong>smallest sorted</strong> list of ranges that <strong>cover all the numbers in the array exactly</strong></em>. That is, each element of <code>nums</code> is covered by exactly one of the ranges, and there is no integer <code>x</code> such that <code>x</code> is in one of the ranges but not in <code>nums</code>.</p>
<p>Each range <code>[a,b]</code> in the list should be output as:</p>
<ul>
<li><code>"a->b"</code> if <code>a != b</code></li>
<li><code>"a"</code> if <code>a == b</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,2,4,5,7]
<strong>Output:</strong> ["0->2","4->5","7"]
<strong>Explanation:</strong> The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,2,3,4,6,8,9]
<strong>Output:</strong> ["0","2->4","6","8->9"]
<strong>Explanation:</strong> The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= nums.length <= 20</code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted in ascending order.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> summaryRanges(vector<int>& nums) {\n vector<string> vec_str;\n string s1 = \"\";\n if (nums.size() == 0)\n return vec_str;\n int start = nums[0];\n int end = 0;\n bool flag = true;\n for (int i = 1; i < nums.size(); i++){\n if (nums[i] - 1 != nums[i - 1]){\n if (flag){\n s1 = to_string(nums[i - 1]);\n vec_str.push_back(s1);\n start = nums[i];\n }\n else{\n s1 = to_string(start) + \"->\" + to_string(nums[i - 1]);\n vec_str.push_back(s1);\n flag = true;\n start = nums[i];\n }\n }\n else{\n flag = false;\n }\n }\n if (flag){\n s1 = to_string(start);\n vec_str.push_back(s1);\n }\n else{\n s1 = to_string(start) + \"->\" + to_string(nums.back());\n vec_str.push_back(s1);\n }\n return vec_str;\n }\n};",
"memory": "8500"
} |
228 | <p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p>
<p>A <strong>range</strong> <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive).</p>
<p>Return <em>the <strong>smallest sorted</strong> list of ranges that <strong>cover all the numbers in the array exactly</strong></em>. That is, each element of <code>nums</code> is covered by exactly one of the ranges, and there is no integer <code>x</code> such that <code>x</code> is in one of the ranges but not in <code>nums</code>.</p>
<p>Each range <code>[a,b]</code> in the list should be output as:</p>
<ul>
<li><code>"a->b"</code> if <code>a != b</code></li>
<li><code>"a"</code> if <code>a == b</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,2,4,5,7]
<strong>Output:</strong> ["0->2","4->5","7"]
<strong>Explanation:</strong> The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,2,3,4,6,8,9]
<strong>Output:</strong> ["0","2->4","6","8->9"]
<strong>Explanation:</strong> The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= nums.length <= 20</code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted in ascending order.</li>
</ul>
| 0 | {
"code": "/*class Solution {\npublic:\n vector<string> summaryRanges(vector<int>& nums) {\n vector<char> temp;\n vector<string> ans;\n int start = nums[0];\n int end = 0;\n for(int i = 0; i < nums.size(); ){\n if(nums[i+1] == (nums[i] + 1)){\n i++;\n end = nums[i+1];\n continue;\n }\n else{\n if(start != end){\n temp.push_back(start);\n temp.push_back('-');\n temp.push_back('>');\n temp.push_back(end);\n string str(temp.begin(), temp.end());\n ans.push_back(str);\n }\n else{\n temp.push_back(start);\n string str(temp.begin(), temp.end());\n ans.push_back(str);\n }\n start = nums[i];\n }\n }\n return ans;\n }\n};*/\n\n#include <vector>\n#include <string>\n#include <iostream>\n\nusing namespace std;\n\nclass Solution {\npublic:\n vector<string> summaryRanges(vector<int>& nums) {\n vector<string> ans;\n if (nums.empty()) return ans;\n\n int start = nums[0];\n int end = nums[0];\n \n for (int i = 0; i < nums.size(); ++i) {\n if (i == nums.size() - 1 || nums[i + 1] != nums[i] + 1) {\n if (start == end) {\n ans.push_back(to_string(start));\n } else {\n ans.push_back(to_string(start) + \"->\" + to_string(end));\n }\n if (i < nums.size() - 1) {\n start = nums[i + 1];\n }\n end = start;\n } else {\n end = nums[i + 1];\n }\n }\n \n return ans;\n }\n};\n",
"memory": "8500"
} |
228 | <p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p>
<p>A <strong>range</strong> <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive).</p>
<p>Return <em>the <strong>smallest sorted</strong> list of ranges that <strong>cover all the numbers in the array exactly</strong></em>. That is, each element of <code>nums</code> is covered by exactly one of the ranges, and there is no integer <code>x</code> such that <code>x</code> is in one of the ranges but not in <code>nums</code>.</p>
<p>Each range <code>[a,b]</code> in the list should be output as:</p>
<ul>
<li><code>"a->b"</code> if <code>a != b</code></li>
<li><code>"a"</code> if <code>a == b</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,2,4,5,7]
<strong>Output:</strong> ["0->2","4->5","7"]
<strong>Explanation:</strong> The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,2,3,4,6,8,9]
<strong>Output:</strong> ["0","2->4","6","8->9"]
<strong>Explanation:</strong> The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= nums.length <= 20</code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted in ascending order.</li>
</ul>
| 2 | {
"code": "#include <vector>\n#include <string>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<string> summaryRanges(vector<int>& nums) {\n vector<string> result;\n if (nums.empty()) return result;\n \n int start = nums[0]; // Start of a range\n \n for (int i = 1; i <= nums.size(); ++i) {\n // Check if the current number is not consecutive or we've reached the end of the array\n if (i == nums.size() || nums[i] != nums[i - 1] + 1) {\n if (start == nums[i - 1]) {\n result.push_back(to_string(start)); // Single number range\n } else {\n result.push_back(to_string(start) + \"->\" + to_string(nums[i - 1])); // Range with multiple numbers\n }\n // Update the start of the next range if there are more elements\n if (i < nums.size()) {\n start = nums[i];\n }\n }\n }\n \n return result;\n }\n};\n\n",
"memory": "8600"
} |
228 | <p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p>
<p>A <strong>range</strong> <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive).</p>
<p>Return <em>the <strong>smallest sorted</strong> list of ranges that <strong>cover all the numbers in the array exactly</strong></em>. That is, each element of <code>nums</code> is covered by exactly one of the ranges, and there is no integer <code>x</code> such that <code>x</code> is in one of the ranges but not in <code>nums</code>.</p>
<p>Each range <code>[a,b]</code> in the list should be output as:</p>
<ul>
<li><code>"a->b"</code> if <code>a != b</code></li>
<li><code>"a"</code> if <code>a == b</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,2,4,5,7]
<strong>Output:</strong> ["0->2","4->5","7"]
<strong>Explanation:</strong> The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,2,3,4,6,8,9]
<strong>Output:</strong> ["0","2->4","6","8->9"]
<strong>Explanation:</strong> The ranges are:
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= nums.length <= 20</code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted in ascending order.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<string> summaryRanges(vector<int>& nums) {\n \n if(nums.size()==0) return {};\n \n vector<string> ans;\n int n=nums.size();\n \n int start=nums[0];\n int last=nums[0];\n int cnt=nums[0];\n for(int i=1;i<n;i++)\n {\n if(nums[i]==cnt+1)\n {\n last++;\n cnt++;\n }\n else\n {\n string s=\"\";\n if(start!=last)\n {\n s+=to_string(start)+\"->\"+to_string(last);\n ans.push_back(s);\n }else\n {\n s=to_string(start);\n ans.push_back(s);\n }\n start=nums[i];\n last=nums[i];\n cnt=nums[i];\n }\n }\n string s=\"\";\n if(start!=last)\n {\n s+=to_string(start)+\"->\"+to_string(last);\n ans.push_back(s);\n }else\n {\n s=to_string(start);\n ans.push_back(s);\n }\n \n return ans;\n \n }\n};",
"memory": "8600"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n vector<int>ans;\n int cnt1=0,cnt2=0;\n int el1=INT_MIN;\n int el2=INT_MIN;\n int n=nums.size();\n for(int i=0;i<n;i++){\n if(cnt1==0 && el2!=nums[i]){\n cnt1=1;\n el1=nums[i];\n }\n else if(cnt2==0 && el1!=nums[i]){\n cnt2=1;\n el2=nums[i];\n }\n else if(nums[i]==el1)cnt1++;\n else if(nums[i]==el2)cnt2++;\n else cnt1--,cnt2--;\n }\n cnt1=0,cnt2=0;\n for(int i=0;i<n;i++){\n if(nums[i]==el1)cnt1++;\n if(nums[i]==el2)cnt2++;\n }\n int check=int(n/3)+1;\n if(cnt1>=check)ans.push_back(el1);\n if(cnt2>=check)ans.push_back(el2);\n return ans;\n }\n};",
"memory": "18300"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int candidate1 = 0, candidate2 = 0, count1 = 0, count2 = 0;\n int n = nums.size();\n\n // First pass: find potential candidates\n for (int num : nums) {\n if (num == candidate1) {\n count1++;\n } else if (num == candidate2) {\n count2++;\n } else if (count1 == 0) {\n candidate1 = num;\n count1 = 1;\n } else if (count2 == 0) {\n candidate2 = num;\n count2 = 1;\n } else {\n count1--;\n count2--;\n }\n }\n\n // Second pass: confirm the candidates\n count1 = count2 = 0;\n for (int num : nums) {\n if (num == candidate1) count1++;\n else if (num == candidate2) count2++;\n }\n\n vector<int> result;\n if (count1 > n / 3) result.push_back(candidate1);\n if (count2 > n / 3) result.push_back(candidate2);\n\n return result;\n}\n\n};",
"memory": "18400"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int n = nums.size();\n int c1 = 0, c2 = 0;\n int ele1, ele2;\n\n for(int i=0; i<n; i++) {\n if(c1 == 0 && ele2 != nums[i]) {\n c1 = 1;\n ele1 = nums[i];\n }\n else if(c2 == 0 && ele1 != nums[i]) {\n c2 = 1;\n ele2 = nums[i];\n }\n else if(ele1 == nums[i])\n c1++;\n else if(ele2 == nums[i])\n c2++;\n else {\n c1--;\n c2--;\n }\n }\n\n int maj1 = 0, maj2 = 0;\n for(int i=0; i<n; i++) {\n if(nums[i] == ele1) maj1++;\n if(nums[i] == ele2) maj2++;\n }\n\n if(maj1 > n/3 && maj2 > n/3) return {ele1, ele2};\n if(maj1 > n/3) return {ele1};\n if(maj2 > n/3) return {ele2};\n\n return {};\n }\n};",
"memory": "18400"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n //1.\n\n // vector<int>vec;\n // int n = nums.size();\n // map<int,int>mp;\n // for(int i = 0; i<n; i++){\n // mp[nums[i]]++;\n // }\n // for(auto it:mp){\n // if(it.second>n/3){\n // vec.push_back(it.first);\n // }\n // }\n // return vec;\n\n //2. better use of map;\n\n // int n = nums.size(); //size of the array\n // vector<int> ls; // list of answers\n\n // //declaring a map:\n // map<int, int> mpp;\n\n // // least occurrence of the majority element:\n // int mini = int(n / 3) + 1;\n\n // //storing the elements with its occurnce:\n // for (int i = 0; i < n; i++) {\n // mpp[nums[i]]++;\n\n // //checking if v[i] is\n // // the majority element:\n // if (mpp[nums[i]] == mini) {\n // ls.push_back(nums[i]);\n // }\n // if (ls.size() == 2) break;\n // }\n\n // return ls;\n\n\n //3. Optimal approach(to solve this problem in o(1))- Moore Voting Algorithm\n vector<int>vec;\n int candidate1 = 0;\n int candidate2 = 0;\n int count1 = 0;\n int count2 = 0;\n for(int i = 0; i<nums.size(); i++){\n if(nums[i]==candidate1){\n count1++;\n }\n else if(nums[i]==candidate2){\n count2++;\n }\n else if(count1==0 && nums[i]!=candidate2){\n candidate1 = nums[i];\n count1++;\n }\n else if(count2==0 && nums[i]!=candidate1){\n candidate2 = nums[i];\n count2++;\n }\n else{\n count1--;\n count2--;\n }\n }\n\n int cnt1 = 0;\n int cnt2 = 0;\n for(int i = 0; i<nums.size(); i++){\n if(nums[i]==candidate1) cnt1++;\n else if(nums[i]==candidate2){\n cnt2++;\n } \n }\n if(cnt1>nums.size()/3) vec.push_back(candidate1);\n if(cnt2>nums.size()/3) vec.push_back(candidate2);\n return vec;\n }\n};",
"memory": "18500"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& v) {\n int n = v.size(); //size of the array\n\n int cnt1 = 0, cnt2 = 0; // counts\n int el1 = INT_MIN; // element 1\n int el2 = INT_MIN; // element 2\n\n // applying the Extended Boyer Moore's Voting Algorithm:\n for (int i = 0; i < n; i++) {\n if (cnt1 == 0 && el2 != v[i]) {\n cnt1 = 1;\n el1 = v[i];\n }\n else if (cnt2 == 0 && el1 != v[i]) {\n cnt2 = 1;\n el2 = v[i];\n }\n else if (v[i] == el1) cnt1++;\n else if (v[i] == el2) cnt2++;\n else {\n cnt1--, cnt2--;\n }\n }\n\n vector<int> ls; // list of answers\n\n // Manually check if the stored elements in\n // el1 and el2 are the majority elements:\n cnt1 = 0, cnt2 = 0;\n for (int i = 0; i < n; i++) {\n if (v[i] == el1) cnt1++;\n if (v[i] == el2) cnt2++;\n }\n\n int mini = int(n / 3) + 1;\n if (cnt1 >= mini) ls.push_back(el1);\n if (cnt2 >= mini) ls.push_back(el2);\n\n // Uncomment the following line\n // if it is told to sort the answer array:\n // sort(ls.begin(), ls.end()); //TC --> O(2*log2) ~ O(1);\n\n return ls;\n }\n};",
"memory": "18500"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int n = nums.size();\n int m1 = INT_MIN;\n int m2 = INT_MIN;\n int f1 = 0, f2 = 0;\n vector<int> ans;\n for(int x : nums)\n {\n if( f1 == 0 && x != m2)\n {\n m1 = x;\n f1++;\n }\n else if(f2 == 0 && x != m1)\n {\n f2++;\n m2 = x;\n }\n else if(x == m1) f1++;\n else if(x == m2) f2++;\n else\n {\n f1--;\n f2--;\n }\n }\n int cnt1 = 0, cnt2 = 0;\n for (int x : nums) {\n if (x == m1) cnt1++;\n if (x == m2) cnt2++;\n }\n int mini = int(n / 3) + 1;\n if (cnt1 >= mini) ans.push_back(m1);\n if (cnt2 >= mini) ans.push_back(m2);\n return ans;\n // I haven't coded the O(1) solution yet.\n\n // initially, I was mistaking this for asking the top 3 elements. NO!\n // it is just asking for values with freq. above n/3.\n\n // o(N) space : frequency map\n // us map se heap of pair of integers -- NO! you have it wrong!\n // it is asking for values with frequency above a threshold. Not the top 3 elements.\n // it just so happens that the max no. of values that can have a frequency above \n // n/3 are 2.\n // so we could have a maximum of 2 values in our answer list.(and at minimum, 0 \n // values.)\n }\n};",
"memory": "18600"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums)\n {\n vector<int> res;\n int n = nums.size();\n int c1 = 0;\n int c2 = 0;\n int e1 = INT_MIN;\n int e2 = INT_MIN;\n\n for(int i = 0;i < n;i++)\n {\n if(c1 == 0 && e2 != nums[i])\n {\n c1 = 1;\n e1 = nums[i];\n }\n else if(c2 == 0 && e1 != nums[i])\n {\n c2 = 1;\n e2 = nums[i];\n }\n else if(nums[i] == e1)\n {\n c1++;\n }\n else if(nums[i] == e2)\n {\n c2++;\n }\n else\n {\n c1--;\n c2--;\n }\n }\n\n c1 = 0;\n c2 = 0;\n\n for(int i = 0;i < n;i++)\n {\n if(nums[i] == e1) c1++;\n else if(nums[i] == e2)c2++;\n }\n int majority = n / 3;\n if(c1 > majority) res.emplace_back(e1);\n if(c2 > majority)res.emplace_back(e2);\n return res;\n }\n};",
"memory": "18600"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int n = nums.size();\n int cnt1 = 0;\n int cnt2=0;\n int el1 = INT_MIN;\n int el2 = INT_MIN;\n vector<int> list;\n int mini = n/3 + 1;\n for(int i =0;i<n;i++){\n if(cnt1 == 0 && nums[i]!=el2){\n cnt1++;\n el1 = nums[i];\n }\n else if(cnt2 == 0 && nums[i]!=el1){\n cnt2++;\n el2 = nums[i];\n }\n else if(el1 == nums[i]) cnt1++;\n else if(el2 == nums[i]) cnt2++;\n else{\n cnt1--;\n cnt2--;\n }\n }\n // manual check\n cnt1 = 0;\n cnt2 = 0;\n for(int i =0 ;i<n;i++){\n if(el1 == nums[i]) cnt1++;\n if(el2 == nums[i]) cnt2++;\n }\n if(cnt1 >= mini) list.push_back(el1);\n if(cnt2>=mini) list.push_back(el2);\n return list ;\n }\n};",
"memory": "18700"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int n=nums.size();\n vector<int> ans;\n int c1=0,c2=0,e1,e2;\n for(int i=0;i<n;i++)\n {\n if(c1==0 && e2!=nums[i])\n {\n c1++;\n e1=nums[i];\n }\n else if(c2==0 && e1!=nums[i])\n {\n c2++;\n e2=nums[i];\n }\n else if(e1==nums[i])\n c1++;\n else if(e2==nums[i])\n c2++;\n else\n {\n c1--;\n c2--;\n }\n }\n\n c1=0,c2=0;\n for(int i=0;i<n;i++)\n {\n if(nums[i]==e1)\n c1++;\n if(nums[i]==e2)\n c2++;\n }\n\n if(c1>n/3)\n ans.push_back(e1);\n if(c2>n/3 && e2!=e1)\n ans.push_back(e2);\n\n return ans;\n }\n};",
"memory": "18700"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n if(nums.size()==1) return nums;\n int x,y;\n x=INT_MAX; y=INT_MAX;\n int count_x, count_y;\n count_x=0; count_y=0;\n for(int i=0; i<nums.size(); i++){\n if(x!=nums[i] && y!=nums[i]){\n if(count_x==0){\n x=nums[i];\n count_x++;\n }\n else if(count_y==0){\n y=nums[i];\n count_y++;\n }\n else{\n count_x--;\n count_y--;\n }\n }\n else if(x==nums[i]) count_x++;\n else if(y==nums[i]) count_y++;\n }\n count_x=0; count_y=0;\n for(int it: nums){\n if(it==x) count_x++;\n else if(it==y) count_y++;\n }\n if(count_x>nums.size()/3 && count_y>nums.size()/3) return {x, y};\n else if(count_x>nums.size()/3) return {x};\n else if(count_y>nums.size()/3) return {y};\n else return {};\n }\n};",
"memory": "18800"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int n = nums.size();\n map<int, int> map;\n vector<int> result;\n\n for (int i = 0; i < n; i++) {\n map[nums[i]] += 1;\n }\n\n for (auto it : map) {\n if (it.second > floor(n / 3)) {\n result.push_back(it.first);\n }\n }\n\n return result;\n }\n};",
"memory": "18800"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) \n {\n int n = nums.size();\n int maj = n/3;\n \n int maj1 = 0, maj2 = 0;\n \n int vote1 = 0,vote2 = 0;\n for(int i = 0 ;i<n;i++)\n {\n if(nums[i] == maj1)\n vote1++;\n else if(nums[i] == maj2)\n vote2++;\n else if(vote1 == 0)\n {\n maj1 = nums[i];\n vote1 = 1;\n }\n else if(vote2 == 0)\n {\n maj2 = nums[i];\n vote2 = 1;\n }\n else\n {\n vote1--;\n vote2--;\n }\n }\n vote1 = 0;\n vote2 = 0;\n\n for(int i = 0;i<nums.size();i++)\n {\n if(nums[i] == maj1)\n vote1++;\n else if(nums[i] == maj2)\n vote2++;\n }\n\n vector<int> ans;\n if(vote1 > maj)\n ans.push_back(maj1);\n if(vote2 > maj)\n ans.push_back(maj2);\n \n return ans;\n }\n};",
"memory": "18900"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) \n {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n int n = nums.size();\n int maj = n/3;\n \n int maj1 = 0, maj2 = 0;\n \n int vote1 = 0,vote2 = 0;\n for(int i = 0 ;i<n;i++)\n {\n if(nums[i] == maj1)\n vote1++;\n else if(nums[i] == maj2)\n vote2++;\n else if(vote1 == 0)\n {\n maj1 = nums[i];\n vote1 = 1;\n }\n else if(vote2 == 0)\n {\n maj2 = nums[i];\n vote2 = 1;\n }\n else\n {\n vote1--;\n vote2--;\n }\n }\n vote1 = 0;\n vote2 = 0;\n\n for(int i = 0;i<nums.size();i++)\n {\n if(nums[i] == maj1)\n vote1++;\n else if(nums[i] == maj2)\n vote2++;\n }\n\n vector<int> ans;\n if(vote1 > maj)\n ans.push_back(maj1);\n if(vote2 > maj)\n ans.push_back(maj2);\n \n return ans;\n }\n};",
"memory": "18900"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int size = nums.size();\n if (size == 0) {\n return vector<int>{0};\n } \n map<int, int> m;\n int num = size / 3;\n if(size == 1 && nums[0] > num) {\n return nums;\n }\n\n set <int> v;\n for (auto it : nums) {\n if (m.contains(it)) {\n m[it] += 1;\n } else {\n m[it] = 1;\n // if(1 > num) {\n // v.push_back(it);\n // }\n } \n if (m[it] > num) {\n v.insert(it);\n }\n }\n // cout<<\"\\n\";\n // unordered_set <int> s(v.begin(), v.end());\n // v.clear();\n // copy(s.begin(), s.end(), back_inserter(v));\n\n return vector <int> (v.begin(), v.end());\n }\n};",
"memory": "19000"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int n=nums.size()/3;\n vector<int>ans;\n ans.reserve(2);\n map<int,int>m;\n for(auto i:nums){\n m[i]++;\n }\n for(auto x:m){\n if(x.second>n) ans.push_back(x.first);\n }\n return ans;\n }\n};",
"memory": "19000"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int count = 0;\n vector<int> ans;\n int ind = nums[0];\n sort(nums.begin(), nums.end());\n for (int i = 0; i < nums.size(); i++) {\n\n if (nums[i] == ind)\n count++;\n else {\n ind = nums[i];\n count = 1;\n }\n if (count > nums.size() / 3 && find(ans.begin(),ans.end(),ind) == ans.end()) {\n ans.push_back(ind);\n }\n }\n return ans;\n }\n};",
"memory": "19100"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int> v) {\n int n = v.size(); \n\n int cnt1 = 0, cnt2 = 0; \n int el1 = INT_MIN; \n int el2 = INT_MIN; \n\n // applying the Extended Boyer Moore's Voting Algorithm:\n for ( int i = 0; i < n; i++ ) {\n if ( cnt1 == 0 && el2 != v[i] ) {\n cnt1 = 1;\n el1 = v[i];\n }\n else if ( cnt2 == 0 && el1 != v[i] ) {\n cnt2 = 1;\n el2 = v[i];\n }\n else if ( v[i] == el1 ) cnt1++;\n else if ( v[i] == el2 ) cnt2++;\n else {\n cnt1--, cnt2--;\n }\n }\n\n vector<int> ls;\n\n \n cnt1 = 0, cnt2 = 0;\n for ( int i = 0; i < n; i++ ) {\n if ( v[i] == el1 ) cnt1++;\n if ( v[i] == el2 ) cnt2++;\n }\n\n int mini = int(n / 3) + 1;\n if ( cnt1 >= mini ) ls.push_back(el1);\n if ( cnt2 >= mini ) ls.push_back(el2);\n\n\n return ls;\n}\n};",
"memory": "19200"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int lim=nums.size()/3;int mi=0;vector<int>ans;\n unordered_set<int>st;\n nums.push_back(INT_MAX);\n sort(nums.begin(),nums.end());\n for(int i=0;i<nums.size()-1;i++){\n \n if(nums[i]==nums[i+1]){mi++;if(mi>=lim and st.find(nums[i])==st.end()){ans.push_back(nums[i]);st.insert(nums[i]);}}\n else{mi=0;}\n if(mi>=lim and st.find(nums[i])==st.end()){ans.push_back(nums[i]);st.insert(nums[i]);}\n }\n return ans;\n\n }\n};",
"memory": "19200"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int> v) { \n\t int cnt1 = 0; int cnt2 = 0; \n\t int ele1 = INT_MIN; int ele2 = INT_MIN; \n\t int n = v.size();\n\t for(int i = 0;i<n;i++){\n\t\t if(cnt1 == 0 && ele2 != v[i]){\n\t\t\t\t cnt1 = 1; ele1 = v[i]; } \n\t\t else if(cnt2 == 0 && ele1 != v[i]){\n\t\t\t\t cnt2 = 1; ele2 = v[i]; }\n\t\t else if(v[i] == ele1) cnt1++;\n\t\t else if(v[i] == ele2) cnt2++;\n\t\t else { cnt1--; cnt2--; } \n\t\t }\n\t\t vector<int> ans;\n\t\t\t cnt1 = 0,cnt2 = 0;\n\t\t\t for(int i = 0;i<n;i++){\n\t\t\t\t if (v[i] == ele1) cnt1++;\n\t\t\t\t if (v[i] == ele2) cnt2++;\n\t\t\t\t\t } \n\t\t\tint mini = (n/3) + 1;\n if (cnt1 >= mini) {\n ans.push_back(ele1);\n }\n if (cnt2 >= mini) {\n ans.push_back(ele2);\n }\n return ans;\n\t\t\t\t\t \n }\n};",
"memory": "19300"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int n = nums.size();\n sort(nums.begin(),nums.end());\n int i=0;\n vector<int> ans;\n while(i<n){\n int count = 1;\n int j = i+1;\n while(j<n && nums[i]==nums[j]){\n j++;\n count++;\n }\n if(count>n/3) ans.push_back(nums[i]);\n i=j;\n }\n return ans;\n }\n};",
"memory": "19300"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n vector<int> ans;\n int x=nums.size()/3+1;\n map<int,int> count;\n for(int i=0;i<nums.size();i++){\n count[nums[i]]++;\n if(count[nums[i]]==x){\n ans.push_back(nums[i]);\n }\n if(ans.size()==2){\n break;\n }\n }\n sort(ans.begin(),ans.end());\n return ans;\n // int count=0;\n // for(int i=0;i<nums.size()-1;i++){\n // int tmp=nums[i];\n // if(count>x){\n // ans.push_back(nums[i]);\n // }\n // if(tmp==nums[i+1]){\n // count++;\n // }\n // else count=0;\n // }\n // return ans;\n }\n};",
"memory": "19400"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) \n {\n vector <int> v;\n for(int i=0;i<nums.size();i++)\n {\n int count=0;\n \n for(int j=0;j<nums.size();j++)\n {\n if(nums[i]==nums[j])\n {\n count++;\n }\n }\n if(count>(nums.size())/3)\n {\n \n v.push_back(nums[i]);\n \n }\n }\n for(int i=0;i<v.size();i++)\n {\n for(int j=i+1;j<v.size();j++)\n {\n if(v[i]==v[j])\n {\n v.erase(v.begin()+j);\n j--;\n }\n }\n }\n return v;\n \n }\n};",
"memory": "19500"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& a) {\n\n unordered_map<int, int> hash;\n vector<int> res;\n for (int i = 0; i < a.size(); i++) {\n hash[a[i]]++;\n }\n int n=(a.size() / 3);\n // cout<<\"n:\"<<n<<endl;\n for (int i = 0; i < a.size(); i++) {\n // cout<<\"hash[a[\"<<i<<\"]]: \"<<hash[a[i]]<<endl;\n if (hash[a[i]] > n)\n {\n res.push_back(a[i]);\n hash[a[i]]=0;\n }\n }\n return res;\n }\n};",
"memory": "19600"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n unordered_map<int,int> mp;\n set<int> ans;\n for(auto it:nums)\n {\n mp[it]++;\n }\n for(auto it:nums)\n {\n if(mp[it]>(nums.size()/3))\n ans.insert(it);\n }\n vector<int> rans(ans.begin(),ans.end());\n return rans;\n }\n};",
"memory": "19700"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n int n=nums.size();\n vector<int> res;\n unordered_map<int, int> freq;\n for(int i=0; i<n; i++){\n freq[nums[i]]++;\n }\n for(auto item: freq){\n if(item.second>n/3){\n res.push_back(item.first);\n }\n }\n return res; \n }\n};",
"memory": "19700"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n unordered_map<int,int> fr;\n vector<int> ans;\n int n = nums.size();\n for(int num:nums){\n fr[num]++;\n if(fr[num] > n/3){\n ans.push_back(num);\n fr[num] = INT_MIN;\n }\n }\n return ans;\n }\n};",
"memory": "19800"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n vector<int>ans;\n unordered_map<int,int>mpp;\n int min = nums.size()/3+1;\n for(int i=0;i<nums.size();i++){\n mpp[nums[i]]++;\n if(mpp[nums[i]]==min){\n ans.push_back(nums[i]);\n }\n }\n return ans;\n \n }\n};",
"memory": "19800"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n vector<int>ans;\n unordered_map<int,int>mpp;\n int min = nums.size()/3+1;\n for(int i=0;i<nums.size();i++){\n mpp[nums[i]]++;\n if(mpp[nums[i]]==min){\n ans.push_back(nums[i]);\n }\n if(ans.size()==2)\n {\n break;\n }\n }\n return ans;\n \n }\n};",
"memory": "19900"
} |
229 | <p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>⌊ n/3 ⌋</code> times.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,3]
<strong>Output:</strong> [3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1]
<strong>Output:</strong> [1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> [1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve the problem in linear time and in <code>O(1)</code> space?</p>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> majorityElement(vector<int>& nums) {\n unordered_map<int,int>mpp;\n int n = nums.size();\n vector<int>ans;\n int mini= n/3 +1;\n for(int i = 0; i < n ; i++ ){\n mpp[nums[i]]++;\n if( mpp[ nums[i] ] == mini ){\n ans.push_back(nums[i]);\n }\n if(ans.size()>=2)break;\n } return ans;\n\n\n }\n};",
"memory": "19900"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int vali;\n int kthSmallest(TreeNode* root, int k) {\n if (root == NULL)\n return 0;\n int v = 0;\n findk(root, v, k);\n root->left = root->right = NULL;\n return vali;\n }\n void findk(TreeNode* root, int& v, int k) {\n\n if (root == NULL)\n return;\n findk(root->left, v, k);\n v++;\n if (v == k) {\n vali = root->val;\n return;\n }\n findk(root->right, v, k);\n }\n};\nauto init = []()\n{\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();",
"memory": "16600"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int vali;\n int kthSmallest(TreeNode* root, int k) {\n if (root == NULL)\n return 0;\n int v = 0;\n findk(root, v, k, 0);\n root->left=root->right=NULL;\n return vali;\n }\n void findk(TreeNode* root, int &v, int k, int j) {\n if (j == 0) {\n if (root == NULL)\n return;\n findk(root->left, v, k,j);\n v++;\n if (v == k) {\n vali = root->val;\n root = NULL;\n j = 1;\n }\n if (root)\n // v.push_back(root->val);\n findk(root->right, v, k,j);\n }\n }\n};\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();",
"memory": "16700"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int vali;\n int kthSmallest(TreeNode* root, int k) {\n if (root == NULL)\n return 0;\n int v = 0;\n findk(root, v, k);\n root->left = root->right = NULL;\n return vali;\n }\n void findk(TreeNode* root, int& v, int k) {\n\n if (root == NULL)\n return;\n findk(root->left, v, k);\n v++;\n if (v == k) {\n vali = root->val;\n return;\n }\n findk(root->right, v, k);\n }\n};\n/*auto init = []()\n{\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();*/",
"memory": "16700"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nstatic auto _ = []() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return true;\n}();\n\n#pragma -O3\nclass Solution {\npublic:\n int ans=-1;\n int k;\n void solve(TreeNode* root){\n if(!root){return;}\n solve(root->left);\n k--;\n if(k==0){\n ans=root->val;\n return;\n }\n solve(root->right);\n\n }\n int kthSmallest(TreeNode* root, int k) {\n if(!root){return -1;}\n this->k=k;\n solve(root);\n root->left=root->right=nullptr;\n return ans;\n }\n};",
"memory": "16800"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int kthSmallest(TreeNode* root, int k) {\n vector<TreeNode*> stack = {root};\n int ans;\n for(int i = 0; i < k; i++){\n while(true){\n TreeNode* cur = stack.back();\n if(cur->right){\n stack.insert(stack.end()-1,cur->right);\n cur->right = nullptr;\n };\n if(cur->left) {\n stack.push_back(cur->left);\n (*(stack.end()-2))->left = nullptr;\n }\n else break;\n }\n ans = stack.back()->val;\n stack.pop_back();\n }\n return ans;\n }\n};",
"memory": "16900"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int kthSmallest(TreeNode* root, int k) {\n vector<TreeNode*> stack = {root};\n int ans;\n for(int i = 0; i < k; i++){\n while(true){\n TreeNode* cur = stack.back();\n if(cur->right){\n stack.insert(stack.end()-1,cur->right);\n cur->right = nullptr;\n };\n if(cur->left) {\n stack.push_back(cur->left);\n (*(stack.end()-2))->left = nullptr;\n }\n else break;\n }\n ans = stack.back()->val;\n stack.pop_back();\n }\n return ans;\n }\n};",
"memory": "17000"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int kthSmallest(TreeNode* root, int k) {\n vector<TreeNode*> stack = {root};\n int ans;\n for(int i = 0; i < k; i++){\n while(true){\n TreeNode* cur = stack.back();\n if(cur->right){\n stack.insert(stack.end()-1,cur->right);\n cur->right = nullptr;\n };\n if(cur->left) {\n stack.push_back(cur->left);\n (*(stack.end()-2))->left = nullptr;\n }\n else break;\n }\n ans = stack.back()->val;\n stack.pop_back();\n }\n return ans;\n }\n};",
"memory": "17000"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int kthSmallest(TreeNode* root, int k) {\n vector<int>ans;\n TreeNode* curr=root;\n while(curr){\n if(!curr->left){\n ans.push_back(curr->val);\n curr=curr->right;\n\n }\n else{\n\n TreeNode* leftChild=curr->left;\n\n while(leftChild->right){\n leftChild=leftChild->right;\n }\n\n leftChild->right=curr;\n TreeNode* temp=curr;\n curr=curr->left;\n temp->left=NULL;\n }\n }\n return ans[k-1];\n }\n};",
"memory": "17100"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int kthSmallest(TreeNode* root, int k) {\n // Here we will do morris tarversal so that space can be saved.\n // inorder traversal as elemnts are in asecnding order\n \n vector<int> ans;\n TreeNode* curr =root;\n while(curr!=NULL){\n \n if(curr->left==NULL){\n ans.push_back(curr->val);\n curr = curr->right;\n }\n else{\n TreeNode* temp = curr->left;\n while(temp->right != NULL){\n temp=temp->right;\n }\n // left ko delete karna hai \n \n temp->right=curr;\n \n TreeNode* x = curr;\n curr = curr->left;\n x->left=NULL;\n }\n }\n \n return ans[k-1];\n \n }\n};",
"memory": "17200"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int kthSmallest(TreeNode* root, int k) {\n // Here we will do morris tarversal so that space can be saved.\n // inorder traversal as elemnts are in asecnding order\n \n vector<int> v;\n TreeNode* curr=root;\n while(curr!=NULL){\n \n if(curr->left==NULL){\n v.push_back(curr->val);\n curr=curr->right;\n }\n else{\n TreeNode* temp = curr->left;\n while(temp->right != NULL){\n temp=temp->right;\n }\n \n \n temp->right=curr;\n temp=curr;\n curr=curr->left;\n temp->left=NULL;\n }\n }\n \n return v[k-1];\n }\n};\n\n\n ",
"memory": "17200"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int kthSmallest(TreeNode* root, int k) {\n vector<int>ans;\n TreeNode* curr=root;\n while(curr){\n if(!curr->left){\n ans.push_back(curr->val);\n curr=curr->right;\n\n }\n else{\n\n TreeNode* leftChild=curr->left;\n\n while(leftChild->right){\n leftChild=leftChild->right;\n }\n\n leftChild->right=curr;\n TreeNode* temp=curr;\n curr=curr->left;\n temp->left=NULL;\n }\n }\n return ans[k-1];\n }\n};",
"memory": "17300"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* findpred(TreeNode* curr){\n TreeNode* pred = curr;\n pred = pred -> left;\n while(pred -> right != NULL){\n pred = pred -> right;\n } \n return pred;\n }\n void solve(TreeNode* &root){\n if(root == NULL || (root -> left == NULL && root -> right == NULL)){\n return;\n }\n TreeNode* curr = root;\n TreeNode* prev = root;\n bool firstleftnull = false;\n TreeNode* rightexist = NULL;\n while(curr != NULL){\n if(curr -> left != NULL){\n TreeNode* pred = findpred(curr);\n if(pred -> right == NULL){\n pred -> right = curr;\n curr = curr -> left;\n prev -> left = NULL;\n prev = curr;\n }\n else{\n curr = curr -> left;\n prev = curr;\n }\n }\n else{\n if(firstleftnull == false){\n root = curr;\n firstleftnull = true;\n }\n if(rightexist){\n rightexist -> right = curr;\n }\n rightexist = curr;\n curr = curr -> right;\n prev = curr;\n }\n }\n }\n int kthSmallest(TreeNode* root, int k) {\n solve(root);\n k = k-1;\n while(k--){\n root = root -> right;\n }\n return root -> val;\n }\n};",
"memory": "17500"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> a;\n void bb(TreeNode* root){\n if(!root) return;\n bb(root->left);\n a.push_back(root->val);\n bb(root->right);\n \n root->left=NULL;\n root->right=NULL;\n\n }\n int kthSmallest(TreeNode* root, int k) {\n bb(root);\n // int n = a.size();\n // for(int i=0;i<n;i++) cout<<a[i];\n return a[k-1];\n }\n};",
"memory": "18200"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "class Solution {\npublic:\n int kthSmallest(TreeNode* root, int k) {\n int cnt = 0;\n return solve(root, cnt, k);\n }\n \n int solve(TreeNode* root, int &cnt, int k) {\n if (root == NULL) {\n return -1;\n }\n int left = solve(root->left, cnt, k);\n if (left != -1) {\n return left;\n }\n cnt++;\n if (cnt == k) {\n return root->val;\n }\n return solve(root->right, cnt, k);\n }\n};",
"memory": "22400"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int kthSmallest(TreeNode* root, int k) {\n int cnt = 0; \n int ans;\n solve(root, cnt, ans, k);\n return ans;\n }\n void solve(TreeNode* root, int &cnt, int &ans, int k){\n if(root == NULL) return;\n //left, root, right \n solve(root->left, cnt, ans, k);\n cnt++;\n if(cnt == k){\n ans = root->val;\n return;\n }\n solve(root->right, cnt, ans, k);\n }\n};",
"memory": "22500"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int sol(TreeNode* root,int& k){\n //INORDER (left root right)\n if(root == NULL) return -1;\n\n int l = sol(root->left, k);\n if(l != -1) return l; //If left subtree found the kth smallest, return it\n\n //Visit curr node\n if(--k == 0) return root->val;\n\n return sol(root->right, k);\n }\n int kthSmallest(TreeNode* root, int k) {\n return sol(root, k);\n }\n};",
"memory": "22600"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int ans=0;\n void inorderTraversal(TreeNode* root,int& k){\n if(!root)\n return;\n inorderTraversal(root->left,k);\n k--;\n if(k==0){\n ans = root->val;\n }\n inorderTraversal(root->right,k);\n }\n int kthSmallest(TreeNode* root, int k) {\n inorderTraversal(root,k);\n return ans;\n }\n};",
"memory": "22600"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void recursion(TreeNode * root,int &k,int & count,int &ans){\n\n if(root->left != nullptr) recursion(root->left,k,count,ans);\n count++;\n if(count ==k) ans = root->val;\n if(root->right != nullptr) recursion(root->right,k,count,ans);\n \n }\n int kthSmallest(TreeNode* root, int k) {\n int count = 0;\n int ans=-1;\n recursion(root,k,count,ans);\n return ans;\n }\n};",
"memory": "22700"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "class Solution {\npublic:\n void util(TreeNode *root,int ¤tCount, int &ans,int k)\n {\n if(!root)return;\n util(root->left,currentCount,ans,k);\n currentCount++;\n \n if(currentCount == k){\n \n ans = root->val;\n return;\n }\n util(root->right,currentCount,ans,k);\n \n }\n int kthSmallest(TreeNode* root, int k) {\n \n int currentCount = 0;\n int ans ;\n util(root,currentCount,ans,k);\n \n return ans;\n \n }\n};",
"memory": "22700"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, vector<int> &v){\n if(root==NULL) return;\n inorder(root->left,v);\n v.push_back(root->val);\n inorder(root->right,v);\n }\n void ksmall(TreeNode* root, int &count, int &ans,int k){\n if(root==NULL) return;\n ksmall(root->left,count,ans,k);\n count+=1;\n if(count == k) ans = root->val;\n ksmall(root->right,count,ans,k);\n return;\n }\n int kthSmallest(TreeNode* root, int k) {\n int ans = 0;\n int count = 0;\n ksmall(root,count,ans,k);\n return ans;\n }\n};",
"memory": "22800"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int kthSmallest(TreeNode* root, int k) {\n stack<TreeNode*> st;\n TreeNode* cur = root;\n while(cur || !st.empty()){\n\n while(cur){\n st.push(cur);\n cur=cur->left;\n }\n cur = st.top();st.pop();\n k--;\n if(k==0)return cur->val;\n cur = cur->right;\n }\n return -1;\n }\n};",
"memory": "22800"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 1 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode *root, int &k, int &x) {\n if(root==NULL || k==0) return;\n inorder(root->left, k, x);\n k--;\n if(k==0) {\n x=root->val; return;\n }\n inorder(root->right, k, x);\n }\n int kthSmallest(TreeNode* root, int k) {\n int x=-1;\n inorder(root, k, x);\n return x;\n }\n};",
"memory": "22900"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 1 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int kthSmallest(TreeNode* root, int k) {\n stack<TreeNode*> s;\n TreeNode* node=root;\n\n int i=1;\n long long int x=LLONG_MAX;\n\n while(s.size()>0 or node){\n if(node){\n s.push(node);\n node=node->left;\n }\n\n else{\n TreeNode* temp=s.top();\n s.pop();\n if(i==k) return temp->val;\n if(x!=LLONG_MAX){\n x=temp->val;\n }\n i++;\n node=temp->right;\n }\n \n }\n return -1;\n }\n};",
"memory": "22900"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void inorder(TreeNode* root, vector<int>& result){\n if(!root) return;\n\n inorder(root->left, result);\n result.push_back(root->val);\n inorder(root->right, result);\n }\n\n int kthSmallest(TreeNode* root, int k) {\n vector<int> result;\n inorder(root, result);\n return result[k-1];\n }\n};",
"memory": "23000"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int helper(TreeNode* root, int k,int &c,int &ans){\n if(!root) return 0;\n helper(root->left,k,c,ans);\n c++;\n if(k==c) {\n ans=root->val;\n return ans;\n }\n helper(root->right,k,c,ans);\n return ans;\n \n }\n int kthSmallest(TreeNode* root, int k) {\n int ans=0;\n int c=0;\n helper(root,k,c,ans);\n return ans;\n }\n};",
"memory": "23000"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> ans;\n void inOrder(TreeNode* root) {\n if(!root)return;\n if(root->left) inOrder(root->left);\n ans.push_back(root->val);\n if(root->right) inOrder(root->right);\n }\n\n int kthSmallest(TreeNode* root, int k) {\n inOrder(root);\n return ans[k-1];\n }\n};",
"memory": "23100"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inOrderTraversal(TreeNode* root, std::vector<int>& vec)\n {\n if (!root)\n return;\n \n inOrderTraversal(root->left, vec);\n vec.push_back(root->val);\n inOrderTraversal(root->right, vec);\n }\n\n int kthSmallest(TreeNode* root, int k)\n {\n if (!root || k < 0)\n return -1;\n \n std::vector<int> vec;\n inOrderTraversal(root, vec);\n\n if (vec.size() >= k)\n {\n return vec[k-1];\n }\n\n return -1;\n }\n};",
"memory": "23100"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void Inorder_Traversal(TreeNode* root,vector<int>&S,int& k){\n if(root==NULL)\n return ;\n if(S.size()==k) return;\n Inorder_Traversal(root->left,S,k);\n S.push_back(root->val);\n Inorder_Traversal(root->right,S,k);\n \n }\n int kthSmallest(TreeNode* root, int k) {\n \n vector<int>v;\n Inorder_Traversal(root,v,k);\n return v[k-1];\n \n }\n};",
"memory": "23200"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root,vector<int> &v){\n if(!root){\n return;\n }\n\n inorder(root->left,v);\n v.push_back(root->val);\n inorder(root->right,v);\n }\n\n int kthSmallest(TreeNode* root, int k) {\n \n vector<int> node; \n inorder(root,node);\n return node[k-1];\n }\n};",
"memory": "23200"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nums;\n\n void solve(TreeNode* root){\n\n if(root == NULL) return ;\n nums.push_back(root->val);\n\n solve(root->left);\n solve(root->right);\n }\n\n int kthSmallest(TreeNode* root, int k) {\n \n solve(root);\n sort(nums.begin(), nums.end());\n return nums[k-1];\n }\n};",
"memory": "23300"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorderTraversal(TreeNode* root, vector<int> &inorder) {\n if (!root)\n return;\n inorderTraversal(root->left, inorder);\n inorder.push_back(root->val);\n inorderTraversal(root->right, inorder);\n }\n int kthSmallest(TreeNode* root, int k) {\n vector<int> inorder;\n inorderTraversal(root, inorder);\n\n return inorder[k-1];\n }\n};",
"memory": "23300"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n void inorder(TreeNode* root, vector<int>& v){\n if(!root) return;\n\n if(root -> left) inorder(root -> left, v);\n v.push_back(root -> val);\n if(root -> right) inorder(root -> right, v);\n }\npublic:\n int kthSmallest(TreeNode* root, int k) {\n if(!root) return 0;\n\n vector<int> v;\n inorder(root, v);\n sort(v.begin(), v.end());\n\n return v[k-1];\n }\n};",
"memory": "23400"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n struct Comparator {\n bool operator()(int a, int b) {\n return a < b; // ascending order\n }\n };\npublic:\n int kthSmallest(TreeNode* root, int k) {\n priority_queue<int, vector<int>, Comparator> pq;\n queue<TreeNode*> q;\n q.emplace(root);\n while (q.size() != 0) {\n auto node = q.front();\n q.pop();\n if(pq.size() < k) {\n pq.push(node->val);\n cout << \"push val \" << node->val << \"\\n\";\n } else {\n if (node->val < pq.top()){\n cout << \"pop val \" << pq.top() << \"\\n\";\n pq.pop();\n cout << \"push val \" << node->val << \"\\n\";\n pq.push(node->val);\n }\n }\n if (node->left != nullptr) {\n q.emplace(node->left);\n }\n if (node->right != nullptr) {\n q.emplace(node->right);\n }\n }\n return pq.top();\n\n }\n};",
"memory": "23500"
} |
230 | <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void processNode(TreeNode* root, vector<int>&ans)\n {\n ans.push_back(root->val);\n }\n void inorder(TreeNode* root, vector<int>&ans)\n {\n if(root->left!=NULL)\n {\n inorder(root->left,ans);\n }\n processNode(root, ans);\n \n if(root->right!=NULL)\n {\n inorder(root->right,ans);\n }\n\n }\n int kthSmallest(TreeNode* root, int k) \n {\n\n \n TreeNode* curr = root;\n TreeNode* prev = NULL;\n stack<TreeNode*>s;\n s.push(root);\n vector<int>ans;\n\n while(true) \n {\n if(curr!=NULL)\n {\n s.push(curr);\n curr = curr->left;\n }\n else\n {\n if(s.empty()){break;}\n curr = s.top();\n ans.push_back(curr->val);\n s.pop();\n curr = curr->right;\n \n }\n\n }\n // vector<int>ans;\n // inorder(root, ans);\n\n return ans[k-1];\n \n }\n};",
"memory": "23500"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "vector<string> mitoa;\nunordered_map<string, int> matoi;\nbool myCmp(int a, int b) { return mitoa[a] > mitoa[b]; }\n\nclass Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n mitoa.clear();\n matoi.clear();\n matoi[\"JFK\"] = 0;\n mitoa.push_back(\"JFK\");\n vector<vector<int>> g(1);\n for(const auto& ticket: tickets)\n {\n int u, v;\n if(matoi.find(ticket[0]) == matoi.end())\n {\n matoi[ticket[0]] = u = mitoa.size();\n mitoa.push_back(ticket[0]);\n g.push_back(vector<int>());\n }\n else\n u = matoi[ticket[0]];\n\n if(matoi.find(ticket[1]) == matoi.end())\n {\n matoi[ticket[1]] = v = mitoa.size();\n mitoa.push_back(ticket[1]);\n g.push_back(vector<int>());\n }\n else\n v = matoi[ticket[1]];\n\n g[u].push_back(v);\n\n }\n\n for(auto & adjList: g)\n sort(adjList.begin(), adjList.end(), myCmp);\n\n stack<int> st;\n stack<int> path;\n\n int start = 0;\n\n st.push(start);\n while(st.size())\n {\n int curr = st.top();\n if(g[curr].size())\n {\n st.push(g[curr].back());\n g[curr].pop_back();\n }\n else\n {\n path.push(curr);\n st.pop();\n }\n }\n\n vector<string> ans(tickets.size() + 1);\n int i = 0;\n while(path.size())\n {\n start = path.top();\n path.pop();\n ans[i++] = mitoa[start];\n }\n\n return ans;\n }\n};",
"memory": "16700"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\r\npublic:\r\n vector<string> findItinerary(vector<vector<string>>& tickets) {\r\n std::unordered_map<std::string, std::map<std::string, int>> adjs;\r\n for (auto& ticket : tickets) {\r\n ++adjs[ticket[0]][ticket[1]];\r\n }\r\n std::vector<std::string> res;\r\n res.reserve(tickets.size() + 1);\r\n res.push_back(\"JFK\");\r\n dfs(tickets.size() + 1, adjs, res);\r\n return res;\r\n }\r\n\r\n bool dfs(int target,\r\n std::unordered_map<std::string, std::map<std::string, int>>& adjs,\r\n std::vector<std::string>& res) {\r\n if (res.size() == target) {\r\n return true;\r\n }\r\n\r\n auto& cur = res.back();\r\n for (auto& [adj, cnt] : adjs[cur]) {\r\n if (cnt > 0) {\r\n --cnt;\r\n res.push_back(adj);\r\n if (dfs(target, adjs, res)) {\r\n return true;\r\n }\r\n res.pop_back();\r\n ++cnt;\r\n }\r\n }\r\n\r\n return false;\r\n }\r\n};",
"memory": "17000"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n int n = tickets.size();\n std::vector<std::string> ans(n + 1);\n std::unordered_map<std::string, std::multiset<std::string, std::greater<>>> adj;\n for (int i = 0; i < n; ++i) adj[tickets[i][0]].insert(tickets[i][1]);\n std::stack<std::string> stk;\n stk.push(\"JFK\");\n std::string tmp;\n while (!stk.empty()) {\n tmp = stk.top();\n if (adj[tmp].empty()) {\n ans[n--] = tmp;\n stk.pop();\n }\n else {\n stk.emplace(*(--adj[tmp].end()));\n adj[tmp].erase(--adj[tmp].end());\n }\n }\n return ans;\n }\n};",
"memory": "17200"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n auto m = std::unordered_map<std::string_view,\n std::multiset<std::string_view>>{};\n for (auto& ticket : tickets) {\n m[ticket[0]].insert(ticket[1]);\n }\n auto itinerary = std::deque<std::string_view>{};\n auto stack = std::stack<std::string_view>{};\n stack.push(\"JFK\");\n while (!stack.empty()) {\n while (true) {\n auto& to = m[stack.top()];\n if (to.empty()) {\n break;\n }\n auto it = to.begin();\n stack.push(*it);\n to.erase(it);\n }\n itinerary.push_back(stack.top());\n stack.pop();\n }\n return {itinerary.rbegin(), itinerary.rend()};\n }\n};",
"memory": "17300"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "#include <vector>\n#include <string>\n#include <unordered_map>\n#include <map>\n#include <deque>\n\nusing namespace std;\n\nclass Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n unordered_map<string, map<string, int>> graph;\n vector<string> result;\n\n for (const auto& ticket : tickets) {\n graph[ticket[0]][ticket[1]]++;\n }\n\n dfs(\"JFK\", graph, result, tickets.size() + 1);\n\n return result;\n }\n\nprivate:\n bool dfs(const string& airport, unordered_map<string, map<string, int>>& graph, vector<string>& result, int totalTickets) {\n result.push_back(airport);\n\n if (result.size() == totalTickets) {\n return true;\n }\n\n if (graph.find(airport) != graph.end()) {\n for (auto& [next, count] : graph[airport]) {\n if (count > 0) {\n graph[airport][next]--;\n if (dfs(next, graph, result, totalTickets)) {\n return true;\n }\n graph[airport][next]++;\n }\n }\n }\n\n result.pop_back();\n return false;\n }\n};\n",
"memory": "17400"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n auto m = std::unordered_map<std::string_view,\n std::multiset<std::string_view>>{};\n for (auto& ticket : tickets) {\n m[ticket[0]].insert(ticket[1]);\n }\n auto itinerary = std::vector<std::string_view>{};\n auto stack = std::stack<std::string_view>{};\n stack.push(\"JFK\");\n while (!stack.empty()) {\n while (true) {\n auto& to = m[stack.top()];\n if (to.empty()) {\n break;\n }\n auto it = to.begin();\n stack.push(*it);\n to.erase(it);\n }\n itinerary.push_back(stack.top());\n stack.pop();\n }\n return {itinerary.rbegin(), itinerary.rend()};\n }\n};",
"memory": "17500"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n unordered_map<string, map<string, int>> targets;\n\n bool backtracking (int ticketnum, vector<string>& result) {\n if (result.size() == ticketnum + 1) {\n return true;\n }\n\n for (pair<const string, int>& target : targets[result[result.size() - 1]]) {\n if (target.second > 0) {\n result.push_back(target.first);\n target.second--;\n if (backtracking (ticketnum, result)) return true;\n //undo the choice\n result.pop_back();\n target.second++;\n }\n }\n return false;\n }\n\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n targets.clear();\n vector<string> result;\n for (const vector<string>& vec : tickets) {\n targets [vec[0]][vec[1]]++;\n }\n result.push_back(\"JFK\");\n backtracking(tickets.size(), result);\n return result;\n }\n};",
"memory": "17600"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n int t = tickets.size();\n unordered_map<string, map<string, int>> adj;\n vector<bool> used(t, false);\n\n \n for(int i = 0; i < t; ++i){\n auto& v = tickets[i];\n adj[v[0]][v[1]]++;;\n }\n\n // for(auto it = adj.begin(); it != adj.end(); ++it){\n // sort(it->second.begin(), it->second.end());\n // }\n\n vector<string> path;\n path.reserve(t);\n search(\"JFK\", t, 0, path, adj);\n return path;\n }\n\n bool search(string here, int t, int u, vector<string>& path, unordered_map<string, map<string, int>>& adj) {\n path.push_back(here);\n\n if(u == t) {\n return true;\n }\n\n auto& mp = adj[here];\n\n for(auto it = mp.begin(); it != mp.end(); ++it) {\n if(it->second > 0) {\n --mp[it->first];\n if (search(it->first, t, u+1, path, adj)) return true;\n ++mp[it->first];\n }\n \n }\n\n path.pop_back();\n return false;\n }\n};",
"memory": "17600"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n unordered_map<string, map<string, int>> m;\n bool backtracking(int sz, vector<string>& res)\n {\n if (sz + 1 == res.size())\n {\n return true;\n }\n\n for (auto && [addr, count] : m[res.back()])\n {\n if (count > 0)\n {\n res.push_back(addr);\n --count;\n if (backtracking(sz, res))\n {\n return true;\n }\n\n res.pop_back();\n ++count;\n \n }\n \n }\n \n return false;\n }\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets)\n {\n vector<string> res;\n for (auto& vec : tickets)\n {\n ++m[vec[0]][vec[1]];\n }\n \n res.push_back(\"JFK\");\n backtracking(tickets.size(), res);\n return res;\n }\n};",
"memory": "17700"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\n unordered_map<string, vector<string>>graph;\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n int n = tickets.size();\n if(!n)\n return {};\n for(const auto&t:tickets){\n graph[t[0]].push_back(t[1]);\n }\n for(auto&[_, row]:graph){\n sort(row.begin(), row.end(), greater<string>());\n }\n vector<string>ret;\n dfs(\"JFK\", ret);\n reverse(ret.begin(), ret.end());\n return ret;\n }\n \n \n void dfs(const string& cur, vector<string>&ret){\n while(graph[cur].size()){\n auto nex = graph[cur].back();\n graph[cur].pop_back();\n dfs(nex, ret);\n }\n ret.push_back(cur);\n }\n};",
"memory": "17800"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n \n unordered_map<string,multiset<string>> flightMap;\n vector<string> res;\n\n for(const auto& ticket: tickets){\n flightMap[ticket[0]].insert(ticket[1]);\n }\n\n dfsTraversal(\"JFK\",flightMap,res);\n\n reverse(res.begin(),res.end());\n return res;\n }\n\n void dfsTraversal(const string& current,unordered_map<string, multiset<string>>& flightMap,\n vector<string>& result) {\n auto& destinations =flightMap[current];\n\n while(!destinations.empty()){\n auto nextDest = *destinations.begin();\n destinations.erase(destinations.begin());\n dfsTraversal(nextDest,flightMap,result);\n\n }\n\n result.push_back(current);\n }\n\n};",
"memory": "17900"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n vector<string> ans;\n unordered_map<string, multiset<string>> graph;\n\n for (const vector<string>& ticket : tickets)\n graph[ticket[0]].insert(ticket[1]);\n\n dfs(graph, \"JFK\", ans);\n ranges::reverse(ans);\n return ans;\n }\n\n private:\n void dfs(unordered_map<string, multiset<string>>& graph, const string& u,\n vector<string>& ans) {\n while (graph.contains(u) && !graph[u].empty()) {\n const string v = *graph[u].begin();\n graph[u].erase(graph[u].begin());\n dfs(graph, v, ans);\n }\n ans.push_back(u);\n }\n};",
"memory": "18000"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void DFS(vector<string> &res, const string &start, unordered_map<string, multiset<string>> &map) {\n while ( map[start].size() ) {\n const auto next = *(map[start].begin());\n map[start].erase(map[start].begin());\n DFS(res, next, map);\n }\n\n res.emplace_back(start);\n }\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n unordered_map<string, multiset<string>> map;\n for ( const auto &pair : tickets ) {\n map[pair[0]].insert(pair[1]);\n }\n\n vector<string> res;\n DFS(res, \"JFK\", map);\n reverse(res.begin(), res.end());\n return res;\n }\n};",
"memory": "18100"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n vector<string> itinerary;\n unordered_map<string, vector<pair<string, bool>>> map;\n for (const vector<string>& ticket : tickets)\n map[ticket[0]].push_back(make_pair(ticket[1], false));\n for (auto& airport : map)\n sort(airport.second.begin(), airport.second.end());\n int total = tickets.size();\n int count = 0;\n stack<int> stack;\n itinerary.push_back(\"JFK\");\n while (count < total)\n {\n vector<pair<string, bool>>& airlines = map[itinerary.back()];\n auto first = find_if(begin(airlines), end(airlines), [](pair<string, bool> airline) { return !airline.second; });\n if (first == airlines.end())\n {\n bool found = false;\n while (!found)\n {\n itinerary.pop_back();\n --count;\n vector<pair<string, bool>>& prev = map[itinerary.back()];\n int top = stack.top();\n stack.pop();\n prev[top].second = false;\n for (int i = top + 1; i < prev.size() && !found; ++i)\n {\n if (!prev[i].second && prev[i].first != prev[top].first)\n {\n found = true;\n prev[i].second = true;\n itinerary.push_back(prev[i].first);\n ++count;\n stack.push(i);\n }\n }\n }\n continue;\n }\n (*first).second = true;\n itinerary.push_back((*first).first);\n ++count;\n stack.push(first - airlines.begin());\n }\n return itinerary;\n }\n};",
"memory": "18200"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n unordered_map<string, multiset<string>> mp;\n for(const auto& t : tickets) {\n mp[t[0]].insert(t[1]);\n }\n stack<string> s;\n s.push(\"JFK\");\n vector<string> ans;\n while(!s.empty()) {\n const string& cur = s.top();\n if(mp[cur].empty()){\n ans.push_back(cur);\n s.pop();\n } else {\n const string& next = *mp[cur].begin();\n s.push(next);\n mp[cur].erase(mp[cur].begin());\n }\n }\n reverse(ans.begin(), ans.end());\n return ans;\n }\n};",
"memory": "18200"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "\nclass Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n // Build the adjacency list\n map<string, multiset<string>> adj;\n for (const auto& ticket : tickets) {\n adj[ticket[0]].insert(ticket[1]);\n }\n\n vector<string> res;\n stack<string> stk;\n stk.push(\"JFK\");\n\n while (!stk.empty()) {\n string curr = stk.top();\n if (adj[curr].empty()) {\n res.push_back(curr);\n stk.pop();\n } else {\n stk.push(*adj[curr].begin());\n adj[curr].erase(adj[curr].begin());\n }\n }\n\n reverse(res.begin(), res.end());\n\n if (res.size() != tickets.size() + 1) {\n return {};\n }\n\n return res;\n }\n};\n",
"memory": "18300"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n using Map = unordered_map<string, multiset<string>>;\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n Map m;\n for(auto& ticket : tickets) {\n m[ticket[0]].insert(ticket[1]); // {\"JFK\" : [\"A\", \"B\", \"B\"]}\n }\n\n vector<string> route;\n std::string start{\"JFK\"};\n dfs(start, route, m);\n return vector<string>(route.rbegin(), route.rend());\n }\n\n void dfs(std::string& cur, vector<string>& route, Map& m) {\n while(m[cur].size()) {\n std::string next = *m[cur].begin();\n m[cur].erase(m[cur].begin());\n dfs(next, route, m);\n }\n route.push_back(cur);\n }\n};",
"memory": "18400"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>> tickets) {\n\n // find the unique nodes\n set<string> nodesUnique;\n for (auto &e: tickets){\n nodesUnique.insert(e[0]);\n nodesUnique.insert(e[1]);\n }\n\n // prepare mappings from str to integer IDs - so we can apply regular DFS\n vector<string> nodes2str(nodesUnique.size());\n int n = 0;\n for (auto &i : nodesUnique) nodes2str[n++] = i;\n map<string, int> str2nodes;\n for (int i = 0; i < n; i++) str2nodes[nodes2str[i]] = i;\n\n // prepare an integer adjacency list (with duplicate edges possible!).\n vector<vector<int>> adjlist(n, vector<int> ());\n vector<vector<bool>> visited(n, vector<bool> ());\n for (auto &e: tickets) adjlist[str2nodes[e[0]]].push_back(str2nodes[e[1]]);\n\n for (int i = 0; i < n; i++) {\n sort(begin(adjlist[i]), end(adjlist[i]));\n visited[i].resize(adjlist[i].size());\n fill(begin(visited[i]), end(visited[i]), false);\n }\n\n int pathlen = 0;\n vector<int> path(tickets.size() + 1);\n dfs(str2nodes[\"JFK\"], pathlen, path, adjlist, visited);\n\n pathlen = 0;\n vector<string> ans(tickets.size() + 1);\n for (int i : path) ans[pathlen++] = nodes2str[i];\n\n return ans;\n }\n\n bool dfs(int node, int &pathlen, vector<int> &path, vector<vector<int>> &adjlist,\n vector<vector<bool>> &visited) {\n\n // add node to the path\n path[pathlen++] = node;\n if (pathlen == path.size()) return true;\n\n for (int i = 0; i < adjlist[node].size(); i++) {\n if (!visited[node][i]) {\n visited[node][i] = true;\n if(dfs(adjlist[node][i], pathlen, path, adjlist, visited)) return true;\n else {\n int j = i + 1;\n while (j < adjlist[node].size() and adjlist[node][j] == adjlist[node][i]) j++;\n visited[node][i] = false;\n i = j - 1;\n }\n }\n }\n \n pathlen--;\n return false;\n }\n};\n\nauto init = [](){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();",
"memory": "18500"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>> tickets) {\n\n // find the unique nodes\n set<string> nodesUnique;\n for (auto &e: tickets){\n nodesUnique.insert(e[0]);\n nodesUnique.insert(e[1]);\n }\n\n // prepare mappings from str to integer IDs - so we can apply regular DFS\n vector<string> nodes2str(nodesUnique.size());\n int n = 0;\n for (auto &i : nodesUnique) nodes2str[n++] = i;\n map<string, int> str2nodes;\n for (int i = 0; i < n; i++) str2nodes[nodes2str[i]] = i;\n\n // prepare an integer adjacency list (with duplicate edges possible!).\n vector<vector<int>> adjlist(n, vector<int> ());\n vector<vector<bool>> visited(n, vector<bool> ());\n for (auto &e: tickets) adjlist[str2nodes[e[0]]].push_back(str2nodes[e[1]]);\n\n for (int i = 0; i < n; i++) {\n sort(begin(adjlist[i]), end(adjlist[i]));\n visited[i].resize(adjlist[i].size());\n fill(begin(visited[i]), end(visited[i]), false);\n }\n\n int pathlen = 0;\n vector<int> path(tickets.size() + 1);\n dfs(str2nodes[\"JFK\"], pathlen, path, adjlist, visited);\n\n pathlen = 0;\n vector<string> ans(tickets.size() + 1);\n for (int i : path) ans[pathlen++] = nodes2str[i];\n\n return ans;\n }\n\n bool dfs(int node, int &pathlen, vector<int> &path, vector<vector<int>> &adjlist,\n vector<vector<bool>> &visited) {\n\n // add node to the path\n path[pathlen++] = node;\n if (pathlen == path.size()) return true;\n\n for (int i = 0; i < adjlist[node].size(); i++) {\n if (!visited[node][i]) {\n visited[node][i] = true;\n if(dfs(adjlist[node][i], pathlen, path, adjlist, visited)) return true;\n else {\n int j = i + 1;\n while (j < adjlist[node].size() and adjlist[node][j] == adjlist[node][i]) j++;\n visited[node][i] = false;\n i = j - 1;\n }\n }\n }\n \n pathlen--;\n return false;\n }\n};\n\nauto init = [](){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();",
"memory": "18600"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\n unordered_map<string, vector<string>>graph;\n unordered_set<string>nodes;\n unordered_map<string,int>indegree, outdegree;\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n int n = tickets.size();\n if(!n)\n return {};\n for(const auto&t:tickets){\n graph[t[0]].push_back(t[1]);\n nodes.insert(t[0]);\n nodes.insert(t[1]);\n outdegree[t[0]]++;\n indegree[t[1]]++;\n }\n string start_point = \"JFK\";\n if(!validate(start_point) || start_point != \"JFK\")\n return {};\n for(auto&[_, row]:graph){\n sort(row.begin(), row.end(), greater<string>());\n }\n vector<string>ret;\n dfs(\"JFK\", ret);\n reverse(ret.begin(), ret.end());\n return ret;\n }\n \n \n void dfs(const string& cur, vector<string>&ret){\n while(graph[cur].size()){\n auto nex = graph[cur].back();\n graph[cur].pop_back();\n dfs(nex, ret);\n }\n ret.push_back(cur);\n }\n \n bool validate(string& start_point){\n int start = 0, end = 0;\n for(const auto&node:nodes){\n if(abs(indegree[node]-outdegree[node])>1)\n return false;\n if(indegree[node]>outdegree[node])\n end++;\n else if(indegree[node]<outdegree[node]){\n start_point = node;\n start++;\n }\n }\n return (!start&&!end)||(start == 1 && end == 1);\n }\n};",
"memory": "18700"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n void eulerianPath(\n unordered_map<string, vector<string>>& graph,\n unordered_map<string, int>& outDegree,\n string& from,\n stack<string>& itinerary\n ) {\n while (outDegree[from] != 0) {\n string to = *graph[from].begin();\n graph[from].erase(graph[from].begin());\n outDegree[from]--;\n eulerianPath(graph, outDegree, to, itinerary);\n }\n itinerary.push(from);\n }\n\npublic:\n // Eulerian path solution: https://leetcode.com/problems/reconstruct-itinerary/submissions/1343666189?envType=study-plan-v2&envId=google-spring-23-high-frequency\n\n // See https://www.youtube.com/watch?v=8MpoO2zA2l4 for Eulerian Path\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n unordered_map<string, vector<string> > graph;\n // unordered_map<string, int> inDegree;\n unordered_map<string, int> outDegree;\n\n for (int i = 0; i < tickets.size(); i++) {\n string from = tickets[i][0];\n string to = tickets[i][1];\n graph[from].push_back(to);\n // inDegree[to]++;\n outDegree[from]++;\n }\n\n for (auto& it: graph) {\n sort(it.second.begin(), it.second.end());\n }\n\n stack<string> reverseItinerary;\n string startNode = \"JFK\";\n eulerianPath(graph, outDegree, startNode, reverseItinerary);\n\n vector<string> itinerary;\n while (!reverseItinerary.empty()) {\n itinerary.push_back(reverseItinerary.top());\n reverseItinerary.pop();\n }\n return itinerary;\n }\n};",
"memory": "18800"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void dfs(unordered_map<string, priority_queue<string, vector<string>, greater<>>>& g, vector<string>& res, string pos) {\n while(!g[pos].empty()) {\n string t = g[pos].top();\n g[pos].pop();\n dfs(g, res, t);\n }\n res.push_back(pos);\n }\n\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n unordered_map<string, priority_queue<string, vector<string>, greater<>>> g;\n\n for(int i = 0; i < tickets.size(); ++i) {\n g[tickets[i][0]].push(tickets[i][1]);\n }\n\n vector<string> res;\n dfs(g, res, \"JFK\");\n\n reverse(res.begin(), res.end());\n return res;\n }\n};",
"memory": "18900"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n // Use a min-heap (priority_queue) for lexicographically sorting the destinations\n unordered_map<string, priority_queue<string, vector<string>, greater<string>>> graph;\n \n // Build the graph\n for (const auto& ticket : tickets) {\n graph[ticket[0]].push(ticket[1]);\n }\n \n vector<string> itinerary;\n dfs(\"JFK\", graph, itinerary);\n \n // The itinerary will be in reverse order, so reverse it before returning\n reverse(itinerary.begin(), itinerary.end());\n return itinerary;\n }\n\nprivate:\n void dfs(const string& airport, unordered_map<string, priority_queue<string, vector<string>, greater<string>>>& graph, vector<string>& itinerary) {\n auto& destinations = graph[airport];\n \n // Visit all the destinations using DFS (in lexicographical order automatically)\n while (!destinations.empty()) {\n string next_destination = destinations.top();\n destinations.pop(); // Remove this ticket (edge)\n dfs(next_destination, graph, itinerary);\n }\n \n // Add the current airport to the itinerary after visiting all its destinations\n itinerary.push_back(airport);\n }\n};\n",
"memory": "18900"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n unordered_map<string, priority_queue<string, vector<string>, greater<string>>> graph;\n vector<string> result;\n \n void dfs(string node) {\n auto& destinations = graph[node];\n while (!destinations.empty()) {\n string next = destinations.top();\n destinations.pop();\n dfs(next);\n }\n result.push_back(node); \n }\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n for (auto& ticket : tickets) {\n graph[ticket[0]].push(ticket[1]);\n }\n\n dfs(\"JFK\");\n \n reverse(result.begin(), result.end());\n return result;\n }\n};",
"memory": "19300"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n void dfs(std::string departure)\n {\n std::string arrival;\n while (graph[departure].size())\n {\n arrival = graph[departure][graph[departure].size() - 1];\n graph[departure].pop_back();\n dfs(arrival);\n }\n searched.insert(searched.begin(), departure);\n }\n vector<string> findItinerary(vector<vector<string>>& tickets)\n {\n for (const auto& ticket : tickets)\n graph[ticket[0]].push_back(ticket[1]);\n for (const auto& ticket : tickets)\n for (const auto& i : ticket)\n if (graph[i].size())\n std::sort(graph[i].begin(), graph[i].end(), std::greater<std::string>());\n dfs(\"JFK\");\n return searched;\n }\nprivate:\n std::map<std::string, std::vector<std::string>> graph;\n std::vector<std::string> searched;\n};",
"memory": "19300"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 1 | {
"code": "class Solution {\n void dfs(string curr, unordered_map<string, multiset<string>>& adjL, vector<string>& path){\n while(!adjL[curr].empty()){\n auto itr = adjL[curr].begin();\n\n string next = *itr;\n\n adjL[curr].erase(itr);\n\n dfs(next, adjL, path);\n }\n\n path.push_back(curr);\n }\n\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n unordered_map<string, multiset<string>> adjL;\n\n unordered_map<string, pair<int,int>> degree;\n\n for(auto& v: tickets){\n adjL[v[0]].insert(v[1]);\n\n degree[v[0]].second++;\n degree[v[1]].first++;\n }\n\n //check for degrees\n int outPos = 0, outNeg = 0;\n\n for(auto& [city, city_degree]: degree){\n int in = city_degree.first;\n int out = city_degree.second;\n\n if(in == out) continue;\n\n if(in - out >= 2 || out - in >= 2){\n return {};\n }\n\n if(in - out == 1) outNeg++;\n\n if(out - in == 1) outPos++;\n }\n\n if(outPos > 1 || outNeg > 1){\n return {};\n }\n\n vector<string> path;\n\n dfs(\"JFK\", adjL, path);\n\n reverse(path.begin(), path.end());\n\n return path;\n }\n};",
"memory": "19400"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 1 | {
"code": "class Solution {\nprivate:\n unordered_map<string, priority_queue<string, vector<string>, greater<string>>> flightMap;\n vector<string> itinerary;\n\n // Helper function for DFS\n void dfs(string airport) {\n // While there are still flights departing from the airport\n while (!flightMap[airport].empty()) {\n string nextAirport = flightMap[airport].top();\n flightMap[airport].pop();\n dfs(nextAirport);\n }\n itinerary.push_back(airport); // Add airport to the itinerary once all outgoing flights are used\n }\n \npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n // Construct the graph (flightMap)\n for (auto& ticket : tickets) {\n flightMap[ticket[0]].push(ticket[1]);\n }\n \n // Start DFS from \"JFK\"\n dfs(\"JFK\");\n \n // Since we append airports after visiting all their outgoing flights, the itinerary is constructed in reverse\n reverse(itinerary.begin(), itinerary.end());\n \n return itinerary;\n }\n};\n",
"memory": "19400"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 1 | {
"code": "class Solution {\nprivate:\n unordered_map<string, priority_queue<string, vector<string>, greater<string>>> flightMap;\n vector<string> itinerary;\n\n void dfs(string airport) {\n // While there are still flights departing from the airport\n while (!flightMap[airport].empty()) {\n string nextAirport = flightMap[airport].top();\n flightMap[airport].pop();\n dfs(nextAirport);\n }\n itinerary.push_back(airport); // Add airport to the itinerary once all outgoing flights are used\n }\n \npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n // Construct the graph (flightMap)\n for (auto& ticket : tickets) {\n flightMap[ticket[0]].push(ticket[1]);\n }\n \n // Start DFS from \"JFK\"\n dfs(\"JFK\");\n \n // Since we append airports after visiting all their outgoing flights, the itinerary is constructed in reverse\n reverse(itinerary.begin(), itinerary.end());\n \n return itinerary;\n }\n};\n",
"memory": "19500"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 1 | {
"code": "#include <unordered_map>\n\nclass Solution {\nprivate:\n std::unordered_map<std::string, std::multiset<std::string>> connections;\n std::vector<std::string> route; \npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n // create connection mapings\n for(auto v : tickets) {\n connections[v[0]].insert(v[1]);\n }\n\n // travel recursevily from JFK\n travel(\"JFK\");\n\n std::reverse(route.begin(),route.end());\n return route; \n }\n\n void travel(const std::string& startpoint) {\n\n while(!connections[startpoint].empty())\n {\n auto it = connections[startpoint].begin();\n std::string next_stop = *it;\n connections[startpoint].erase(it);\n travel(next_stop);\n }\n route.push_back(startpoint);\n }\n};",
"memory": "19500"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 1 | {
"code": "class Solution {\nprivate:\n void travel(unordered_map<string, vector<string>>& graph, string node, stack<string>& itinerary) {\n while (graph[node].size() != 0) {\n string to = graph[node].back();\n graph[node].pop_back();\n travel(graph, to, itinerary);\n }\n itinerary.push(node);\n }\n\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n unordered_map<string, vector<string>> graph;\n\n for (int i = 0; i < tickets.size(); i++) {\n graph[tickets[i][0]].push_back(tickets[i][1]);\n }\n\n for (auto &it: graph) {\n sort(it.second.begin(), it.second.end(), greater<string>());\n }\n\n stack<string> itineraryStack;\n vector<string> itinerary;\n travel(graph, \"JFK\", itineraryStack);\n\n while (!itineraryStack.empty()) {\n itinerary.push_back(itineraryStack.top());\n itineraryStack.pop();\n }\n\n return itinerary;\n }\n};",
"memory": "19600"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n vector<string> ans;\n unordered_map<string,multiset<string>> mp;\n void rec(string s) {\n\n while (!mp[s].empty()) {\n string t = *(mp[s].begin());\n mp[s].erase(mp[s].begin());\n rec(t);\n }\n\n ans.push_back(s);\n }\n\n\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n int n = tickets.size();\n int m = tickets[0].size();\n string s1,s2;\n for (int i = 0;i<n;i++) {\n s1 = tickets[i][0];\n s2 = tickets[i][1];\n mp[s1].insert(s2);\n }\n rec(\"JFK\");\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};",
"memory": "19600"
} |
332 | <p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p>
<p>All of the tickets belong to a man who departs from <code>"JFK"</code>, thus, the itinerary must begin with <code>"JFK"</code>. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.</p>
<ul>
<li>For example, the itinerary <code>["JFK", "LGA"]</code> has a smaller lexical order than <code>["JFK", "LGB"]</code>.</li>
</ul>
<p>You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>Input:</strong> tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>Output:</strong> ["JFK","MUC","LHR","SFO","SJC"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>Input:</strong> tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>Output:</strong> ["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>Explanation:</strong> Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tickets.length <= 300</code></li>
<li><code>tickets[i].length == 2</code></li>
<li><code>from<sub>i</sub>.length == 3</code></li>
<li><code>to<sub>i</sub>.length == 3</code></li>
<li><code>from<sub>i</sub></code> and <code>to<sub>i</sub></code> consist of uppercase English letters.</li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>
| 2 | {
"code": "\nostream& operator<<(ostream& os, const vector<string>& v) {\n cout << \"[\";\n for (int i = 0; i < v.size(); i++) {\n if (i != 0) {\n cout << \", \";\n }\n cout << v[i];\n }\n cout << \"]\";\n return os;\n}\n\nclass Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n map<string, map<string, int>> ticketsByOrigin = fillTicketsByOrigin(tickets);\n\n vector<string> itinerary;\n itinerary.push_back(\"JFK\");\n\n vector<string> lastTries;\n int currentDepth = 0;\n\n while (currentDepth < tickets.size()) {\n const string& currentPosition = itinerary.back();\n map<string, int>& possibleDestinations = ticketsByOrigin[currentPosition];\n\n cout << currentDepth << \" \" << itinerary.back() << \": \" << lastTries << endl;\n\n const auto& it = lastTries.size() == currentDepth + 1\n ? possibleDestinations.upper_bound(lastTries.back())\n : possibleDestinations.begin();\n\n if (lastTries.size() > currentDepth) {\n lastTries.pop_back();\n }\n\n if (it != possibleDestinations.end()) {\n const string& nextPosition = it->first;\n cout << \"+ \" << nextPosition << endl;\n\n itinerary.push_back(nextPosition);\n lastTries.push_back(nextPosition);\n\n if (it->second > 1) {\n it->second--;\n } else {\n possibleDestinations.erase(nextPosition);\n }\n currentDepth++;\n } else {\n cout << \"-\" << endl;\n string temp = currentPosition;\n itinerary.pop_back();\n ticketsByOrigin[itinerary.back()][temp]++;\n\n currentDepth--;\n }\n }\n\n return itinerary;\n }\n\n map<string, map<string, int>> fillTicketsByOrigin(const vector<vector<string>>& tickets) {\n map<string, map<string, int>> ticketsByOrigin;\n\n for (const vector<string>& ticket : tickets) {\n ticketsByOrigin[ticket[0]][ticket[1]]++;\n }\n\n return ticketsByOrigin;\n }\n\n};\n",
"memory": "19700"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.