id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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": "class 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 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\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 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" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n unordered_map<string, set<string>> routes;\n unordered_map<string, int> availableTickets;\n for(vector<string>& ticket: tickets){\n routes[ticket[0]].insert(ticket[1]);\n ++availableTickets[(ticket[0] + \"#\" + ticket[1])];\n }\n\n \n vector<string> ans;\n\n ans.push_back(\"JFK\");\n dfs(routes, \"JFK\", availableTickets, ans, tickets.size() + 1);\n \n return ans;\n }\n\n bool dfs(unordered_map<string, set<string>>& routes, string src, unordered_map<string, int>& availableTickets, vector<string>& ans, int target){\n if(ans.size() == target){\n return true;\n }\n\n set<string>& dests = routes[src];\n for(string dest: dests){\n string ticket = src + \"#\" + dest;\n if(availableTickets[ticket] == 0){\n continue;\n }\n\n ans.push_back(dest);\n --availableTickets[ticket];\n if(dfs(routes, dest, availableTickets, ans, target)){\n return true;\n }\n ans.pop_back();\n ++availableTickets[ticket];\n }\n\n return false;\n }\n};", "memory": "19800" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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": "class Solution {\npublic:\n\n struct hashing{\n size_t operator() (const std::pair<std::string, std::string> & a) const{\n return std::hash<std::string>()(a.first) ^ std::hash<std::string>()(a.second)<<1;\n }\n };\n\n bool dfsForMinLexi(std::string next, std::vector<std::string> &currentWay, int travelLength, \n std::unordered_map<std::pair<std::string, std::string>, int, hashing> & ticketsLeft,\n std::unordered_map<std::string, std::vector<std::string>> & connections){\n\n travelLength--;\n currentWay.push_back(next);\n if(travelLength==0){\n return true;\n }else{\n for(const auto & connection: connections[next]){\n if(ticketsLeft[{next, connection}]>0){\n ticketsLeft[{next, connection}]--;\n if(dfsForMinLexi(connection, currentWay, travelLength, ticketsLeft, connections)){\n return true;\n }\n ticketsLeft[{next, connection}]++;\n }\n }\n }\n currentWay.pop_back();\n return false;\n }\n\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n std::unordered_map<std::pair<std::string, std::string>, int, hashing> ticketsLeft;\n std::unordered_map<std::string, std::vector<std::string>> connections;\n\n for(const auto & ticket: tickets){\n ticketsLeft[{ticket[0], ticket[1]}] ++;\n if(ticketsLeft[{ticket[0], ticket[1]}] == 1){\n connections[ticket[0]].push_back(ticket[1]);\n }\n }\n\n for(auto & [key, c]: connections){\n sort(begin(c), end(c));\n }\n\n int travelLength = tickets.size()+1;\n std::vector<std::string> currentWay;\n dfsForMinLexi(\"JFK\", currentWay, travelLength, ticketsLeft, connections);\n\n return currentWay;\n }\n};", "memory": "19900" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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": "class Solution {\npublic:\n\n struct hashing{\n size_t operator() (const std::pair<std::string, std::string> & a) const{\n return std::hash<std::string>()(a.first) ^ std::hash<std::string>()(a.second)<<1;\n }\n };\n\n bool dfsForMinLexi(std::string next, std::vector<std::string> &currentWay, int travelLength, \n std::unordered_map<std::pair<std::string, std::string>, int, hashing> & ticketsLeft,\n std::unordered_map<std::string, std::vector<std::string>> & connections){\n\n travelLength--;\n currentWay.push_back(next);\n if(travelLength==0){\n return true;\n }else{\n for(const auto & connection: connections[next]){\n if(ticketsLeft[{next, connection}]>0){\n ticketsLeft[{next, connection}]--;\n if(dfsForMinLexi(connection, currentWay, travelLength, ticketsLeft, connections)){\n return true;\n }\n ticketsLeft[{next, connection}]++;\n }\n }\n }\n currentWay.pop_back();\n return false;\n }\n\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n std::unordered_map<std::pair<std::string, std::string>, int, hashing> ticketsLeft;\n std::unordered_map<std::string, std::vector<std::string>> connections;\n\n for(const auto & ticket: tickets){\n ticketsLeft[{ticket[0], ticket[1]}] ++;\n if(ticketsLeft[{ticket[0], ticket[1]}] == 1){\n connections[ticket[0]].push_back(ticket[1]);\n }\n }\n\n for(auto & [key, c]: connections){\n sort(begin(c), end(c));\n }\n\n int travelLength = tickets.size()+1;\n std::vector<std::string> currentWay;\n dfsForMinLexi(\"JFK\", currentWay, travelLength, ticketsLeft, connections);\n\n return currentWay;\n }\n};", "memory": "20000" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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": "class Solution {\npublic:\n void dfs(vector<string>& ans, map<string, multiset<string>>& adj,\n string s) {\n while (adj[s].size()) {\n string v = *(adj[s].begin());\n adj[s].erase(adj[s].begin());\n dfs(ans, adj, v);\n }\n ans.push_back(s);\n }\n\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n map<string, multiset<string>> adj;\n for (int i = 0; i < tickets.size(); i++) {\n vector<string> ticket = tickets[i];\n adj[ticket[0]].insert(ticket[1]);\n }\n vector<string> ans;\n dfs(ans, adj, \"JFK\");\n reverse(ans.begin(), ans.end());\n return ans;\n }\n};", "memory": "20100" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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": "class Solution {\nprivate:\n using pq=priority_queue<string,vector<string>,greater<string>>;\n vector<string> ans;\n void dfs(string node,unordered_map<string,pq> &mp){\n while(!mp[node].empty()){\n string adjNode=mp[node].top();\n mp[node].pop();\n dfs(adjNode,mp);\n }\n ans.push_back(node);\n }\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n unordered_map<string,pq> mp;\n for(auto val: tickets) mp[val[0]].push(val[1]);\n\n dfs(\"JFK\",mp);\n reverse(ans.begin(),ans.end());\n \n return ans;\n }\n};", "memory": "20200" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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": "class Solution {\npublic:\n unordered_map<string,\n priority_queue<string, vector<string>, greater<string>>>\n adj;\n vector<string> result;\n void dfs(string u) {\n auto& edges = adj[u];\n\n while (!edges.empty()) {\n string v = edges.top();\n edges.pop();\n dfs(v);\n }\n result.push_back(u);\n }\n\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n for (auto e : tickets)\n adj[e[0]].push(e[1]);\n\n dfs(\"JFK\");\n reverse(result.begin(), result.end());\n return result;\n }\n};", "memory": "20300" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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": "class Solution {\nprivate:\n using pq=priority_queue<string,vector<string>,greater<string>>;\n vector<string> ans;\n void dfs(string node,unordered_map<string,pq> &mp){\n while(!mp[node].empty()){\n string adjNode=mp[node].top();\n mp[node].pop();\n dfs(adjNode,mp);\n }\n ans.push_back(node);\n }\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n unordered_map<string,pq> mp;\n for(auto val: tickets) mp[val[0]].push(val[1]);\n\n dfs(\"JFK\",mp);\n reverse(ans.begin(),ans.end());\n \n return ans;\n }\n};", "memory": "20400" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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": "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": "20500" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n for (auto ticket : tickets)\n targets[ticket[0]].insert(ticket[1]);\n visit(\"JFK\");\n return vector<string>(route.rbegin(), route.rend());\n }\n\n map<string, multiset<string>> targets;\n vector<string> route;\n\n void visit(string airport) {\n while (targets[airport].size()) {\n string next = *targets[airport].begin();\n targets[airport].erase(targets[airport].begin());\n visit(next);\n }\n route.push_back(airport);\n }\n \n vector<string> findItineraryTLE(vector<vector<string>>& tickets) {\n \n int ticket_size = tickets.size();\n \n unordered_map<string, deque<string>> um;\n vector<string> result;\n sort(tickets.rbegin(), tickets.rend());\n \n for(auto& each : tickets)\n {\n um[each[0]].emplace_back(each[1]);\n }\n \n \n \n \n string jfk = \"JFK\";\n \n result.push_back(jfk);\n \n DFS(um, jfk, result, tickets.size());\n \n return result;\n }\n \n bool DFS(unordered_map<string, deque<string>>& um, string& cur, vector<string>& result, int ticket_size)\n {\n if(result.size() == ticket_size+1)\n {\n return true;\n }\n \n if(!um.count(cur))\n {\n cout << cur << endl;\n return false;\n }\n \n auto copy_um = um;\n for(auto& u : copy_um[cur])\n {\n auto back = um[cur].back();\n um[cur].pop_back();\n result.push_back(back);\n \n if(DFS(um, back, result, ticket_size))\n {\n return true;\n }\n \n um[cur].push_front(back);\n result.pop_back();\n }\n return false;\n }\n};\n\n// JFK -> NRT, KUL\n// NRT -> JFK\n\n", "memory": "20600" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\n\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n unordered_map<string, vector<string>> connections;\n for(int i=0;i<tickets.size();i++){\n connections[tickets[i][0]].push_back(tickets[i][1]);\n }\n for( auto &s:connections){\n sort(s.second.rbegin(), s.second.rend());\n }\n vector<string> itenary;\n stack<string> st;\n st.push(\"JFK\");\n while(!st.empty()){\n string currAirport = st.top();\n vector<string> &destinations = connections[currAirport];\n if(!destinations.empty()){\n string nextDestination = destinations.back();\n destinations.pop_back();\n st.push(nextDestination);\n }\n else{\n itenary.push_back(currAirport);\n st.pop();\n }\n }\n reverse(itenary.begin(),itenary.end());\n return itenary;\n }\n};", "memory": "20600" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\npublic:\n unordered_map<string, vector<string>> flightGraph;\n \n // Store the final itinerary\n vector<string> itinerary;\n/*map<string, multiset<string>> targets;\nvector<string> route;\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n return findItinerary1(tickets);\n }\n vector<string> findItinerary1(vector<vector<string>> tickets) {\n for (auto ticket : tickets)\n targets[ticket[0]].insert(ticket[1]);\n visit(\"JFK\");\n return vector<string>(route.rbegin(), route.rend());\n}\n\n\n\nvoid visit(string airport) {\n while (targets[airport].size()) {\n string next = *targets[airport].begin();\n targets[airport].erase(targets[airport].begin());\n visit(next);\n }\n route.push_back(airport);\n}*/\nvoid dfs(string airport) {\n vector<string> &destinations = flightGraph[airport];\n \n // Visit destinations in lexical order\n while (!destinations.empty()) {\n string nextDestination = destinations.back();\n destinations.pop_back();\n dfs(nextDestination);\n }\n \n // Add the current airport to the itinerary after visiting all destinations\n itinerary.push_back(airport);\n }\n\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n // Populate the flight graph using ticket information\n for (int i = 0; i < tickets.size(); i++) {\n string from = tickets[i][0];\n string to = tickets[i][1];\n\n flightGraph[from].push_back(to);\n }\n \n // Sort destinations in reverse order to visit lexical smaller destinations first\n for (auto &entry : flightGraph) {\n sort(entry.second.rbegin(), entry.second.rend());\n }\n \n // Start the DFS from the JFK airport\n dfs(\"JFK\");\n \n // Reverse the itinerary to get the correct order\n reverse(itinerary.begin(), itinerary.end());\n \n return itinerary;\n }\n};", "memory": "20700" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\n vector<string> res;\n unordered_map<string, vector<string>> G;\npublic:\n vector<string> findItinerary(vector<vector<string>>& T) {\n for(auto &t : T)\n G[t[0]].push_back(t[1]);\n for(auto &[a, v] : G)\n sort(v.rbegin(), v.rend());\n // for(auto &a : G[\"JFK\"])\n // cout<<a<<' ';\n dfs(\"JFK\");\n reverse(res.begin(), res.end());\n return res;\n }\n\n void dfs(const string& a){\n // cout<<a<<' ';\n while(!G[a].empty())\n {\n string b = G[a].back();\n G[a].pop_back();\n dfs(b);\n }\n res.push_back(a);\n }\n};", "memory": "20700" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\nprivate:\n unordered_map<string, vector<string>> graph;\n vector<string> route;\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n for (auto& ticket : tickets) {\n graph[ticket[0]].push_back(ticket[1]);\n }\n for (auto &entry : graph) {\n sort(entry.second.rbegin(), entry.second.rend());\n }\n stack<string> dfsStack;\n dfsStack.push(\"JFK\");\n\n while (!dfsStack.empty()) {\n string top = dfsStack.top();\n if (graph[top].empty()) {\n dfsStack.pop();\n route.emplace_back(top);\n } else {\n string next = graph[top].back();\n graph[top].pop_back();\n dfsStack.push(next);\n }\n }\n reverse(route.begin(), route.end());\n return route;\n }\n};", "memory": "20800" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n vector<string> res;\n map<string,vector<string>> ma;\n for(int i=0;i<tickets.size();i++)\n {\n ma[tickets[i][0]].push_back(tickets[i][1]);\n }\n for(auto& ele:ma)\n {\n cout << ele.first << \" \";\n sort(ele.second.rbegin(),ele.second.rend());\n for(auto x:ele.second)\n cout << x << \" \";\n cout << \"\\n\";\n }\n \n deque<string> dq;\n dq.push_back(\"JFK\");\n while(!dq.empty())\n {\n auto& ele = ma[dq.back()];\n cout << dq.back() << \" \";\n for(auto x:ele)\n cout << x << \" \";\n if(ele.empty())\n {\n cout << \"here\" << \" \";\n res.push_back(dq.back());\n dq.pop_back();\n }\n else\n {\n cout << \"here1 \" << \" \" << ele[ele.size() - 1];\n dq.push_back(ele[ele.size() - 1]);\n ele.pop_back();\n }\n cout << \"\\n\";\n }\n return {res.rbegin(),res.rend()};\n\n }\n};\n", "memory": "20900" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\npublic:\n void eulerPath(string u, map<string, vector<string>>& adj, map<string, int>& vis, vector<string>& path) {\n\n for (auto v: adj[u]) {\n string edge = u + \"_\" + v;\n if (vis[edge] != 0) {\n vis[edge]--;\n eulerPath(v, adj, vis, path);\n }\n }\n\n path.push_back(u);\n }\n\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n map<string, vector<string>> adj;\n map<string, int> vis;\n for (int i = 0; i < tickets.size(); i++) {\n adj[tickets[i][0]].push_back(tickets[i][1]);\n string edge = tickets[i][0] + \"_\" + tickets[i][1];\n vis[edge]++;\n }\n\n for (auto it: adj) {\n vector<string> vec = adj[it.first];\n sort(vec.begin(), vec.end());\n adj[it.first] = vec;\n }\n\n vector<string> path;\n eulerPath(\"JFK\", adj, vis, path);\n reverse(path.begin(), path.end());\n return path;\n }\n};", "memory": "21000" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n vector<string> res;\n map<string,vector<string>> ma;\n for(int i=0;i<tickets.size();i++)\n {\n ma[tickets[i][0]].push_back(tickets[i][1]);\n }\n for(auto& ele:ma)\n {\n sort(ele.second.rbegin(),ele.second.rend());\n }\n \n deque<string> dq;\n dq.push_back(\"JFK\");\n while(!dq.empty())\n {\n auto& ele = ma[dq.back()];\n if(ele.empty())\n {\n res.push_back(dq.back());\n dq.pop_back();\n }\n else\n {\n dq.push_back(ele[ele.size() - 1]);\n ele.pop_back();\n }\n }\n return {res.rbegin(),res.rend()};\n\n }\n};\n", "memory": "21100" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n sort(tickets.rbegin(), tickets.rend());\n\n unordered_map<string, vector<string>> graph;\n for(const auto& ticket : tickets)\n {\n graph[ticket.front()].push_back(ticket.back());\n }\n\n stack<string> st;\n st.push(\"JFK\");\n vector<string> result;\n\n while(!st.empty())\n {\n while(!graph[st.top()].empty())\n {\n auto& temp = st.top();\n st.push(graph[temp].back());\n graph[temp].pop_back();\n }\n result.push_back(st.top());\n st.pop();\n }\n\n return {result.rbegin(), result.rend()};\n\n\n }\n};", "memory": "21200" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\npublic:\n void construct_itinerary(string current_city,\n vector<string> &results_array,map<string,vector<string>> &journey_path){\n // results_array.push_back(current_city);\n while(journey_path[current_city].size()!=0){\n string temp_current_city = journey_path[current_city].back();\n journey_path[current_city].pop_back();\n construct_itinerary(temp_current_city,results_array,journey_path);\n }\n results_array.push_back(current_city);\n }\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n map<string,vector<string>> journey_path;\n vector<string> results_array;\n for(auto element:tickets) journey_path[element[0]].push_back(element[1]);\n \n for(auto &element:journey_path) sort(element.second.rbegin(),element.second.rend());\n \n construct_itinerary(\"JFK\", results_array, journey_path);\n reverse(results_array.begin(),results_array.end());\n return results_array;\n }\n};", "memory": "21300" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n map<string, vector<string>> graph;\n for (auto ticket_pair : tickets){\n string from = ticket_pair[0];\n string to = ticket_pair[1];\n if (graph.find(from) == graph.end()){\n vector<string> tmp;\n graph[from] = tmp;\n }\n graph[from].push_back(to);\n }\n for (auto it=graph.begin();it!=graph.end();it++){\n sort(it->second.rbegin(), it->second.rend());\n }\n vector<string> ans;\n stack<string> st;\n st.push(\"JFK\");\n while (st.size() > 0){\n string current_airport = st.top();\n if (graph.find(current_airport) != graph.end() && graph[current_airport].size() != 0){\n int size = graph[current_airport].size();\n st.push(graph[current_airport][size-1]);\n graph[current_airport].pop_back();\n }else{\n ans.push_back(current_airport);\n st.pop();\n }\n }\n std::reverse(ans.begin(),ans.end());\n return ans;\n }\n};", "memory": "21400" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\nprivate:\n void dfsTraversal(map<string, vector<string>>& airport_graph, string& current, vector<string>& result) {\n while (airport_graph.contains(current) && airport_graph[current].size() > 0) {\n string destination = airport_graph[current].back();\n airport_graph[current].pop_back();\n\n dfsTraversal(airport_graph, destination, result);\n }\n result.push_back(current);\n }\npublic:\n vector<string> findItinerary(vector<vector<string>>& tickets) {\n map<string, vector<string>> airport_graph;\n for (vector<string> ticket : tickets) {\n string source = ticket[0];\n string destination = ticket[1];\n airport_graph[source].push_back(destination);\n }\n for (auto it: airport_graph) {\n sort(airport_graph[it.first].rbegin(), airport_graph[it.first].rend());\n }\n vector<string> result;\n string start =\"JFK\";\n dfsTraversal(airport_graph, start, result);\n reverse(result.begin(), result.end());\n return result;\n }\n};", "memory": "21800" }
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>&quot;JFK&quot;</code>, thus, the itinerary must begin with <code>&quot;JFK&quot;</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>[&quot;JFK&quot;, &quot;LGA&quot;]</code> has a smaller lexical order than <code>[&quot;JFK&quot;, &quot;LGB&quot;]</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>&nbsp;</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 = [[&quot;MUC&quot;,&quot;LHR&quot;],[&quot;JFK&quot;,&quot;MUC&quot;],[&quot;SFO&quot;,&quot;SJC&quot;],[&quot;LHR&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;MUC&quot;,&quot;LHR&quot;,&quot;SFO&quot;,&quot;SJC&quot;] </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 = [[&quot;JFK&quot;,&quot;SFO&quot;],[&quot;JFK&quot;,&quot;ATL&quot;],[&quot;SFO&quot;,&quot;ATL&quot;],[&quot;ATL&quot;,&quot;JFK&quot;],[&quot;ATL&quot;,&quot;SFO&quot;]] <strong>Output:</strong> [&quot;JFK&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;SFO&quot;] <strong>Explanation:</strong> Another possible reconstruction is [&quot;JFK&quot;,&quot;SFO&quot;,&quot;ATL&quot;,&quot;JFK&quot;,&quot;ATL&quot;,&quot;SFO&quot;] but it is larger in lexical order. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tickets.length &lt;= 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>
3
{ "code": "class Solution {\npublic:\n // We use a map to store the adjacency list of the graph.\n unordered_map<string, deque<string>> graph;\n \n vector<string> findItinerary(vector<vector<string>>& tickets) {\n // Step 1: Build the graph (adjacency list)\n for (const auto& ticket : tickets) {\n graph[ticket[0]].push_back(ticket[1]);\n }\n \n // Step 2: Sort the destinations for each departure point in lexical order.\n for (auto& [from, destinations] : graph) {\n sort(destinations.begin(), destinations.end());\n }\n \n vector<string> result;\n dfs(\"JFK\", result); // Start DFS from \"JFK\"\n \n // Step 3: Since we backtrack in reverse order, reverse the final result.\n reverse(result.begin(), result.end());\n return result;\n }\n \n void dfs(const string& from, vector<string>& result) {\n auto& destinations = graph[from];\n \n // Explore all edges/tickets from the current airport.\n while (!destinations.empty()) {\n string to = destinations.front(); // Get the lexicographically smallest destination\n destinations.pop_front(); // Remove the edge after using it\n dfs(to, result); // Perform DFS on the next destination\n }\n \n // Backtrack by adding the airport to the result.\n result.push_back(from);\n }\n};", "memory": "21900" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
0
{ "code": "static auto _ = []() {\n ios::sync_with_stdio(false);\n return true;\n}();\n\nclass Solution {\npublic:\n static constexpr bool increasingTriplet(const vector<int>& nums) {\n int n1 = INT_MAX, n2 = INT_MAX;\n for(auto n : nums) {\n if(n1 >= n) {\n n1 = n;\n } else if(n2 >= n) {\n n2 = n;\n } else {\n return true;\n }\n }\n return false;\n }\n};", "memory": "114000" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
0
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n ios::sync_with_stdio(false);\n //c1<c2<c3 we try to make\n int c1=INT_MAX, c2=INT_MAX;\n for(auto &x: nums){\n if(x<=c1) c1=x;\n else if(x<=c2) c2=x;\n else return true;\n }\n return false;\n }\n};", "memory": "114100" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
0
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n if ( nums.size() < 3) {\n return false;\n }\n \n int minOne = INT_MAX;\n int minTwo = INT_MAX;\n\n for (int num : nums) {\n if (num < minOne) {\n minOne = num;\n }\n\n else if (num > minOne) {\n minTwo = min(num, minTwo);\n }\n\n if (num > minTwo) {\n return true;\n }\n }\n\n return false;\n }\n};", "memory": "114100" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
0
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& v) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n vector<int> ans;\n for (int& i : v) {\n auto it = lower_bound(ans.begin(), ans.end(), i);\n if (it == ans.end()) {\n if (ans.size() == 2) return true;\n ans.push_back(i);\n }\n else *it = i;\n }\n return false;\n }\n};", "memory": "114200" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
0
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) \n {\n int first_small = INT_MAX;\n int second_small = INT_MAX;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]<=first_small)\n first_small=nums[i];\n else if( nums[i]<= second_small )\n second_small= nums[i];\n else\n return true;\n }\n return false;\n }\n};", "memory": "114300" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
0
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n // bool type=false;\n // int king = nums.size();\n // for(int i=0;i<king-2;i++){\n // if(nums[i]<nums[i+1] && nums[i+1]<nums[i+2]) {type=true; break;}\n // }\n // if(type==false){\n \n // }\n // return type;\n int first_small = INT_MAX;\n int second_small = INT_MAX;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]<=first_small)\n first_small=nums[i];\n else if( nums[i]<= second_small )\n second_small= nums[i];\n else\n return true;\n }\n return false;\n }\n};", "memory": "114300" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
0
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n = nums.size();\n if (n < 3)\n return false;\n int f1 = INT_MAX;\n int f2 = INT_MAX;\n for (int i : nums) {\n if (i <= f1) {\n f1 = i;\n } else if (i <= f2) {\n f2 = i;\n } else {\n return true;\n }\n }\n return false;\n }\n};", "memory": "114400" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
0
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int x = INT_MAX;\n int y = INT_MAX;\n\n for (int i=0; i<nums.size();i++) {\n if (x >= nums[i]) {\n x = nums[i];\n } else if (y >= nums[i]) {\n y = nums[i];\n } else {\n return true;\n }\n }\n return false;\n }\n};", "memory": "114400" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
1
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n if(nums.size()<3){\n return false;\n }\n int a = INT_MAX;\n int b = INT_MAX;\n\n for (int num:nums){\n if (num<=a){\n a = num;\n } else if (num<=b){\n b = num;\n } else{\n return true;\n }\n\n }\n return false;\n\n\n }\n};", "memory": "114500" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
1
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n=nums.size();\n int first=INT_MAX;\n int second=INT_MAX;\n for(int i=0;i<n;i++){\n if(nums[i]<=first){\n first=nums[i];\n }\n else if(nums[i]<=second){\n second=nums[i];\n }\n\n else\n return true;\n }\n\n return false;\n }\n};", "memory": "114500" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int a = INT_MAX;\n int b = INT_MAX;\n for(int num : nums){\n if(num <= a) a = num;\n else if (num <= b) b = num;\n else return true;\n }\n return false;\n } \n};", "memory": "114600" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n // bool type=false;\n // int king = nums.size();\n // for(int i=0;i<king-2;i++){\n // if(nums[i]<nums[i+1] && nums[i+1]<nums[i+2]) {type=true; break;}\n // }\n // if(type==false){\n \n // }\n // return type;\n int first_small = INT_MAX;\n int second_small = INT_MAX;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]<=first_small)\n first_small=nums[i];\n else if( nums[i]<= second_small )\n second_small= nums[i];\n else\n return true;\n }\n return false;\n }\n};", "memory": "114600" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int small=INT_MAX,large=INT_MIN;\n vector<bool>store(nums.size(),false);\n for(int i=0;i<nums.size();i++){\n if(small<nums[i])store[i]=true;\n small=min(small,nums[i]);\n }\n \n \n for(int j=nums.size()-1;j>=0;j--){\n if(large>nums[j]&&store[j])return true;\n large=max(nums[j],large);\n }\n return false;\n }\n};", "memory": "114700" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int small=INT_MAX,large=INT_MIN;\n vector<bool>store(nums.size(),false);\n for(int i=0;i<nums.size();i++){\n if(small<nums[i])store[i]=true;\n small=min(small,nums[i]);\n }\n \n \n for(int j=nums.size()-1;j>=0;j--){\n if(large>nums[j]&&store[j])return true;\n large=max(nums[j],large);\n }\n return false;\n }\n};", "memory": "114700" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int small=INT_MAX,large=INT_MIN;\n vector<bool>store(nums.size(),false);\n for(int i=0;i<nums.size();i++){\n if(small<nums[i])store[i]=true;\n small=min(small,nums[i]);\n }\n for(auto e:store)cout<<e<<\" \";\n \n for(int j=nums.size()-1;j>=0;j--){\n if(large>nums[j]&&store[j])return true;\n large=max(nums[j],large);\n }\n return false;\n }\n};", "memory": "114800" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n vector<bool> min2(nums.size(),false);\n\n int min1 = nums[0];\n for(int j=1;j<nums.size();j++)\n {\n if(nums[j]<=min1)\n {\n min1 = nums[j];\n }\n else{\n min2[j] = true;\n }\n\n }\n int max1 = nums[nums.size()-1];\n for(int j=nums.size()-2;j>=0;j--)\n {\n if(nums[j]>=max1)\n {\n max1 = nums[j];\n }\n else{\n \n if(min2[j]==true)\n {\n return true;\n }\n }\n }\n return false;\n\n \n }\n};", "memory": "114900" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n if(nums.size() < 3) return false;\n set<int> s;\n for(int x : nums){\n s.insert(x);\n }\n if(s.size() < 3) return false;\n for(int i = 0; i < nums.size(); i++){\n\n }\n for(int i = 0; i < nums.size() - 2; i++){\n for(int j = i + 1; j < nums.size() - 1; j++){\n if(nums.at(j) > nums.at(i)){\n for(int k = j + 1; k < nums.size(); k++){\n if(nums.at(k) > nums.at(j)){\n return true;\n }\n }\n }\n }\n\n // if(nums.at(i - 1) < nums.at(i) && nums.at(i) < nums.at(i + 1)){\n // return true;\n // }\n }\n return false;\n\n\n }\n};", "memory": "115300" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "typedef long long int;\n\nclass Solution {\n\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int totalNumbers = nums.size();\n set<int> ans(nums.begin(),nums.end());\n if(ans.size()>=3){\n\n for(int i = 0;i<totalNumbers;i++){\n for(int j=i+1;j<totalNumbers;j++){\n if(nums[i]<nums[j]){\n for(int k = j+1;k<totalNumbers;k++){\n if(nums[j]<nums[k]){\n cout<<i<<j<<k;\n return true;\n }\n }}\n }\n }}\n\n return false;\n \n }\n};", "memory": "115400" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n if (nums.size() < 3)\n return false;\n set<int> temp;\n for(int i : nums)\n temp.insert(i);\n if(temp.size() < 3)\n return false;\n for (int i = 0; i < nums.size() - 2; i++)\n {\n for (int j = i + 1; j < nums.size() - 1; j++)\n {\n if (nums[i] >= nums[j]) continue;\n for (int k = j + 1; k < nums.size(); k++)\n {\n if (nums[j] < nums[k])\n return true;\n }\n }\n }\n return false;\n }\n};", "memory": "115500" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n bool right[600000]={}, left[600000]={};\n int mx = nums.back();\n int mxIndex = nums.size()-1;\n for(int i=nums.size()-2;i>=0;i--){\n if(nums[i]>=mx){\n mx = nums[i];\n mxIndex = i;\n }\n else{\n right[i] = mxIndex;\n }\n }\n mx = nums[0], mxIndex = 0;\n for(int i=1;i<nums.size();i++){\n if(nums[i]<=mx){\n mx = nums[i];\n mxIndex = i;\n }\n else{\n if(right[i])return true;\n left[i] = mxIndex;\n }\n }\n return false;\n }\n};", "memory": "115600" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n set<int> st;\n int n = nums.size();\n vector<bool> right(n, 0);\n stack<int> s;\n for(int i=n-1;i>-1;i--){\n while(s.size() && nums[s.top()] <= nums[i])\n s.pop();\n s.push(i);\n right[i] = s.size()>1;\n \n }\n for(int i=0; i<n;i++){\n st.insert(nums[i]);\n auto itr = st.lower_bound(nums[i]);\n int ele = distance(st.begin(), itr);\n if(ele > 0 && right[i])return true;\n }\n return false;\n }\n};", "memory": "115700" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n=nums.size();\n \n unordered_map<int,int> triplets;\n for(int i=0;i<n;++i){\n int k=2;\n while(k>=0){\n\n if(!triplets.count(k)||triplets[k]>nums[i]) k--;\n else break;\n }\n \n\n if(k<0) k=0;\n \n if(triplets.count(k)&&triplets[k]<nums[i]) k++;\n \n\n if(k==2) return true;\n\n\n triplets[k]=nums[i];\n\n }\n\n\n return false;\n }\n};", "memory": "115800" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n bool right[600000]={}, left[600000]={};\n int mx = nums.back();\n int mxIndex = nums.size()-1;\n for(int i=nums.size()-2;i>=0;i--){\n if(nums[i]>=mx){\n mx = nums[i];\n mxIndex = i;\n }\n else{\n right[i] = mxIndex;\n }\n }\n mx = nums[0], mxIndex = 0;\n for(int i=1;i<nums.size();i++){\n if(nums[i]<=mx){\n mx = nums[i];\n mxIndex = i;\n }\n else{\n if(right[i])return true;\n left[i] = mxIndex;\n }\n }\n return false;\n }\n};", "memory": "115900" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n\tbool increasingTriplet(vector<int>& nums) {\n\t\tif (nums.size() < 3) return false;\n\t\tunordered_map<int, bool> T;\n\t\tfor (auto s : nums) {\n\t\t\tT[s] = true;\n\t\t}\n\t\tif (T.size() < 3) return false;\n\t\tfor (int i = 0; i < nums.size(); i++) {\n\t\t\t//跳过降序区间\n\t\t\tint u = i + 1;\n\t\t\tfor (; u < nums.size(); u++) {\n\t\t\t\tif (nums[u - 1] < nums[u])\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tu--;\n\t\t\tif (u <= nums.size() - 3) {\n\t\t\t\ti = u;\n\t\t\t}\n\t\t\telse break;\n\t\t\tfor (int j = i + 1; j < nums.size(); j++) {\n\t\t\t\twhile (j < nums.size() && nums[j] <= nums[i]) {\n\t\t\t\t\tj++;\n\t\t\t\t}\n\t\t\t\tif (j == nums.size()) break;\n\t\t\t\tfor (int k = j + 1; i < nums.size(); k++) {\n\t\t\t\t\twhile (k < nums.size() && nums[k] <= nums[j]) {\n\t\t\t\t\t\tk++;\n\t\t\t\t\t}\n\t\t\t\t\tif (k == nums.size()) break;\n\t\t\t\t\tif (nums[i] < nums[j] && nums[j] < nums[k]) return true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n};", "memory": "116000" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n if(nums.size()<3) return false;\n map<int,int>map;\n map[nums[nums.size()-1]]=1;\n if(nums[nums.size()-2]<nums[nums.size()-1]) map[nums[nums.size()-2]]=2;\n else{\n map[nums[nums.size()-2]]=1;\n }\n for(int i=nums.size()-3;i>=0;i--){\n auto it=map.upper_bound(nums[i]);\n if(it!=map.end()) map[nums[i]]=2;\n else{\n map[nums[i]]=1;\n }\n for(;it!=map.end();it++){\n if(it->second==2) return true;\n }\n } \n return false; \n }\n};", "memory": "116100" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n if(nums.size()<3) return false;\n map<int,int>map;\n map[nums[nums.size()-1]]=1;\n if(nums[nums.size()-2]<nums[nums.size()-1]) map[nums[nums.size()-2]]=2;\n else{\n map[nums[nums.size()-2]]=1;\n }\n for(int i=nums.size()-3;i>=0;i--){\n auto it=map.upper_bound(nums[i]);\n map[nums[i]]=it->second+1;\n for(;it!=map.end();it++){\n if(it->second==2) return true;\n }\n } \n return false; \n }\n};", "memory": "116200" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n std::unordered_set set ( nums.begin(), nums.end() );\n if(set.size() <= 2) return false; \n\n for(auto i {0}; i < std::ssize(nums) - 2; i++) {\n for(auto j {i + 1}; j < std::ssize(nums) - 1; j++) {\n if(nums.at(j) <= nums.at(i)) continue;\n for(auto k {j + 1}; k < std::ssize(nums); k++) {\n if(nums.at(i) < nums.at(j) && nums.at(j) < nums.at(k)) return true;\n }\n }\n }\n return false;\n }\n};", "memory": "116300" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums)\n {\n unordered_set<int> uniqueNumbers;\n int s=nums.size(), a=0;\n\n for (int num : nums) {\n uniqueNumbers.insert(num); \n if (uniqueNumbers.size() == 3) { \n a=1;\n }\n }\n \n if(a==1)\n for(int a=0; a<s; a++){\n for(int b=a+1; b<s; b++){\n if(nums[a]<nums[b]){\n for(int c=b+1; c<s; c++){\n if(nums[b]<nums[c]){\n return true;\n exit(0);\n }\n }\n }\n }\n }\n\n return false;\n }\n};", "memory": "116400" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n if(nums.size()<3) return false;\n unordered_set<int> s(nums.begin(), nums.end());\n if(s.size()<3) return false; \n int j, k;\n for(int i = 0; i<nums.size()-2; i++){\n for(j = i+1; j<nums.size()-1; j++){\n if(nums[i] >= nums[j]) continue;\n for(k = j+1; k<nums.size();k++){\n if(nums[j] >= nums[k]) continue;\n return true;\n }\n }\n }\n return false;\n }\n};", "memory": "116500" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n // for(int i=1;i<n && x<2;i++) x+=(nums[i-1]<nums[i]);\n // return x==2;\n int n=nums.size(),mi,ma[n];\n mi=nums[0],ma[n-1]=nums[n-1];\n for(int i=n-2;i>=0;i--) ma[i]=max(ma[i+1],nums[i]);\n for(int i=0;i<n;i++) {\n mi=min(mi,nums[i]);\n if(mi<nums[i] && nums[i]<ma[i]) return 1;\n }\n return 0;\n }\n};", "memory": "116600" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n stack<int> s;\n int n = nums.size();\n int nb[n];\n for(int i=n-1;i>=0;i--) {\n while(s.size() && nums[s.top()] <= nums[i]) s.pop();\n if(s.size()) nb[i] = s.top();\n else nb[i] = n;\n s.push(i);\n }\n while(s.size()) s.pop();\n for(int i=0;i<n;i++) {\n while(s.size() && nums[s.top()] >= nums[i]) s.pop();\n if(s.size() && nb[i] != n) return true;\n s.push(i);\n }\n return false;\n }\n};", "memory": "116700" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n = nums.size();\n int maxRight[n];\n maxRight[n - 1] = nums[n - 1];\n for (int i = n - 2; i >= 0; i--) {\n maxRight[i] = max(maxRight[i + 1], nums[i + 1]);\n }\n int minLeft = nums[0];\n for (int i = 1; i < n - 1; i++) {\n if (minLeft < nums[i] && nums[i] < maxRight[i])\n return true;\n minLeft = min(minLeft, nums[i]);\n }\n return false;\n }\n};", "memory": "116800" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n //int i=0;\n int n=nums.size();\n if(n<3)return false;\n int m=nums[0];\n vector<int>v;\n for(int i=1;i<n;i++){\n if(nums[i]>m){\n v.push_back(nums[i]);\n }\n else m=min(nums[i],m);\n }\n if(v.size()==0)return false;\n m=v[0];\n for(int i=1;i<v.size();i++){\n if(v[i]>m){\n return true;\n }\n else m=min(v[i],m);\n }\n return false;\n }\n};", "memory": "117000" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n //int i=0;\n int n=nums.size();\n if(n<3)return false;\n int m=nums[0];\n vector<int>v;\n for(int i=1;i<n;i++){\n if(nums[i]>m){\n v.push_back(nums[i]);\n }\n else m=min(nums[i],m);\n }\n if(v.size()==0)return false;\n m=v[0];\n for(int i=1;i<v.size();i++){\n if(v[i]>m){\n return true;\n }\n else m=min(v[i],m);\n }\n return false;\n }\n};", "memory": "117100" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n // O(n*log(n)) to insert all values to BST. O(n*log(n)) to check if each value has values higher and smaller than him. \n if (nums.size() < 3) {\n return false;\n }\n // map of all occurences of nums\n unordered_map<int, int> nums_occurences;\n set<int> nums_set; // BST - O(log(n)) insertion and access.\n for (int num: nums) {\n if (!nums_occurences.count(num)) {\n nums_occurences[num] = 1;\n nums_set.insert(num);\n } else {\n nums_occurences[num]++;\n }\n }\n int last_min = nums[0];\n remove_occurences(nums_occurences, nums_set, nums[0]);\n int cur_num;\n for (int num_index = 1; num_index < nums.size(); num_index++) {\n cur_num = nums[num_index];\n if (cur_num < last_min) {\n last_min = cur_num;\n }\n else if (cur_num > last_min) {\n if (cur_num < *nums_set.rbegin()) { // biggest key in nums_set\n return true;\n }\n }\n remove_occurences(nums_occurences, nums_set, cur_num);\n }\n return false;\n }\n\n void remove_occurences(unordered_map<int,int>& nums_occurences, set<int>& nums_set, int cur_num) {\n nums_occurences[cur_num]--;\n if (nums_occurences[cur_num] == 0) {\n nums_occurences.erase(cur_num);\n nums_set.erase(cur_num);\n }\n }\n};", "memory": "117200" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& a) {\n map<int,int> ls, rs ;\n int n=a.size() ;\n for(int i=0 ; i<n ; i++) {\n rs[a[i]]++ ;\n }\n for(int i=0 ; i<n ; i++) {\n ls[a[i]]++ ;\n if(ls.find(a[i])!=ls.begin() && rs.find(a[i])!=prev(rs.end())) return 1 ;\n // if(ls.find(a[i])!=ls.begin() && ls.find(a[i])!=ls.end() && rs.find(a[i])!=prev(rs.end())) return 1 ;\n if(--rs[a[i]]==0) rs.erase(a[i]) ;\n \n }\n return 0 ;\n }\n};", "memory": "117300" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& arr) {\n int a = INT_MAX;\n stack<int> st;\n int n = arr.size();\n for(int i=0; i<n; i++){\n if(arr[i] > a){\n return true;\n }\n if(!st.empty() && st.top() >= arr[i]){\n st.pop();\n }\n if(!st.empty() && st.top() < arr[i]){\n a = arr[i];\n }\n st.push(arr[i]);\n }\n return false;\n }\n};", "memory": "117600" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n = nums.size() ;\n int leftmin[n] ;\n int rightmax[n] ;\n leftmin[0] = nums[0] ;\n int i = 1 ;\n while ( i < n ) {\n leftmin[i] = min( leftmin[i-1] , nums[i] ) ;\n i ++ ;\n }\n rightmax[n-1] = nums[n-1] ;\n i = n-2 ;\n while ( i >= 0 ) {\n rightmax[i] = max( rightmax[i+1] , nums[i] ) ;\n i -- ;\n }\n for ( i = 0 ; i < n ; i ++ ) {\n if ( leftmin[i] < nums[i] && nums[i] < rightmax[i] ) {\n return true ;\n }\n }\n return false ;\n }\n};", "memory": "118500" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int l=nums.size();\n int left[l];\n int right[l];\n left[0]=nums[0];\n right[l-1]=nums[l-1];\n for(int i=1;i<l;i++){\n left[i]=min(left[i-1],nums[i]);\n }\n for(int i=l-2;i>=0;i--){\n right[i]=max(right[i+1],nums[i]);\n }\n for(int i=0;i<l;i++){\n if(left[i]<nums[i] && nums[i]<right[i]){\n return true;\n }\n }\n return false;\n\n\n \n \n\n \n }\n};", "memory": "118600" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n if(nums.size()<3){\n return false;\n }\n int n=nums.size();\n int left_min[n];\n int right_max[n];\n left_min[0]=nums[0];\n for(int i=1;i<nums.size();i++){\n left_min[i]=min(left_min[i-1],nums[i]);\n }\n right_max[n-1]=nums[n-1];\n for(int i=n-2;i>=0;i--){\n right_max[i]=max(right_max[i+1],nums[i]);\n }\n for(int i=0;i<n;i++){\n if(left_min[i]<nums[i]&&nums[i]<right_max[i]){\n return true;\n }\n }\n return false;\n\n \n }\n};", "memory": "118700" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n ios_base::sync_with_stdio(0); cin.tie(0);\n int n = nums.size();\n int pref[n], suff[n];\n\n pref[0] = nums[0]; \n for (int i = 1; i < n; i++)\n pref[i] = min(nums[i], pref[i - 1]);\n\n suff[n - 1] = nums[n - 1];\n for (int i = n - 2; i >= 0; i--)\n suff[i] = max(nums[i], suff[i + 1]);\n\n for (int i = 1; i < n - 1; i++)\n if ((pref[i - 1] < nums[i]) && (nums[i] < suff[i + 1]))\n return 1;\n return 0;\n \n }\n};", "memory": "118800" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n \n int N = nums.size();\n int l_r_min[N]; \n int r_l_max[N];\n int maxx_num = INT_MIN;\n int minn_num = INT_MAX;\n for(int i =0; i < nums.size(); i++) {\n\n maxx_num = max(maxx_num, nums[N-i-1]);\n minn_num = min(minn_num, nums[i]);\n l_r_min[i] = minn_num;\n r_l_max[N-i-1] = maxx_num;\n \n }\n \n\n for(int i =1; i < N-1; i++) {\n \n if(nums[i] > l_r_min[i-1] and nums[i] < r_l_max[i]) {\n return true;\n }\n }\n return false;\n }\n};", "memory": "118900" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "\n\nclass Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n\n vector<int> largest(nums.size(), 0);\n\n int largestNum = nums.back(); // Start with the last element\n\n // Iterate the array in reverse to fill the `largest` vector\n for (int i = nums.size() - 1; i >= 0; --i) {\n largestNum = max(largestNum, nums[i]);\n largest[i] = largestNum;\n }\n\n for (int i = 0; i < largest.size(); ++i) {\n cout << largest[i] << endl;\n }\n\n int smallest = nums[0];\n\n for (int i = 1; i < nums.size() - 1; ++i) {\n if (smallest < nums[i]) {\n\n if (nums[i] < largest[i + 1]) {\n return true;\n }\n } else {\n smallest = nums[i];\n }\n }\n\n return false;\n \n }\n};", "memory": "119000" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& A) {\n int n = A.size() , s = -1;\n \n vector<int> left(n, INT_MIN);\n s = A[0];\n // find smaller element to left of index\n for (int i=1 ; i<n ; i++) {\n if (A[i] > s) {\n left[i] = s;\n }\n else {\n s = A[i];\n }\n }\n\n // find greater element to the right\n int g = A[n-1];\n for (int i=n-2 ; i>=0 ; i--) {\n if (A[i] < g ) {\n if (left[i] != INT_MIN) {\n return true;\n }\n }\n else {\n g = A[i];\n }\n }\n\n return false;\n }\n};", "memory": "119100" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n if(nums.size()<3){\n return false;\n }\n if(nums.size() == 3){\n return nums[0]<nums[1] && nums[1] < nums[2];\n }\n // arr -> 1 0 2 0 3 0 4 0 5 \n // brute force\n // select k \n // select j that satisfies \n // find i that satisfies\n\n\n // for(int k = nums.size() - 1; i>1 ; i--){\n // for (int j = k-1 ; j>0 ;j--){\n // for (int i = j-1 ; i>=0 ; i--){\n // if(nums[k]>nums[j] && nums[j] > nums[i]) return true;\n // }\n // }\n // }\n\n // need to optimise this code\n\n // think in nlogn\n\n // for each element store the min before it and the max after it\n\n vector<int> min_arr = vector<int> (nums.size(),INT_MAX);\n int max_num = nums[nums.size() -1];\n // min[0] = nums[0];\n for (int i =1 ; i < nums.size() ; i++){\n min_arr[i] = min(min_arr[i-1] , nums[i-1]);\n }\n for (int i = nums.size() -2 ; i >0 ; i--){\n if(nums[i]< max_num && nums[i]> min_arr[i]) return true;\n max_num = max(max_num ,nums[i]);\n }\n\n return false;\n }\n};", "memory": "119100" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& A) {\n int n = A.size() , s = -1;\n \n vector<int> left(n, INT_MIN);\n s = A[0];\n // find smaller element to left of index\n for (int i=1 ; i<n ; i++) {\n if (A[i] > s) {\n left[i] = s;\n }\n else {\n s = A[i];\n }\n }\n\n // find greater element to the right\n int g = A[n-1];\n for (int i=n-2 ; i>=0 ; i--) {\n if (A[i] < g ) {\n if (left[i] != INT_MIN) {\n return true;\n }\n }\n else {\n g = A[i];\n }\n }\n\n return false;\n }\n};", "memory": "119200" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n=nums.size();\n vector<int>suffix(n,0);\n for(int i=n-1;i>=0;i--){\n if(i==n-1)suffix[i]=nums[i];\n else{\n suffix[i]=max(suffix[i+1],nums[i]);\n }\n }\n set<int>st;\n st.insert(nums[0]);\n for(int i=1;i<n;i++){\n st.insert(nums[i]);\n auto it=st.find(nums[i]);\n if(it!=st.begin() && suffix[i]!=nums[i]){\n return true;\n }\n }\n return false;\n }\n};", "memory": "120100" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n set<int> convertToSet(vector<int> v){\n set<int> s;\n for (int x : v) {\n s.insert(x);\n }\n return s;\n }\n bool increasingTriplet(vector<int>& nums) {\n set<int> s = convertToSet(nums);\n if(s.size() <= 2){\n return false;\n }\n int cnt,t1;\n for(int i = 0; i < nums.size();i++){\n cnt = 0;t1 = nums[i];\n for(int j = i + 1; j < nums.size();j++){\n if(nums[j] > t1){\n cnt++;\n t1 = nums[j];\n }\n else if(nums[j] < t1){\n if(nums[j] > nums[i]){\n t1 = nums[j];\n }\n }\n\n if(cnt == 2){\n return true;\n }\n }\n }\n return false;\n \n }\n};", "memory": "120100" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n // remove the impossible\n auto s = nums;\n sort(s.begin(), s.end());\n auto iter = unique(s.begin(), s.end());\n if (iter - s.begin() < 3)\n return false;\n \n int n = nums.size();\n int b{}; \n // use the j in our triplet as the base\n for (b = n - 2; b > 0; --b){\n for (int d = b+1; d < n; ++d){\n if (nums[d] > nums[b]){\n for(int a{}; a < b; ++a){\n if (nums[a] < nums[b])\n return true;\n }\n }\n }\n }\n return false;\n }\n};", "memory": "120300" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n = nums.size();\n if (n<3){\n return false;\n }\n int leftMin=nums[0];\n vector<int> right(nums.begin() + 2, nums.end());\n sort(right.begin(), right.end());\n int rightMax=right.back();\n for(int i=3;i<n;i++){\n if(nums[i] > rightMax){\n rightMax=nums[i];\n }\n }\n for(int i=1;i<n-1;i++){\n if(leftMin<nums[i] && nums[i]<rightMax){\n return true;\n }\n if(nums[i]<leftMin){\n leftMin=nums[i];\n }\n if(nums[i]==rightMax && right.size()>1){\n right.pop_back();\n rightMax=right.back();\n }\n }\n return false;\n }\n};", "memory": "120400" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n = nums.size();\n if (n<3){\n return false;\n }\n int leftMin=nums[0];\n vector<int> right(nums.begin() + 2, nums.end());\n sort(right.begin(), right.end());\n int rightMax=right.back();\n for(int i=3;i<n;i++){\n if(nums[i] > rightMax){\n rightMax=nums[i];\n }\n }\n for(int i=1;i<n-1;i++){\n if(leftMin<nums[i] && nums[i]<rightMax){\n return true;\n }\n if(nums[i]<leftMin){\n leftMin=nums[i];\n }\n if(nums[i]==rightMax && right.size()>1){\n right.pop_back();\n rightMax=right.back();\n }\n }\n return false;\n }\n};", "memory": "120400" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) \n {\n int n = nums.size();\n map<int,int> mp;\n for(int i=0;i<n;i++)\n {\n mp[nums[i]] = i;\n }\n int l[n];\n l[0] = nums[0];\n int r[n];\n r[n-1] = nums[n-1];\n for(int i=1;i<n;i++)\n {\n l[i] = min(nums[i],l[i-1]);\n }\n for(int i=n-2;i>=0;i--)\n {\n r[i] = max(nums[i],r[i+1]);\n }\n for(int i=0;i<n;i++)\n {\n if(l[i]<nums[i] && nums[i]<r[i])\n return true;\n }\n return false;\n }\n};", "memory": "120600" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n=nums.size();\n int minP;\n minP=nums[0];\n int i,j;\n stack<int>s;\n for(i=n-1;i>0;i--)\n {\n if(s.empty() || nums[i]>s.top())\n s.push(nums[i]);\n else\n s.push(s.top());\n }\n for(i=1;i<nums.size();i++)\n {\n s.pop();\n if(s.empty())\n continue;\n minP=min(minP,nums[i-1]);\n if(minP>=nums[i])\n continue;\n if(nums[i]<s.top())\n return true;\n }\n return false;\n }\n};", "memory": "120900" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "#include <ext/pb_ds/assoc_container.hpp> \n#include <ext/pb_ds/tree_policy.hpp> \nusing namespace __gnu_pbds; \n \n#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> \n\nclass Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n = nums.size();\n ordered_set os1,os2;\n for(int i=0;i<n;i++) {\n if(os2.order_of_key(nums[i])>0) {\n return true;\n }\n if(os1.order_of_key(nums[i])>0) {\n os2.insert(nums[i]);\n }\n os1.insert(nums[i]);\n }\n return false;\n }\n};", "memory": "121000" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n vector<int> a=nums;\n sort(a.begin(),a.end());\n map<int,int> m;\n for(int i=0;i<a.size();i++){\n m[a[i]]=i;\n }\n int c=3,k=INT_MIN;\n for(int i=0;i<nums.size();i++){\n if(m[nums[i]]<=nums.size()-c && k<nums[i]){\n c--;\n k=nums[i];\n }\n if(c==0){\n break;\n }\n else if(k>=nums[i]){\n k=nums[i];\n }\n }\n if(c==0){\n return true;\n }\n return false;\n }\n};", "memory": "121300" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "#include <utility>\n\nclass Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n vector<int> possible_middle; // track possible values\n int highest_subsequent_value = INT_MIN; // highest int from the end \n\n // find possible middle values;\n for (int i = nums.size() - 1; i >= 0; i--) {\n if (nums[i] > highest_subsequent_value) {\n // this is the highest value so far\n highest_subsequent_value = nums[i];\n }\n else if (nums[i] < highest_subsequent_value) {\n // does this int have a higher subsequent value?\n possible_middle.push_back(nums[i]); \n }\n }\n // reset highest_subsequent_value\n highest_subsequent_value = INT_MIN;\n\n // find possible first values - note that order is now reversed;\n for (int k = 0; k < possible_middle.size(); k++) {\n if (possible_middle[k] > highest_subsequent_value) {\n highest_subsequent_value = possible_middle[k];\n }\n else if (possible_middle[k] < highest_subsequent_value) {\n return true;\n }\n }\n // default if no possible triplet is found;\n return false;\n }\n};", "memory": "121700" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "#include <utility>\n\nclass Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n vector<int> possible_middle, possible_first; // track possible values\n int highest_subsequent_value = INT_MIN; // highest int from the end \n\n // find possible middle values;\n for (int i = nums.size() - 1; i >= 0; i--) {\n if (nums[i] > highest_subsequent_value) {\n // this is the highest value so far\n highest_subsequent_value = nums[i];\n }\n else if (nums[i] < highest_subsequent_value) {\n // does this int have a higher subsequent value?\n possible_middle.push_back(nums[i]); \n }\n }\n // reset highest_subsequent_value\n highest_subsequent_value = INT_MIN;\n\n // find possible first values - note that order is now reversed;\n for (int k = 0; k < possible_middle.size(); k++) {\n if (possible_middle[k] > highest_subsequent_value) {\n highest_subsequent_value = possible_middle[k];\n }\n else if (possible_middle[k] < highest_subsequent_value) {\n return true;\n }\n }\n // default if no possible triplet is found;\n return false;\n }\n};", "memory": "121800" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n = nums.size();\n // min value before position i\n int* min = new int[n];\n // max value after position i\n int* max = new int[n];\n min[0] = nums[0];\n max[n - 1] = nums[n - 1];\n\n // filling min\n for(int i = 1; i < n; i++){\n if(nums[i] < min[i - 1]){\n min[i] = nums[i];\n }\n else{\n min[i] = min[i - 1];\n }\n }\n\n // filling max\n for(int i = n - 2; i >= 0; i--){\n if(nums[i] > max[i + 1]){\n max[i] = nums[i];\n }\n else{\n max[i] = max[i + 1];\n }\n }\n\n for(int i = 1; i < n - 1; i++){\n if((nums[i] > min[i]) && (nums[i] < max[i])){\n return true;\n }\n }\n return false;\n }\n};", "memory": "122800" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n\tvector<int> findSequence(int iter, vector<int>& nums) {\n\t\tvector <int> passed;\n\t\tpassed.reserve(nums.size());\n\t\tint len = nums.size();\n\t\tint mx[len];\n\t\tmx[len-1] = INT_MIN;\n\n\t\tfor (int i = len-2; i >= 0; i--) {\n\t\t\tmx[i] = max(mx[i+1], nums[i+1]);\n\t\t\tif (nums[i] >= mx[i]) {\n\t\t\t\tmx[i] = INT_MIN;\n\t\t\t}\n\t\t\tif (mx[i] != INT_MIN) {\n\t\t\t\t// cout << \"ITER:\" << iter << \" => \" << nums[i] << \" m:\" << mx[i] << endl;\n\t\t\t\tpassed.push_back(nums[i]);\n\t\t\t}\n\t\t}\n\n\t\t// cout << \"ITER:\" << iter << \" => \";\n\t\t// for (auto x : passed) {\n\t\t// \tcout << x << \" \";\n\t\t// }\n\t\t// cout << endl;\n\n\t\treverse(passed.begin(), passed.end());\n\t\treturn passed;\n\t}\n\n\tbool increasingTriplet(vector<int>& nums) {\n\t\t// [1, 2, 3, 4, 5]\n\t\t// 2 mins that min[i] < min[j] so that i < j\n\t\t// [1, 2, 3, 4, 5]\n\t\t// 1, 2 2, 3 3, 4 4,5 5\n\t\t// [5, 4, 3, 4, 1, 6, 10]\n\t\t// [ 100, 2, 4, 105, 1, 10]\n\t\t// [ YES, YES, YES, NO, YES, NO]\n\t\t// 100 -> 103 ? > 103 O(n^2 * logN)\n\t\t\n\t\tbool answer = false;\n\t\tauto first = findSequence(1, nums);\n\n\t\tif (first.size() < 2) {\n\t\t\treturn false;\n\t\t}\n\n\t\tauto second = findSequence(2, first);\n\t\treturn second.size() > 0;\n\t}\n};", "memory": "122900" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n\tvector<int> findSequence(int iter, vector<int>& nums) {\n\t\tvector <int> passed;\n\t\tpassed.reserve(nums.size());\n\t\tint len = nums.size();\n\t\tint mx[len];\n\t\tmx[len-1] = INT_MIN;\n\n\t\tfor (int i = len-2; i >= 0; i--) {\n\t\t\tmx[i] = max(mx[i+1], nums[i+1]);\n\t\t\tif (nums[i] >= mx[i]) {\n\t\t\t\tmx[i] = INT_MIN;\n\t\t\t}\n\t\t\tif (mx[i] != INT_MIN) {\n\t\t\t\t// cout << \"ITER:\" << iter << \" => \" << nums[i] << \" m:\" << mx[i] << endl;\n\t\t\t\tpassed.push_back(nums[i]);\n\t\t\t}\n\t\t}\n\n\t\t// cout << \"ITER:\" << iter << \" => \";\n\t\t// for (auto x : passed) {\n\t\t// \tcout << x << \" \";\n\t\t// }\n\t\t// cout << endl;\n\n\t\treverse(passed.begin(), passed.end());\n\t\treturn passed;\n\t}\n\n\tbool increasingTriplet(vector<int>& nums) {\n\t\t// [1, 2, 3, 4, 5]\n\t\t// 2 mins that min[i] < min[j] so that i < j\n\t\t// [1, 2, 3, 4, 5]\n\t\t// 1, 2 2, 3 3, 4 4,5 5\n\t\t// [5, 4, 3, 4, 1, 6, 10]\n\t\t// [ 100, 2, 4, 105, 1, 10]\n\t\t// [ YES, YES, YES, NO, YES, NO]\n\t\t// 100 -> 103 ? > 103 O(n^2 * logN)\n\t\t\n\t\tbool answer = false;\n\t\tauto first = findSequence(1, nums);\n\n\t\tif (first.size() < 2) {\n\t\t\treturn false;\n\t\t}\n\n\t\tauto second = findSequence(2, first);\n\t\treturn second.size() > 0;\n\t}\n};", "memory": "122900" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n long long increasingTriplet(vector<int>& nums) {\n long long min_val[500005] = { 0, };\n long long reverse_max_val[500005] = { 0, };\n\n long long temp = nums[0];\n\n for (int i = 0; i < nums.size(); i++) {\n if (temp > nums[i])\n temp = nums[i];\n\n min_val[i] = temp;\n }\n\n temp = nums[nums.size() - 1];\n\n for (int i = nums.size() - 1; i >= 0; i--) {\n if (temp < nums[i])\n temp = nums[i];\n\n reverse_max_val[i] = temp;\n }\n\n for (int i = 1; i < nums.size(); i++) {\n if (min_val[i - 1] < nums[i]) {\n if (nums[i] < reverse_max_val[i + 1])\n return true;\n }\n }\n\n return false;;\n }\n};\n", "memory": "123100" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n long long increasingTriplet(vector<int>& nums) {\n long long min_val[500005] = { 0, };\n long long reverse_max_val[500005] = { 0, };\n\n long long temp = nums[0];\n\n for (int i = 0; i < nums.size(); i++) {\n if (temp > nums[i])\n temp = nums[i];\n\n min_val[i] = temp;\n }\n\n temp = nums[nums.size() - 1];\n\n for (int i = nums.size() - 1; i >= 0; i--) {\n if (temp < nums[i])\n temp = nums[i];\n\n reverse_max_val[i] = temp;\n }\n\n for (int i = 1; i < nums.size(); i++) {\n if (min_val[i - 1] < nums[i]) {\n if (nums[i] < reverse_max_val[i + 1])\n return true;\n }\n }\n\n return false;;\n }\n};\n", "memory": "123200" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int len = nums.size();\n int max = len - 1;\n int min = 0;\n int i;\n int* smaller = new int[len];\n smaller[0] = -1;\n for(i = 1; i < len; i++) {\n if(nums[i] <= nums[min]) {\n min = i;\n smaller[i] = -1;\n }\n else {\n smaller[i] = min;\n }\n }\n int* greater = new int[len];\n greater[len-1] = -1;\n for(i = len - 2; i >= 0; i--) {\n if(nums[i] >= nums[max]) {\n max = i;\n greater[i] = -1;\n }\n else {\n greater[i] = max;\n }\n }\n for(i = 0; i < len; i++) {\n if(smaller[i] != -1 && greater[i] != -1) {\n return true;\n }\n }\n delete[] smaller;\n delete[] greater;\n return false;\n }\n};", "memory": "123300" }
334
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4,5] <strong>Output:</strong> true <strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,4,3,2,1] <strong>Output:</strong> false <strong>Explanation:</strong> No triplet exists. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,5,0,4,6] <strong>Output:</strong> true <strong>Explanation:</strong> The triplet (3, 4, 5) is valid because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
3
{ "code": "class Solution {\npublic:\n bool increasingTriplet(vector<int>& nums) {\n int n = nums.size();\n vector<int>right(n);\n vector<int>left(n);\n \n left[0] = nums[0];\n for(int i = 1; i < n; i++){\n left[i] = min(left[i-1],nums[i]);\n }\n right[n-1] = nums[n-1];\n for(int i = n-2; i >= 0; i--){\n right[i] = max(right[i+1],nums[i]);\n }\n for(int i = 1; i < n-1; i++){\n if(left[i-1] < nums[i] && nums[i] < right[i+1]){\n return true;\n }\n }\n return false;\n }\n};", "memory": "123500" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 pair<int,int> includeAndNotIncludeNodes(TreeNode* node){\n if(node==NULL) return make_pair(0,0);\n\n pair<int,int>left = includeAndNotIncludeNodes(node->left);\n pair<int,int>right = includeAndNotIncludeNodes(node->right);\n\n return {node->val+left.second+right.second , max(left.first,left.second)+max(right.first,right.second)};\n }\n int rob(TreeNode* root) {\n std::ios_base::sync_with_stdio(0);\n std::cin.tie(0);\n std::cout.tie(0);\n if(root==NULL) return 0;\n pair<int,int>p = includeAndNotIncludeNodes(root);\n\n root->left = NULL;\n root->right = NULL;\n\n return max(p.first, p.second);\n }\n};", "memory": "15105" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 rob(TreeNode* root) {\n if(!root)return 0;\n if(!root->left && !root->right)return root->val;\n if(root->left && root->right){\n int mid=root->val+rob(root->left->left)+rob(root->left->right)+rob(root->right->left)+rob(root->right->right);\n int left=rob(root->left);\n int right=rob(root->right);\n\n root->left->val=left;\n root->left->left=nullptr;\n root->left->right=nullptr;\n\n root->right->val=right;\n root->right->left=nullptr;\n root->right->right=nullptr;\n\n root->val=mid;\n return max(mid,left+right);\n }\n else if(root->left){\n return max(rob(root->left->left)+rob(root->left->right)+root->val,rob(root->left));\n }\n else if(root->right){\n return max(rob(root->right->left)+rob(root->right->right)+root->val,rob(root->right));\n }\n return 0;\n }\n};", "memory": "15105" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 pair<int, int> helper(TreeNode* root){\n if(root == nullptr){\n return make_pair(0,0);\n }\n\n pair<int, int> left = helper(root->left);\n pair<int, int> right = helper(root->right);\n\n pair<int, int> ans;\n ans.first = root->val + left.second + right.second;\n ans.second = max(left.first, left.second) + max(right.first, right.second);\n\n return ans;\n }\n int rob(TreeNode* root) {\n if(root == nullptr) return 0;\n pair<int, int> ans = helper(root);\n\n return max(ans.first, ans.second);\n }\n};", "memory": "15316" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 rob(TreeNode* root) {\n \n return dfs(root).first;\n }\n\n pair<int,int> dfs(TreeNode* node) {\n if (node == nullptr) {\n return {0, 0};\n }\n\n pair<int,int> left = dfs(node->left);\n pair<int,int> right = dfs(node->right);\n\n \n // rob current;\n int current = node->val + left.second + right.second;\n\n int no_current = left.first + right.first;\n\n return {max(current, no_current), no_current};\n\n }\n};", "memory": "15316" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 pair<int,int> func(TreeNode* root){\n if(!root)return{0,0};\n if(!root->left && !root->right)return{root->val,0};\n pair<int,int> leftsum=func(root->left);\n pair<int,int> rightsum=func(root->right);\n \n return {(root->val+leftsum.second+rightsum.second),(max(leftsum.first,leftsum.second)+max(rightsum.first,rightsum.second))};\n }\n int rob(TreeNode* root) {\n pair<int,int> ans=func(root);\n return max(ans.first,ans.second);\n }\n};", "memory": "15528" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 rob(TreeNode* root) {\n pair<int,int> mx = robp(root);\n \n return max(mx.first,mx.second);\n }\n\n pair<int,int> robp(TreeNode* root){\n if(root==NULL) return pair(0,0);\n pair<int,int> right = robp(root->right);\n pair<int,int> left = robp(root->left);\n int mx = root->val + right.second + left.second;\n mx = max(mx,left.first+right.first);\n return pair(mx,left.first+right.first);\n }\n};", "memory": "15528" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 rob(TreeNode* root) {\n auto result = robsub(root);\n // vector<int,int>dp()\n return max(result.first,result.second);\n }\n\nprivate: \n pair<int,int>robsub(TreeNode* root){\n if(!root) return {0,0};\n\n auto left = robsub(root->left);\n auto right= robsub(root->right);\n\n int robcurrent = root->val + left.second + right.second;\n int skipcurr = max(left.first,left.second) + max(right.first, right.second);\n\n return {robcurrent,skipcurr};\n } \n // memo\n // pair<int,int>robsub(TreeNode* root){\n // if(!root) return {0,0};\n\n // auto left = robsub(root->left);\n // auto right= robsub(root->right);\n\n // int robcurrent = root->val + left.second + right.second;\n // int skipcurr = max(left.first,left.second) + max(right.first, right.second);\n\n // return {robcurrent,skipcurr};\n // } \n};", "memory": "15739" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 pair<int,int> maxAmount(TreeNode* root){\n if(root==NULL)return {0,0};\n auto l = maxAmount(root->left);\n auto r = maxAmount(root->right);\n return {root->val+ l.second + r.second, max(l.first,l.second)+max(r.first,r.second)};\n }\n int rob(TreeNode* root) {\n auto ans = maxAmount(root);\n return max(ans.first,ans.second);\n }\n};", "memory": "15739" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 pair<int, int> helper(TreeNode* root){\n if(root == nullptr){\n return make_pair(0,0);\n }\n\n pair<int, int> left = helper(root->left);\n pair<int, int> right = helper(root->right);\n\n pair<int, int> ans;\n ans.first = root->val + left.second + right.second;\n ans.second = max(left.first, left.second) + max(right.first, right.second);\n\n return ans;\n }\n int rob(TreeNode* root) {\n\n pair<int, int> ans = helper(root);\n\n return max(ans.first, ans.second);\n }\n};", "memory": "15950" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 {\n pair<int,int> solve(TreeNode* root){\n if(root==NULL){\n return {0,0};\n }\n pair<int,int> lc,rc;\n lc=solve(root->left);\n rc=solve(root->right);\n pair<int,int> ans;\n ans.first=root->val+lc.second+rc.second;\n ans.second=max(lc.first,lc.second)+max(rc.first,rc.second);\n return ans;\n }\n\npublic:\n int rob(TreeNode* root) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n pair<int,int> ans=solve(root);\n return max(ans.first,ans.second);\n }\n};", "memory": "16161" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int getValue(TreeNode* node,vector<int>& vec){\n fill(vec.begin(), vec.end(), 0);\n vec[0] = node->val;\n if(node->left != NULL)\n {\n vec[1] = node->left->val;\n if(node->left->left != NULL)\n vec[3] = node->left->left->val;\n if(node->left->right != NULL)\n vec[4] = node->left->right->val;\n }\n if(node->right != NULL)\n {\n vec[2] = node->right->val;\n if(node->right->left != NULL)\n vec[5] = node->right->left->val;\n if(node->right->right != NULL)\n vec[6] = node->right->right->val;\n }\n return max(vec[0]+vec[3]+vec[4]+vec[5]+vec[6],vec[1]+vec[2]);\n }\n\n int rob(TreeNode* root) {\n TreeNode* node = root;\n stack<TreeNode*> sk;\n TreeNode* last = NULL;\n vector<TreeNode*> res;\n vector<int> vec(7,0);\n while(node || !sk.empty()){\n while(node)\n {\n sk.push(node);\n node = node->left;\n }\n node = sk.top();\n\n if(!node->right || node->right == last){\n res.push_back(node);\n sk.pop();\n last = node;\n node = NULL;\n }\n else{\n node = node->right;\n }\n }\n for(int i=0;i<res.size();i++)\n res[i]->val = getValue(res[i],vec);\n return root->val;\n }\n};", "memory": "16795" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 */\n int final_ans;\n int ans (TreeNode* root,int &c){\n if(root == NULL){\n c=0;\n return 0;\n }\n //int c = max(p,gp+root->val);\n // final_ans =max(final_ans,c);\n int left_ans =0 , right_ans =0,lc=0,rc=0;\n if(root->left){\n left_ans =ans(root->left,lc);\n }\n if(root->right){\n right_ans= ans(root->right,rc);\n }\n c= left_ans+right_ans;\n cout<<c<<\" \";\n return max(left_ans+right_ans,root->val+lc+rc);\n }\nclass Solution {\npublic:\n int rob(TreeNode* root) {\n int k=0;\n return ans(root,k);\n }\n};", "memory": "17006" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 ans[10010][2];\n void print(TreeNode *root){\n if(root->left) print(root->left);\n if(root->right) print(root->right);\n cout<<root->val<<' ';\n }\n void traverse(TreeNode *root,TreeNode *x,int &c){\n //cout<<c<<' ';\n //x=new TreeNode(c);\n if(root->left){\n x->left=new TreeNode(c++);\n traverse(root->left,x->left,c);\n } \n if(root->right){\n x->right=new TreeNode(c++);\n traverse(root->right,x->right,c);\n }\n }\n int dp(TreeNode *root,TreeNode *x,int check){\n if(root==NULL || x==NULL) return 0;\n if(ans[x->val][check]!=-1) return ans[x->val][check];\n //cout<<root->val<<' ';\n int f=dp(root->left,x->left,0)+dp(root->right,x->right,0);\n if(check==0){\n f=max(root->val+dp(root->left,x->left,1)+dp(root->right,x->right,1),f);\n }\n return ans[x->val][check]=f;\n }\n int rob(TreeNode* root){\n memset(ans,-1,sizeof(ans));\n TreeNode *x=new TreeNode(0);\n // TreeNode *y=x,*z=x,*h=x;\n // TreeNode *d=root,*e=root,*a=root,*b=root;\n int c=1;\n // print(root);cout<<endl;\n traverse(root,x,c);\n // print(x);cout<<endl;\n //print(root);cout<<endl;\n cout<<endl;\n return max(dp(root,x,0),dp(root,x,1));\n }\n};", "memory": "17218" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
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 dfsSize(TreeNode* root)\n {\n if (!root) return 0;\n\n int left = dfsSize(root->left);\n int right = dfsSize(root->right);\n\n return left + right + 1;\n }\n\n int dfs(TreeNode* root, vector<vector<int>> &dp, int &c)\n {\n if (!root) return 0;\n\n int node = c;\n dp[node][1] = root->val;\n\n if (root->left)\n {\n int child = dfs(root->left, dp, ++c);\n\n dp[node][1] += dp[child][0];\n dp[node][0] += max(dp[child][0], dp[child][1]);\n }\n\n if (root->right)\n {\n int child = dfs(root->right, dp, ++c);\n\n dp[node][1] += dp[child][0];\n dp[node][0] += max(dp[child][0], dp[child][1]);\n }\n\n return node;\n }\n\n int rob(TreeNode* root) \n {\n int n = dfsSize(root);\n\n vector<vector<int>> dp(n + 1, vector<int> (2, 0));\n\n n = 0; \n dfs(root, dp, n);\n\n return max(dp[0][0], dp[0][1]);\n }\n};", "memory": "17429" }