id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long dfsTraversal(unordered_map<int, vector<int>> & record_map, int & seats, int node_val, int prev_node_val, long long & res_min_cnter)\n {\n //=======================================================================//\n // Input type: //\n // - unordered_map<int, vector<int>> ref. (record dictionary, hashmap) //\n // - int ref. (total seats) //\n // - int (current node value) //\n // - int (previous node value) //\n // - long long ref. (result minimun summary counter) //\n // Return type: //\n // - long long (result minimun summary counter) //\n //=======================================================================//\n\n /*Initialize*/\n ///// Result people, representations /////\n long long res_people = 1;\n\n /*Whole process, flow*/\n for (int sub_val: record_map[node_val]) //Whole\n {\n ///// Check if the current indexed-value is previous or not /////\n if (sub_val == prev_node_val) { continue; }\n else\n {\n res_people += dfsTraversal(record_map, seats, sub_val, node_val, res_min_cnter); //Recursion function call\n }\n\n } //Whole\n\n ///// Check if the current node-value is capital type or not /////\n if (node_val)\n {\n ///// Check if the current divisions matched conditions or not /////\n if ((res_people % seats)) { res_min_cnter += ((res_people / seats) + 1); } //Update, Accumulate\n else { res_min_cnter += (res_people / seats); } //Update, Accumulate\n }\n\n else { ; }\n\n return res_people;\n }\n\n\n long long minimumFuelCost(vector<vector<int>> & roads, int seats) \n {\n //================================================//\n // Input type: //\n // - vector<vector<int>> ref. (roads vector) //\n // - int (total seats) //\n // Return type: //\n // - long long (result minimun summary counter) //\n //================================================//\n if (! (roads.size())) { return 0; } //Issue, Boundary-case handling\n\n /*Initialize*/\n ///// Length of roads vector /////\n int len_roads = roads.size();\n\n ///// Record main, sub-node values /////\n int record_main_node_val, record_sub_node_val;\n\n ///// Record dictionary, hashmap /////\n unordered_map<int, vector<int>> record_map;\n\n ///// Result minimun summary counter /////\n long long res_min_cnter = 0;\n\n\n /*Recursion-based DFS loop traversal with recorded dictionary, hashmap*/\n ///// Step 1: Record indexed-value with dictionary, hashmap /////\n for (int roads_idx = 0; (roads_idx < len_roads); (roads_idx++)) //Whole\n {\n record_main_node_val = (roads[roads_idx])[0]; //Record main-node value\n record_sub_node_val = (roads[roads_idx])[1]; //Record sub-node value\n\n (record_map[record_main_node_val]).push_back(record_sub_node_val); //Keep updating, recording\n (record_map[record_sub_node_val]).push_back(record_main_node_val); //Keep updating, recording\n\n } //Whole\n\n ///// Step 2: Looped-traversal with recorded hashmap /////\n int res_people = dfsTraversal(record_map, seats, 0, (-1), res_min_cnter); //Resursion function call\n\n return res_min_cnter;\n }\n};", "memory": "225019" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long fuel = 0;\n\n long long dfs(unordered_map<int, vector<int>>& adj_list, int parent, int node, int seats) {\n long long answer = 1;\n\n for (int neighbor : adj_list[node]) {\n if (neighbor == parent)\n continue;\n\n long long people = dfs(adj_list, node, neighbor, seats);\n fuel += ceil(people / (1.0 * seats));\n answer += people;\n }\n\n return answer;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int, vector<int>> adj_list;\n for (vector<int>& r : roads) {\n adj_list[r[0]].push_back(r[1]);\n adj_list[r[1]].push_back(r[0]);\n }\n\n dfs(adj_list, -1, 0, seats);\n return fuel;\n }\n};\n", "memory": "225019" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nlong long dfs(int i,unordered_map<int,vector<int>> &adj,vector<int> &vis,int &s,long long &f){\n vis[i] = 1;\n int cnt = 0;\n int cars = 0;\n for(auto j: adj[i]){\n if(!vis[j]){\n int val = dfs(j,adj,vis,s,f);\n cnt += val;\n if(val%s==0) cars += val/s;\n else cars += ceil((double)val/(double)s);\n }\n }\n\n f += cars;\n return cnt+1;\n} \n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size()+1;\n unordered_map<int,vector<int>> adj;\n for(int i=0;i<n-1;i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n vector<int> vis(n,0);\n long long f = 0;\n long long val = dfs(0,adj,vis,seats,f);\n return f;\n }\n};", "memory": "226899" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n pair<long long,long long> solve(int node,int par,map<int,vector<int>>& adjlist,int seats){\n long long petrol=0;\n long long person=0;\n for(auto neigh:adjlist[node]){\n if(neigh!=par){\n auto temp=solve(neigh,node,adjlist,seats);\n petrol+=temp.first;\n person+=temp.second;\n }\n }\n person++;\n long long porsche=((person-1)/seats)+1;\n return {petrol+porsche,person};\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long ans=0;\n map<int,vector<int>>adjlist;\n for(int i=0;i<roads.size();i++){\n adjlist[roads[i][0]].push_back(roads[i][1]);\n adjlist[roads[i][1]].push_back(roads[i][0]);\n }\n auto temp=solve(0,-1,adjlist,seats);\n long long k=((temp.second-1)/seats)+1;\n return temp.first-k;\n }\n};", "memory": "226899" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=0;\n for(auto road:roads){\n for(auto i:road) {\n n=max(n,i);\n }\n }\n vector<int> ind(n+1,0);\n vector<int> adj[n+1];\n for(auto road:roads){\n ind[road[0]]++;\n ind[road[1]]++;\n adj[road[0]].push_back(road[1]);\n adj[road[1]].push_back(road[0]);\n }\n\n queue<int> q;\n for(int i=1;i<n+1;i++){\n if(ind[i]==1) {\n q.push(i);\n }\n // ind[i]=0;\n }\n // vector<int> vis(n+1,0);\n unordered_map<int,long long> m;\n long long ans=0;\n while(!q.empty()){\n auto f=q.front();\n cout << f << \" f\" << endl;\n q.pop();\n ind[f]=1;\n for(auto nbr:adj[f]){\n ind[nbr]--;\n if(ind[nbr] == 1 && nbr!=0){\n q.push(nbr);\n ind[nbr]=1;\n }\n m[nbr] += (m[f]+1); \n }\n ans += ceil((double)(m[f]+1)/(double)seats);\n cout << ans << \" \" << m[f] << endl;\n }\n return ans;\n }\n};", "memory": "228779" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int, vector<int>> adj;\n for (vector<int>& r : roads) {\n adj[r[0]].push_back(r[1]);\n adj[r[1]].push_back(r[0]);\n }\n\n long long res = 0; // Use long long to handle large results\n\n function<int(int, int)> dfs = [&](int node, int parent) -> int {\n int passengers = 0;\n for (const int& child : adj[node]) {\n if (child == parent) \n continue;\n \n int p = dfs(child, node);\n passengers += p;\n res += (long long)ceil((double)p / seats); // Use floating-point division and cast to long long\n }\n return passengers + 1; // Include the current node's representative\n };\n\n dfs(0, -1);\n return res;\n }\n};\n", "memory": "228779" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define ll long long int\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<vector<int>> adj(n);\n for(auto e : roads){\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n vector<int> sz(n);\n vector<ll> cars(n);\n ll res = 0; \n auto dfs = [&](auto self, int u, int par)->void {\n sz[u] = 1;\n for(auto v : adj[u]){\n if(v == par) continue;\n self(self, v, u);\n sz[u] += sz[v]; \n cars[v] = (sz[v] + seats - 1) / seats; \n res += cars[v]; \n }\n };\n dfs(dfs, 0, -1); \n return res;\n }\n};\n", "memory": "230659" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define ll long long int\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<vector<int>> adj(n);\n for(auto e : roads){\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n vector<int> sz(n);\n vector<ll> cars(n);\n ll res = 0; \n auto dfs = [&](auto self, int u, int par)->void {\n sz[u] = 1;\n for(auto v : adj[u]){\n if(v == par) continue;\n self(self, v, u);\n sz[u] += sz[v]; \n cars[v] = (sz[v] + seats - 1) / seats; \n res += cars[v]; \n }\n };\n dfs(dfs, 0, -1); \n return res;\n }\n};\n", "memory": "230659" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long ans=0;\n vector<bool> vis;\n unordered_map<int,vector<int>> adj;\n \n int dfs(int s,int seats){\n vis[s] = true;\n int count = 1;\n for(auto u: adj[s]){\n if(!vis[u]){\n count+= dfs(u,seats);\n }\n }\n \n if (s != 0) { \n long long cars = ceil(count / (seats * 1.0));\n ans += cars;\n }\n \n return count;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \n int n = roads.size();\n vis.assign(n+1,false);\n for(int i = 0;i<n;i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n dfs(0,seats);\n return ans;\n }\n};", "memory": "232539" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#define ll long long \nclass Solution {\npublic:\n vector<ll>total;\n ll recursiveDFS(int v,vector<bool>&visited,vector<vector<int>>&adj)\n {\n ll count=1;\n for(auto&i:adj[v])\n {\n if(!visited[i])\n {\n visited[i]=true;\n ll curr=recursiveDFS(i,visited,adj);\n count+=(ll)curr;\n }\n }\n total[v]=count;\n return count;\n }\n\n int helper(vector<vector<int>>& roads)\n {\n set<int>st;\n for(auto&i:roads)\n {\n st.insert(i[0]);\n st.insert(i[1]);\n }\n return st.size();\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \n int n=helper(roads);\n if(n==0)\n return 0;\n \n vector<vector<int>> adj(n,vector<int>());\n for(auto&i:roads)\n {\n adj[i[0]].push_back(i[1]);\n adj[i[1]].push_back(i[0]);\n }\n vector<bool>visited(n,false);\n total.resize(n,0);\n visited[0]=true;\n total[0]=recursiveDFS(0,visited,adj);\n\n ll ans=0;\n for(int i=1;i<n;i++)\n {\n ll curr=(ll)total[i]/seats;\n if(total[i]%seats>0)\n curr+=(ll)1;\n ans+=(ll)curr;\n }\n return ans;\n\n }\n};", "memory": "232539" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<int, list<int>> adj;\n\n pair<int, int> dfs(int node, int parent, int seats, long long& totalFuel) {\n int people = 1; // Start with one representative from this city\n int fuel = 0;\n\n for (int neighbor : adj[node]) {\n if (neighbor != parent) {\n auto [subPeople, subFuel] = dfs(neighbor, node, seats, totalFuel);\n people += subPeople; // Add the representatives from the subtree\n totalFuel += subFuel; // Add the fuel cost from the subtree\n }\n }\n\n // If it's not the capital city, calculate the fuel needed to send representatives to the parent\n if (node != 0) {\n // Number of cars needed to transport 'people' representatives to the parent city\n int cars = (people + seats - 1) / seats;\n fuel = cars; // Each car makes one trip to the parent\n }\n\n return {people, fuel};\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n adj.clear();\n for (const vector<int>& road : roads) {\n int u = road[0];\n int v = road[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n long long totalFuel = 0;\n dfs(0, -1, seats, totalFuel);\n\n return totalFuel;\n\n }\n};", "memory": "234419" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long f(unordered_map<int,list<int>> &adj,int node,int parent,long long &cost,int seats){\n\n long long ans=1;\n for(auto it:adj[node]){\n if(it!=parent){\n ans+=f(adj,it,node,cost,seats);\n }\n }\n if(node!=0){\n cost+=(ans/seats);\n if(ans%seats!=0){\n cost++;\n }\n }\n //cout<<cost<<endl;\n return ans;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int,list<int>> adj;\n for(int i=0;i<roads.size();i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n long long cost=0;\n long long ans=f(adj,0,-1,cost,seats);\n\n return cost;\n }\n};", "memory": "236299" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<long long> dfs(vector<int> adj[], int node, int seats, vector<int> &vis, long long &fuel) {\n \n vis[node] = 1; long long carsUsed = 0, seatsFilled = 0;\n for(auto nbr: adj[node]) {\n if(!vis[nbr]) {\n auto nbrDets = dfs(adj, nbr, seats, vis, fuel);\n carsUsed += nbrDets[0];\n seatsFilled += nbrDets[1];\n }\n }\n // cout<<node<<\" \"<<carsUsed<<\" \"<<seatsFilled<<\" \"<<fuel<<endl;\n if(node == 0) return {};\n \n int carNeed = ((seatsFilled) / seats) + 1;\n\n fuel += carNeed;\n return {carNeed, seatsFilled + 1};\n\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \n long long fuel = 0; int n = roads.size() + 1;\n vector<int> adj[n];\n for(auto road: roads) {\n \n int nd1 = road[0], nd2 = road[1];\n adj[nd1].push_back(nd2);\n adj[nd2].push_back(nd1);\n }\n vector<int> vis(n, 0);\n dfs(adj, 0, seats, vis, fuel);\n return fuel;\n }\n};", "memory": "236299" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bitset<100001>bs;\n long long Petrol = 0;\n int vini(int node, unordered_map<int, vector<int>>& mp, int seats){\n // if(mp[node].size() == 0) return 0;\n int person = 0;\n for(auto it : mp[node]){\n if(!bs.test(it)){\n bs.set(it);\n person += vini(it, mp, seats);\n }\n }\n person++;\n long long cars = ceil((double)person/(double)seats);\n Petrol += cars;\n return person;\n\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int, vector<int>>mp;\n for(auto it : roads){\n mp[it[1]].push_back(it[0]);\n mp[it[0]].push_back(it[1]);\n }\n int take = 0;\n bs.set(0);\n for(auto it : mp[0]){\n bs.set(it);\n take += vini(it, mp, seats);\n }\n return Petrol;\n }\n};", "memory": "238179" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long ans = 0 ;\n unordered_map<int,vector<int>> adj;\n int dfs(int parent , int node , int st){\n int cnt = 1;\n for(auto nbr : adj[node]){\n if(parent == nbr)continue ;\n \n cnt += dfs(node , nbr , st);\n \n }\n if(node!=0)ans += (cnt + st -1)/st ;\n\n return cnt ;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() +1;\n \n for(auto e : roads){\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n dfs(-1,0,seats);\n return ans;\n }\n\n};", "memory": "238179" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> graph;\n vector<bool> visited;\n int s;\n pair<long long, pair<long long, int>> dfs(int curr){\n if (visited[curr]) return {0,{0, 0}};\n long long totalFuelCost = 0;\n long long totalCars=0;\n long long totalSeats=1;\n visited[curr]=true;\n for (int i = 0; i< graph[curr].size();i++){\n pair<long long, pair<long long, int>> res = dfs(graph[curr][i]);\n totalFuelCost+=res.first;\n totalFuelCost+=res.second.first+min(1, res.second.second);\n totalCars+=res.second.first;\n totalSeats+=res.second.second;\n }\n return {totalFuelCost, {totalCars+(totalSeats/s), (totalSeats%s)}};\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int sz = 0;\n for (auto road : roads){\n sz=max(sz,max(road[0],road[1]));\n }\n ++sz;\n visited.resize(sz, false);\n graph.resize(sz, vector<int> (0));\n s=seats;\n for (auto road : roads){\n graph[road[0]].push_back(road[1]);\n graph[road[1]].push_back(road[0]);\n }\n return dfs(0).first;\n\n }\n};", "memory": "240059" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size()+1;\n vector<vector<int>>adj(n);\n for(int i = 0;i<roads.size();i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n long long res = 0;\n map<int, vector<int>>mp;\n map<int, int>depthmp;\n queue<int>q;\n q.push(0); int cur, l, depth=0;\n mp[0].push_back(0);\n vector<int>visited(n, 0);visited[0]=1;\n while(!q.empty()){\n l = q.size();\n depth++;\n vector<int>temp;\n while(l>0){\n l--;\n cur = q.front(); q.pop();\n depthmp[cur] = depth-1;\n for(int i = 0;i<adj[cur].size();i++){\n if(!visited[adj[cur][i]]){\n temp.push_back(adj[cur][i]);\n q.push(adj[cur][i]);\n visited[adj[cur][i]]=1;\n } \n }\n }\n mp[depth] = temp; \n }\n // cout << depth << \" \";\n // for(auto it=mp.begin();it!=mp.end();it++){\n // cout << it->first << \" \";\n // vector<int>nodes = it->second;\n // for(int i = 0;i<nodes.size();i++)\n // cout << nodes[i] << \" \";\n // cout << \"\\n\";\n // }\n \n int maxdepth = depth-1, node, parnode;\n //cout << depth << \" \";\n vector<int>children(n, 1); int req=0;\n while(depth>0){\n //process all depth nodes and update their parents ka no. of children \n // if(depth==maxdepth){\n // res+= mp[maxdepth].size();\n // }\n //update parents \n //cout << \"depth= \" << depth << \"\\n\";\n vector<int>curnodes = mp[depth];\n for(int i = 0;i<curnodes.size();i++){\n node = curnodes[i];\n //cout << node << \" \";\n for(int j = 0;j<adj[node].size();j++){\n parnode = adj[node][j];\n if(depthmp[parnode] > depthmp[node])\n continue;\n children[parnode] += children[node];\n }\n //also to travel all children and itself to parent \n //cout << children[node] << \" \";\n req = children[node]/seats;\n if(children[node]%seats)\n req++;\n //cout << req << \"\\n\";\n res += req;\n }\n depth--;\n }\n return res;\n }\n};\n\n// //for me as I wanted to implement a cleaner solution \n// // Above solution was AC in contest :) \n// class Solution {\n// public:\n// int dfs(int src, vector<int>& visited, vector<int>& children, vector<vector<int>>& adj){\n// visited[src]=1;\n// int child = 1;\n// for(int i = 0;i<adj[src].size();i++){\n// if(!visited[adj[src][i]]){\n// child += dfs(adj[src][i], visited, children, adj);\n// }\n// }\n// children[src] = child;\n// return child;\n// }\n// long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n// int n = roads.size()+1;\n// vector<vector<int>>adj(n);\n// for(int i = 0;i<roads.size();i++){\n// adj[roads[i][0]].push_back(roads[i][1]);\n// adj[roads[i][1]].push_back(roads[i][0]);\n// }\n// vector<int>visited(n, 0);\n// vector<int>children(n, 0);\n// dfs(0, visited, children, adj);\n// long long res = 0;\n// for(int i = 1;i<n;i++){\n// // cout << children[i] << \" \";\n// res += (children[i]/seats) + (children[i]%seats? 1: 0);\n// }\n \n// return res;\n// }\n// };", "memory": "240059" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long res = 0;\n long long dfs(int node,int parent,unordered_map<int,vector<int>>& adj,int seats){\n \n long long people = 0;\n for(auto nbr:adj[node]){\n if(nbr!=parent){\n long long p = dfs(nbr,node,adj,seats);\n res+=(p+seats-1)/seats;\n people+=p;\n }\n }\n return people+1;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int,vector<int>> adj;\n\n for(auto road:roads){\n int u = road[0];\n int v = road[1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n \n dfs(0,-1,adj,seats);\n\n return res;\n\n\n }\n};", "memory": "241939" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dfs(int i,int parent,long long &ans,int seats,unordered_map<int,vector<int>> &adj)\n {\n int passengers=0;\n for(auto it:adj[i])\n {\n if(it!=parent)\n {\n int p=dfs(it,i,ans,seats,adj);\n ans+=ceil(static_cast<double>(p) / seats);\n passengers+=p;\n //cout<<passengers<<endl;\n }\n }\n return 1+passengers;\n\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size();\n unordered_map<int,vector<int>> adj;\n for(auto i:roads)\n {\n adj[i[0]].push_back(i[1]);\n adj[i[1]].push_back(i[0]);\n }\n long long ans=0;\n dfs(0,-1,ans,seats,adj);\n\n return ans;\n }\n};", "memory": "241939" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\n private:\n long long dfs(int node, unordered_map<int, list<int>>& adj, vector<bool>& visited, int seats, long long& fuelCost) {\n long long representatives = 1; // Count the representative in the current city\n visited[node] = true;\n\n for (auto it : adj[node]) {\n if (!visited[it]) {\n representatives += dfs(it, adj, visited, seats, fuelCost);\n }\n }\n\n if (node != 0) {\n fuelCost += (representatives + seats - 1) / seats;\n }\n\n return representatives;\n }\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int, list<int>> adj;\n int n = roads.size() + 1;\n for(int i =0; i < roads.size(); ++i){\n int u = roads[i][0];\n int v = roads[i][1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n vector<bool> visited(n, false);\n long long fuelCost = 0;\n dfs(0, adj,visited, seats, fuelCost);\n\n return fuelCost;\n }\n};", "memory": "243819" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> graph;\n vector<bool> visited;\n int s;\n pair<long long, pair<long long, int>> dfs(int curr){\n if (visited[curr]) return {0,{0, 0}};\n long long totalFuelCost = 0;\n long long totalCars=0;\n long long totalSeats=1;\n visited[curr]=true;\n for (int i = 0; i< graph[curr].size();i++){\n if (visited[graph[curr][i]]) continue;\n pair<long long, pair<long long, int>> res = dfs(graph[curr][i]);\n totalFuelCost+=res.first;\n totalFuelCost+=res.second.first+min(1, res.second.second);\n totalCars+=res.second.first;\n totalSeats+=res.second.second;\n }\n return {totalFuelCost, {totalCars+(totalSeats/s), (totalSeats%s)}};\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int sz = 0;\n for (auto road : roads){\n sz=max(sz,max(road[0],road[1]));\n }\n ++sz;\n visited.resize(sz, false);\n graph.resize(sz, vector<int> (0));\n s=seats;\n for (auto road : roads){\n graph[road[0]].push_back(road[1]);\n graph[road[1]].push_back(road[0]);\n }\n return dfs(0).first;\n\n }\n};", "memory": "245699" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<long long,long long> mpp;\nlong long fun(int node,vector<vector<long long>> &adj,vector<long long> &vis){\n vis[node]=1;\n long long sum=1;\n for(auto it: adj[node]){\n if(vis[it]==0)\n sum+=fun(it,adj,vis);\n }\n if(node!=0)\n mpp[node]=sum;\n return sum;\n}\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long n=roads.size();\n vector<vector<long long>> adj(n+1);\n for(auto it: roads){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<long long> vis(n+1,0);\n fun(0,adj,vis);\n long long sum=0;\n for(auto it: mpp){\n if(it.second%seats==0)\n sum=sum+it.second/seats;\n else\n sum=sum+(1+it.second/seats); \n } \n return sum;\n }\n};", "memory": "247579" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int, vector<int>> adj;\n for (const auto& r : roads) {\n adj[r[0]].emplace_back(r[1]);\n adj[r[1]].emplace_back(r[0]);\n }\n unordered_set<int> visited;\n queue<int> q;\n q.push(0);\n while(!q.empty()) {\n int city = q.front();\n q.pop();\n visited.insert(city);\n for(auto it = adj[city].begin(); it != adj[city].end();) {\n if (visited.contains(*it)) {\n it = adj[city].erase(it);\n } else {\n q.push(*it);\n ++it;\n }\n }\n }\n\n // for (int i = 0; i < adj.size(); ++i) {\n // for (int j = 0; j < adj[i].size(); ++j) {\n // cout << adj[i][j] << \",\";\n // }\n // cout << endl;\n // }\n\n auto [cost, num] = dfs(0, adj, seats);\n return cost;\n }\n \n\n pair<int64_t, int32_t> dfs(int node, unordered_map<int, vector<int>>& adj, int s) {\n int64_t totalCost = 0;\n int32_t totalNum = 1;\n for (int i = 0; i < adj[node].size(); ++i) {\n const auto [cost, num] = dfs(adj[node][i], adj, s);\n totalCost += cost + (num + s - 1) / s;\n totalNum += num;\n }\n\n return {totalCost, totalNum};\n }\n};", "memory": "247579" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long ans=0;\n long long solve(unordered_map<int,vector<int>> & m,int n,int pn,int seats){\n vector<int> t=m[n];\n if(t.size()==1 && n){\n ans++;\n return 1;\n }\n long long cnt=0;\n for(auto it: t){\n if(it!=pn){\n cnt+=(solve(m,it,n,seats));\n }\n }\n cnt++;\n if(n){\n ans=ans+(cnt/seats)+((cnt%seats) !=0);\n }\n return cnt;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int,vector<int>> m;\n for(int i=0;i<roads.size();i++){\n m[roads[i][0]].push_back(roads[i][1]);\n m[roads[i][1]].push_back(roads[i][0]);\n }\n int y=solve(m,0,-1,seats);\n return ans;\n }\n};", "memory": "249459" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\n long long postOrder(unordered_map<int, vector<int>>& adj, int node, int pnode, int& seats, long long& ans) {\n vector<int> nbr = adj[node];\n if(nbr.size()==1 && node!=0){\n ans++;\n return 1ll;\n }\n long long count = 0ll;\n for(auto itr= nbr.begin(); itr!=nbr.end(); itr++) {\n if(*itr != pnode) {\n long long peopleAtCurrNode = postOrder(adj, *itr, node, seats, ans);\n count += peopleAtCurrNode;\n }\n }\n count++;\n if(node) {\n ans += (count/seats) + (count%seats != 0);\n }\n return count;\n }\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long ans = 0;\n unordered_map<int, vector<int>> adj;\n int n = roads.size();\n for(int i = 0; i<n; i++) {\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n postOrder(adj, 0, -1, seats, ans);\n return ans;\n }\n};", "memory": "249459" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<int, vector<int>> adj;\n void fill(vector<vector<int>>& roads) {\n for (auto i : roads) {\n int u = i[0], v = i[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n return;\n }\n long long dfs(vector<bool>& vis, int seats, long long& fuelCost, int node = 0) {\n vis[node] = true;\n int representatives = 1;\n\n for (auto it : adj[node]) {\n if (!vis[it]) {\n representatives += dfs(vis, seats, fuelCost, it);\n }\n }\n if (node != 0) {\n fuelCost += (representatives + seats - 1) / seats;\n }\n\n return representatives;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1; \n adj = map<int, vector<int>> ();\n fill(roads);\n\n vector<bool> vis(n, false);\n long long ans = 0;\n\n dfs(vis, seats, ans);\n\n return ans;\n }\n};", "memory": "251339" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long dfs(unordered_map<int , vector<int>>&adj , vector<bool>&vis , long long& ans , int u , int seats){\n vis[u] = true;\n long long cnt = 1;\n for(auto v : adj[u]){\n if(vis[v] == false){\n cnt += dfs(adj , vis , ans , v , seats);\n }\n }\n long long x = cnt/seats;\n if(cnt%seats) x++;\n if(u != 0) ans += x;\n return cnt;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int , vector<int>>adj;\n int n = roads.size();\n for(auto i : roads){\n int u = i[0];\n int v = i[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n vector<bool>vis(n+1 , false);\n long long ans = 0;\n dfs(adj , vis , ans , 0 ,seats);\n\n return ans;\n\n }\n};", "memory": "251339" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long count ;\n long long solve(unordered_map<int,list<int>>&adj,int s,int node,int parent){\n int child=0;\n for(auto i:adj[node]){\n if(i!=parent){\n child+=solve(adj,s,i,node);\n \n \n \n \n \n } }\n double car= ceil((1.0 + (double)child)/(double)s);\n \n if(node)\n count+=car;\n return 1+child;\n \n \n }\n\n \n \n \n \n \n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int,list<int>>adj;\n for(auto i: roads){\n adj[i[0]].push_back(i[1]);\n adj[i[1]].push_back(i[0]); \n \n \n }\n count=0;\n solve(adj,seats,0,-1);\n return count;\n }\n};", "memory": "253219" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n int seat;\n int dfs(int v, unordered_map<int,vector<int>>&adj, unordered_set<int>&visited, long long &fuel){\n\n visited.insert(v);\n\n int people = 1;\n\n for(auto &child : adj[v]){\n\n if(visited.find(child) == visited.end()){\n people += dfs(child, adj, visited, fuel);\n }\n }\n\n if(v != 0){\n\n fuel += (people + seat - 1) / seat;\n }\n \n return people;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n seat = seats;\n\n unordered_map<int, vector<int>>adj;\n\n for(auto &v : roads){\n\n adj[v[0]].push_back(v[1]);\n\n adj[v[1]].push_back(v[0]);\n }\n long long fuel = 0;\n unordered_set<int>visited;\n dfs(0, adj, visited, fuel);\n\n return fuel;\n }\n};", "memory": "253219" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n // make tree\n unordered_map<int, vector<int>> tree;\n for (const vector<int>& road : roads) {\n tree[road[0]].push_back(road[1]);\n tree[road[1]].push_back(road[0]);\n }\n\n // DFS!!\n unordered_set<int> visited;\n return fuelCost(tree, visited, 0, seats).first;\n }\nprivate:\n pair<long long, int> fuelCost(unordered_map<int, vector<int>>& tree, unordered_set<int>& visited, int city, int seats) {\n visited.insert(city);\n int passengers = 0;\n long long fuel = 0LL;\n for (const int nei : tree[city]) {\n if (visited.count(nei) == 0) {\n pair<long long, int> res = fuelCost(tree, visited, nei, seats);\n fuel += res.first;\n passengers += res.second;\n }\n }\n if (city == 0) {\n cout << city << ' ' << fuel << ' ' << passengers << '\\n';\n return make_pair(fuel, passengers);\n }\n ++passengers;\n fuel += passengers / seats + int(passengers % seats != 0);\n cout << city << ' ' << fuel << ' ' << passengers << '\\n';\n return make_pair(fuel, passengers);\n }\n};\n", "memory": "255099" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n // make tree\n unordered_map<int, vector<int>> tree;\n for (const vector<int>& road : roads) {\n tree[road[0]].push_back(road[1]);\n tree[road[1]].push_back(road[0]);\n }\n\n // DFS!!\n unordered_set<int> visited;\n return fuelCost(tree, visited, 0, seats).first;\n }\nprivate:\n pair<long long, int> fuelCost(unordered_map<int, vector<int>>& tree, unordered_set<int>& visited, int city, int seats) {\n visited.insert(city);\n int passengers = 0;\n long long fuel = 0LL;\n for (const int nei : tree[city]) {\n if (visited.count(nei) == 0) {\n pair<long long, int> res = fuelCost(tree, visited, nei, seats);\n fuel += res.first;\n passengers += res.second;\n }\n }\n if (city == 0) {\n return make_pair(fuel, passengers);\n }\n ++passengers;\n fuel += passengers / seats + int(passengers % seats != 0);\n return make_pair(fuel, passengers);\n }\n};\n", "memory": "255099" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long ans = 0, n = roads.size()+1;\n if(n == 0) return 0;\n vector<vector<long long>> adj(n, vector<long long>());\n vector<long long> ending, visited(n, 0), parent(n, -1), count(n, 1); \n vector<vector<long long>> distances; \n\n for(auto & i : roads) adj[i[0]].push_back(i[1]), adj[i[1]].push_back(i[0]);\n for(int i = 0; i < n; i++) if(adj[i].size() == 1 && i != 0) ending.push_back(i);\n queue<vector<long long>> q; q.push({0, 0});\n while(!q.empty()){\n long long cur = q.front()[0], d = q.front()[1]; q.pop();\n if(visited[cur] == 1) continue; visited[cur] = 1;\n distances.push_back({-d, cur});\n for(auto i : adj[cur]) {\n if(visited[i]) continue;\n parent[i] = cur;\n q.push({i, d+1});\n }\n }\n sort(begin(distances), end(distances));\n for(auto v : distances) if(v[1] != 0) count[parent[v[1]]] += count[v[1]];\n for(int i = 1; i < n; i++) ans += ceil( (long double) count[i] / (long double) seats);\n return ans;\n }\n};", "memory": "266379" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long ans = 0, n = roads.size()+1;\n if(n == 0) return 0;\n vector<vector<long long>> adj(n, vector<long long>());\n vector<long long> ending;\n vector<long long> visited(n, 0), parent(n, -1), count(n, 1); count[0] = 0;\n vector<vector<long long>> distances; \n\n for(auto & i : roads) adj[i[0]].push_back(i[1]), adj[i[1]].push_back(i[0]);\n for(int i = 0; i < n; i++) if(adj[i].size() == 1 && i != 0) ending.push_back(i);\n queue<vector<long long>> q; q.push({0, 0});\n while(!q.empty()){\n long long cur = q.front()[0], d = q.front()[1]; q.pop();\n if(visited[cur] == 1) continue; visited[cur] = 1;\n distances.push_back({-d, cur});\n for(auto i : adj[cur]) {\n if(visited[i]) continue;\n parent[i] = cur;\n q.push({i, d+1});\n }\n }\n sort(begin(distances), end(distances));\n for(auto v : distances) if(v[1] != 0) count[parent[v[1]]] += count[v[1]];\n for(int i = 1; i < n; i++) ans += ceil( (long double) count[i] / (long double) seats);\n return ans;\n }\n};", "memory": "266379" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // long long dfs(unordered_map<int, vector<int>> adj, vector<int> &visited, int source, long long &ans, int seats) {\n\n // visited[source] = 1;\n\n // long long count = 1;\n // for(auto x: adj[source]) {\n // if(!visited[x]) {\n // count += dfs(adj, visited, x, ans, seats);\n // }\n // }\n\n // long long temp = (count + seats - 1) / seats; // calculating ceiling of (count / seats)\n\n // if(source != 0) ans += temp;\n // return count;\n\n // }\n\n // int dfs(unordered_map<int, vector<int>> adj, int node, int par, long long &ans, int seats, int people = 1) {\n // for(auto x: adj[node]) {\n // if(x == par) continue;\n // people += dfs(adj, x, node, ans, seats);\n // }\n\n // if(node != 0) ans += (people + seats - 1) / seats;\n\n // return people;\n // }\n\n long long fuel = 0;\n int seats;\n\n int dfs(int src, unordered_map<int, vector<int>>& adjlist, set<int>& visit)\n {\n if (visit.find(src) != visit.end())\n return 0;\n \n visit.insert(src);\n int subtree = 1;\n for (auto neigh: adjlist[src])\n {\n subtree += dfs(neigh, adjlist, visit);\n }\n\n if (src != 0)\n {\n fuel += subtree % seats == 0 ? subtree / seats : subtree / seats + 1;\n }\n\n return subtree;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n // if we know the number of people at each node then we can get the fuel count needed to go to the just parent node.\n // we can find this by ceil(num_of_people / seats)\n // so we recursively do this for all nodes.\n\n // int n = roads.size() + 1;\n\n // unordered_map<int, vector<int>> adj;\n // for(auto x: roads) {\n // int u = x[0];\n // int v = x[1];\n\n // adj[u].push_back(v);\n // adj[v].push_back(u);\n // } \n\n // vector<int> visited(n, 0);\n // long long ans = 0;\n\n // // dfs(adj, visited, 0, ans, seats);\n // dfs(adj, 0, -1, ans, seats);\n\n // return ans;\n\n unordered_map<int, vector<int>> adjlist;\n for (auto road: roads)\n {\n adjlist[road[0]].push_back(road[1]);\n adjlist[road[1]].push_back(road[0]);\n }\n\n set<int> visit;\n this->seats = seats;\n\n dfs(0, adjlist, visit);\n\n return fuel;\n\n }\n};", "memory": "268259" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int, vector<int>> m;\n\n for(auto road: roads){\n m[road[0]].push_back(road[1]);\n m[road[1]].push_back(road[0]);\n }\n\n unordered_set<int> vis;\n vis.insert(0);\n\n long long fuel = 0;\n for(auto nei: m[0]){\n dfs(nei, m, vis, seats, fuel);\n }\n \n return fuel;\n }\n\n long long dfs(int city, unordered_map<int, vector<int>> &m, unordered_set<int> &vis, int cap, long long &fuel){\n vis.insert(city);\n\n long long noOfPas = 1;\n for(auto nei: m[city]){\n if(!vis.count(nei)){\n long long passengers = dfs(nei, m, vis, cap, fuel);\n noOfPas += passengers;\n }\n }\n \n long long noOfCars = noOfPas / cap;\n\n if(noOfPas%cap)noOfCars++;\n\n fuel += noOfCars;\n\n return noOfPas;\n }\n};", "memory": "270139" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int sz = roads.size()+1;\n vector<unordered_set<int>> g(sz);\n for(auto e : roads)\n {\n g[e[0]].insert(e[1]);\n g[e[1]].insert(e[0]);\n }\n queue<int> q;\n vector<long long> cap(sz);\n for(int i=0;i<sz;i++) cap[i]={1};\n long long r= 0;\n for(int i=1;i<sz;i++) if(g[i].size()<=1) q.push(i);\n while(!q.empty())\n {\n int el = q.front();\n q.pop();\n int root = *g[el].begin();\n g[el].erase(root);\n g[root].erase(el);\n if(g[root].size()==1 && root!=0) q.push(root);\n cap[root]+=cap[el];\n r+=(cap[el]%seats!=0)+cap[el]/seats;\n }\n return r;\n }\n};", "memory": "272019" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "long long prune(vector<vector<int>>& edges, int sz, int seat)\n{\n vector<unordered_set<int>> g(sz);\n for(auto e : edges)\n {\n g[e[0]].insert(e[1]);\n g[e[1]].insert(e[0]);\n }\n queue<int> q;\n vector<long long> cap(sz);\n for(int i=0;i<sz;i++) cap[i]={1};\n long long r= 0;\n for(int i=1;i<sz;i++) if(g[i].size()<=1) q.push(i);\n while(!q.empty())\n {\n int el = q.front();\n q.pop();\n int root = *g[el].begin();\n g[el].erase(root);\n g[root].erase(el);\n if(g[root].size()==1 && root!=0) q.push(root);\n cap[root]+=cap[el];\n r+=(cap[el]%seat!=0)+cap[el]/seat;\n }\n return r;\n}\n\nclass Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n return prune(roads, roads.size()+1, seats);\n }\n};", "memory": "273899" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\nbool check (int x , vector<vector<char>>& mat , int i , int j) {\n int n = mat.size();\n int m = mat[0].size();\n if (i + max(0 , x - 1) >= n || j + max(0 , x - 1) >= m) return false;\n for (int l = i; l < i + x; l++) {\n for (int r = j; r < j + x; r++) {\n if (mat[l][r] == '0') return false;\n }\n }\n return true;\n}\n int maximalSquare(vector<vector<char>>& mat) {\n int n = mat.size();\n int m = mat[0].size();\n int x = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n while (check (x , mat , i , j) == true) {\n x++;\n }\n }\n }\n return (x - 1) * (x - 1);\n }\n};", "memory": "22100" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int row = matrix.size();\n int col = matrix[0].size();\n bool formed = false;\n int ret = 0;\n int i, j;\n for (i = 0; i < row; i ++)\n {\n for (j = 0; j < col; j ++)\n if (matrix[i][j] == '1')\n break;\n if (j != col)\n break;\n }\n if (i == row)\n return 0;\n \n while (true)\n {\n formed = false;\n for (int i = 1; i < row; i ++)\n {\n for (int j = 1; j < col; j ++)\n {\n if (matrix[i-1][j-1]=='1'&& matrix[i-1][j]== '1'&& matrix[i][j-1]=='1'&&matrix[i][j]=='1')\n {\n matrix[i-1][j-1] = '1';\n formed = true;\n }\n else\n {\n matrix[i-1][j-1] = '0';\n }\n }\n }\n std::cout << \"formed:\" << formed << std::endl; \n if (!formed)\n {\n break;\n }\n row --;\n col --;\n }\n return (matrix.size() - row + 1) * (matrix.size() - row + 1);\n }\n};", "memory": "22200" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "#pragma GCC optimize (\"Ofast\")\n#pragma GCC optimize (\"O3\", \"unroll-loops\", \"-ffloat-store\")\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\n\n// \"_\": A lambda function to enable faster I/O operations\nauto _=[]()noexcept{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();\n\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\n// 1D DP: [O(m*n), O(n)]\nclass Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int m = matrix.size();\n int n = matrix[0].size();\n\n if (m == 0 || n == 0) return 0;\n\n vector<int> dp(n + 1, 0);\n int max_side = 0;\n int prev = 0; // store dp[j-1]\n\n for (int i = 1; i < m + 1; i++) {\n for (int j = 1; j < n + 1; j++) {\n int temp = dp[j];\n if (matrix[i - 1][j - 1] == '1') {\n dp[j] = min({dp[j], dp[j - 1], prev}) + 1;\n max_side = max(max_side, dp[j]);\n } else {\n dp[j] = 0;\n }\n\n prev = temp;\n }\n }\n\n return max_side * max_side;\n }\n};", "memory": "22300" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n// int solve(vector<vector<char>>& matrix,int row,int col,int &maxi,vector<vector<int>>&dp){\n// if(row>=matrix.size() || col>=matrix[0].size()){\n// return 0;\n// }\n// if(dp[row][col]!=-1){\n// return dp[row][col];\n// }\n \n// int down=solve(matrix,row+1,col,maxi,dp);\n// int diagonal=solve(matrix,row+1,col+1,maxi,dp);\n// int right=solve(matrix,row,col+1,maxi,dp);\n// if(matrix[row][col]=='1'){\n// int ans=1+min(right,min(diagonal,down));\n// maxi=max(ans,maxi);\n// dp[row][col]=ans;\n// return ans;\n// }\n// else{\n// return 0;\n// }\n \n// }\n\n\n\n// void solvetab(vector<vector<char>>& matrix,int row,int col,int &maxi){\n// vector<vector<int>>dp(row+1,vector<int>(col+1,0));\n// for(int i=row-1;i>=0;i--){\n// for(int j=col-1;j>=0;j--){\n// int down=dp[i+1][j];\n// int diagonal=dp[i+1][j+1];\n// int right=dp[i][j+1];\n// if(matrix[i][j]=='1'){\n// int ans=1+min(right,min(diagonal,down));\n// maxi=max(ans,maxi);\n// dp[i][j]=ans;\n// }\n// }\n \n// }\n\n \n\n// }\nvoid solveSO(vector<vector<char>>& matrix, int &maxi) {\n int row = matrix.size();\n int col = matrix[0].size();\n\n vector<int> next(col + 1, 0); // Represents the next row in the dp array\n\n // Iterate from the bottom-right to top-left\n for (int i = row - 1; i >= 0; i--) {\n int right = 0; // Initialize 'right' for the current row\n for (int j = col - 1; j >= 0; j--) {\n int newRight = next[j]; // Store the current value of next[j] to update 'right' later\n \n // If the current cell is '1'\n if (matrix[i][j] == '1') {\n int down = next[j]; // Value from the next row (downwards)\n int diagonal = next[j + 1]; // Value from the next row and next column (diagonally)\n\n // Compute the size of the square that can be formed\n int ans = 1 + min({right, diagonal, down});\n \n // Update the maximum square size found so far\n maxi = max(ans, maxi);\n\n // Update the current dp cell\n next[j] = ans;\n\n // Update the right value to be used in the next iteration (previous column in the current row)\n right = newRight;\n } else {\n // If matrix[i][j] is '0', no square can be formed\n next[j] = 0;\n right = 0; // Reset 'right' because no square can be formed\n }\n }\n }\n}\n\n\n int maximalSquare(vector<vector<char>>& matrix) {\n int maxi=0;\n int row=matrix.size();\n int col=matrix[0].size();\n solveSO(matrix,maxi);\n return maxi*maxi;\n }\n};", "memory": "22400" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "#pragma GCC optimize (\"Ofast\")\n#pragma GCC optimize (\"O3\", \"unroll-loops\", \"-ffloat-store\")\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\n\n// \"_\": A lambda function to enable faster I/O operations\nauto _=[]()noexcept{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();\n\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\n// 1D DP: [O(m*n), O(n)]\nclass Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int m = matrix.size();\n int n = matrix[0].size();\n\n if (m == 0 || n == 0) return 0;\n\n vector<int> dp(n + 1, 0);\n int max_side = 0;\n int prev = 0; // store dp[i-1][j-1]\n\n for (int i = 1; i < m + 1; i++) {\n for (int j = 1; j < n + 1; j ++) {\n int temp = dp[j];\n if (matrix[i - 1][j - 1] == '1') {\n dp[j] = min({dp[j], dp[j - 1], prev}) + 1;\n max_side = max(max_side, dp[j]);\n } else {\n dp[j] = 0;\n }\n\n prev = temp;\n }\n }\n\n return max_side * max_side;\n }\n};", "memory": "22500" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "#include <algorithm>\n\nusing namespace std;\nclass Solution {\npublic:\n//Algo one - scan the matrix for squares of size 1x1, 2x2, 3x3...up to min(n,m)*min(n,m); each step takes n*m comparisons, times n. So around O(n3)\n int maximalSquare(vector<vector<char>>& matrix) {\n\n int maxSide = 0; \n //m*n matrix \n int m = matrix.size() - 1;\n int n = matrix[0].size() - 1;\n\n for(int i = 0; i < matrix.size(); i++)\n for(int j = 0; j < matrix[0].size(); j++)\n if(matrix[i][j] == '1') maxSide = 1; \n\n if(m == 0 || n == 0) return maxSide; \n\n vector<vector<char>> small(m, vector<char>(n, '0'));\n while(m >= 1 && n >=1)\n {\n bool hasSq = false; \n for(int r = 0; r < m; r++)\n {\n for(int c = 0; c < n; c++)\n {\n if(matrix[r][c] == '1' && matrix[r+1][c] == '1' && matrix[r][c+1] == '1' && matrix[r+1][c+1] == '1')\n {\n matrix[r][c] = '1'; \n hasSq = true;\n }\n else matrix[r][c] = '0';\n }\n }\n if(hasSq == true) maxSide++; \n\n m--; n--; \n }\n return maxSide*maxSide; \n\n }\n};", "memory": "23200" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "#include <algorithm>\n\nusing namespace std;\nclass Solution {\npublic:\n//Algo one - scan the matrix for squares of size 1x1, 2x2, 3x3...up to min(n,m)*min(n,m); each step takes n*m comparisons, times n. So around O(n3)\n int maximalSquare(vector<vector<char>>& matrix) {\n\n int maxSide = 0; \n //m*n matrix \n int m = matrix.size() - 1;\n int n = matrix[0].size() - 1;\n\n for(int i = 0; i < matrix.size(); i++)\n for(int j = 0; j < matrix[0].size(); j++)\n if(matrix[i][j] == '1') maxSide = 1; \n\n if(m == 0 || n == 0) return maxSide; \n\n vector<vector<char>> small(m, vector<char>(n, '0'));\n while(m >= 1 && n >=1)\n {\n bool hasSq = false; \n for(int r = 0; r < m; r++)\n {\n for(int c = 0; c < n; c++)\n {\n if(matrix[r][c] == '1' && matrix[r+1][c] == '1' && matrix[r][c+1] == '1' && matrix[r+1][c+1] == '1')\n {\n small[r][c] = '1'; \n hasSq = true;\n }\n }\n }\n if(hasSq == true) maxSide++; \n for(int r = 0; r < m; r++){\n for(int c = 0; c < n; c++){\n matrix[r][c] = small[r][c];\n small[r][c] = '0';\n }\n std::cout << endl; \n }\n m--; n--; \n }\n return maxSide*maxSide; \n\n }\n};", "memory": "23300" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "#include <algorithm>\n\nusing namespace std;\nclass Solution {\npublic:\n//Algo one - scan the matrix for squares of size 1x1, 2x2, 3x3...up to min(n,m)*min(n,m); each step takes n*m comparisons, times n. So around O(n3)\n int maximalSquare(vector<vector<char>>& matrix) {\n\n int maxSide = 0; \n //m*n matrix \n int m = matrix.size() - 1;\n int n = matrix[0].size() - 1;\n\n for(int i = 0; i < matrix.size(); i++)\n for(int j = 0; j < matrix[0].size(); j++)\n if(matrix[i][j] == '1') maxSide = 1; \n\n if(m == 0 || n == 0) return maxSide; \n\n vector<vector<char>> small(m, vector<char>(n, '0'));\n while(m >= 1 && n >=1)\n {\n bool hasSq = false; \n for(int r = 0; r < m; r++)\n {\n for(int c = 0; c < n; c++)\n {\n if(matrix[r][c] == '1' && matrix[r+1][c] == '1' && matrix[r][c+1] == '1' && matrix[r+1][c+1] == '1')\n {\n small[r][c] = '1'; \n hasSq = true;\n }\n }\n }\n if(hasSq == true) maxSide++; \n for(int r = 0; r < m; r++){\n for(int c = 0; c < n; c++){\n matrix[r][c] = small[r][c];\n small[r][c] = '0';\n }\n }\n m--; n--; \n }\n return maxSide*maxSide; \n\n }\n};", "memory": "23300" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "int dp[1000][1000], vertical[1000][1000];\n\nclass Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size(), m = matrix[0].size();\n\n for (int i = 0; i < n; i++) {\n dp[i][0] = (matrix[i][0] == '1');\n // cout << \"HEYY \" << matrix[i][0] << endl;\n }\n for (int j = 0; j < m; j++) {\n dp[0][j] = (matrix[0][j] == '1');\n }\n\n for (int i = 1; i < n; i++) {\n for (int j = 1; j < m; j++) {\n if (matrix[i][j] == '0') {\n dp[i][j] = 0;\n } else {\n dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]);\n dp[i][j]++;\n }\n }\n } \n\n int ans = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n // cout << dp[i][j] << ' ';\n ans = max(ans, dp[i][j]);\n }\n // cout << endl;\n }\n\n\n return ans * ans;\n\n }\n};", "memory": "23400" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int rowWidth[505][505]={0};\n for (int i=0;i<matrix.size();i++){\n for (int j=0;j<matrix[i].size();j++){\n if (j==0) rowWidth[i][j]=(matrix[i][j]-'0');\n else{\n if (matrix[i][j]=='0') rowWidth[i][j]=0;\n else rowWidth[i][j]=rowWidth[i][j-1]+1;\n }\n }\n }\n\n int maxArea=0;\n for (int i=0;i<matrix.size();i++){\n for (int j=0;j<matrix[i].size();j++){\n int w=rowWidth[i][j];\n for (int h=i;h>=0;h--){\n w=min(w,rowWidth[h][j]);\n if (w==(i-h+1)) maxArea=max(maxArea,w*(i-h+1));\n }\n }\n }\n\n return maxArea;\n }\n};", "memory": "23500" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "// O((MN)^3)\n// MN for 1st point; MN for 2nd point; MN for traversing b/w 1st and 2nd point\n\n// O((MN)^2)\n// MN for 1st point. MN for side while traversing the second point\n\n// O(MN)\n// dp\nclass Solution {\npublic:\n int n, m;\n int dp[305][305];\n vector<vector<char>> grid;\n\n bool isValid(int i, int j){return i>=0 && j>=0 && i<n && j<m;}\n int calc(int x, int y){\n if(!isValid(x, y)){return 0;}\n if(grid[x][y] == '0'){\n return 0;\n }\n if(x == 0 && y == 0){return 1;}\n if(dp[x][y] != -1){return dp[x][y];}\n return dp[x][y] = 1 + min(calc(x-1, y-1), \\\n min(calc(x-1, y), calc(x, y-1)));\n }\n int maximalSquare(vector<vector<char>>& grid) {\n this->grid = grid;\n n = grid.size();\n m = grid[0].size();\n\n for(int i=0; i<n; i++){\n for(int j=0; j<m; j++){\n dp[i][j] = -1;\n }\n } \n int ans = 0;\n for(int i=0; i<n; i++){\n for(int j=0; j<m; j++){\n ans = max(ans, calc(i, j));\n }\n }\n return ans * ans;\n\n }\n\n\n};", "memory": "23600" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int rowSize = matrix.size(), colSize = matrix[0].size();\n vector<vector<short>> dp(rowSize, vector<short>(colSize, 0));\n int result = 0;\n for (int i = 0; i < rowSize; i++) {\n for (int j = 0; j < colSize; j++) {\n if (matrix[i][j] == '1') {\n if (i && j) \n dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]))+ 1;\n else\n dp[i][j] = 1;\n result = max(result, (int)dp[i][j]);\n }\n }\n }\n for (int i = 0; i < rowSize; i++) {\n for (int j = 0; j < colSize; j++) {\n cout << dp[i][j] << ' ';\n }\n cout << endl;\n }\n return pow(result, 2);\n }\n};", "memory": "23700" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int rowSize = matrix.size(), colSize = matrix[0].size();\n vector<vector<short>> dp(rowSize, vector<short>(colSize, 0));\n int result = 0;\n for (int i = 0; i < rowSize; i++) {\n for (int j = 0; j < colSize; j++) {\n if (matrix[i][j] == '1') {\n if (i && j) \n dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]))+ 1;\n else\n dp[i][j] = 1;\n result = max(result, (int)dp[i][j]);\n }\n }\n }\n return pow(result, 2);\n }\n};", "memory": "23700" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& M) {\n vector<vector<short>> D(M.size(), vector<short>(M[0].size(), 0));\n short ans = 0;\n for(int i = 0; i < M.size(); ++i)\n D[i][0] = M[i][0]=='1',\n ans = max(ans, D[i][0]);\n for(int j = 0; j < M[0].size(); ++j)\n D[0][j] = M[0][j]=='1',\n ans = max(ans, D[0][j]);\n for(int i = 1; i < M.size(); ++i)\n for(int j = 1; j < M[i].size(); ++j)\n if(M[i][j]=='1')\n D[i][j] = min(D[i-1][j-1], min(D[i][j-1], D[i-1][j])) + 1,\n ans = max(ans, D[i][j]);\n return ans*ans;\n }\n};", "memory": "23800" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n#if 1\n int maximalSquare(vector<vector<char>>& matrix) {\n vector<vector<short>> dp(matrix.size(), vector<short>(matrix[0].size()));\n bool exist_one = false;\n for (int i = 0; i < matrix.size(); i++) {\n for (int j = 0; j < matrix[0].size(); j++) {\n dp[i][j] = matrix[i][j] - '0';\n if (dp[i][j]) exist_one = true;\n }\n }\n if (!exist_one) {\n return 0;\n }\n short d = 1;\n for (int i = 1; i < dp.size(); i++) {\n for (int j = 1; j < dp[0].size(); j++) {\n if (dp[i][j]) {\n dp[i][j] = 1+ min({dp[i-1][j], dp[i][j-1], dp[i-1][j-1]});\n d = max(d, dp[i][j]);\n }\n }\n }\n return d*d;\n }\n#else\n int maximalSquare(vector<vector<char>>& matrix) {\n bool exist_one = false;\n for (auto &m:matrix) {\n for (auto &v:m) {\n v -= '0';\n if (v) exist_one = true;\n }\n }\n if (!exist_one) {\n return 0;\n }\n int d = 0;\n bool updated = true;\n while (updated) {\n d++;\n int check = d+1;\n updated = false;\n for (int i = 0; i <= (int)matrix.size()-check; i++) {\n for (int j = 0; j <= (int)matrix[0].size()-check; j++) {\n if (matrix[i][j] && matrix[i][j+1] && matrix[i+1][j] && matrix[i+1][j+1]) {\n matrix[i][j] = 1;\n updated = true;\n } else {\n matrix[i][j] = 0;\n }\n }\n }\n }\n return d*d;\n }\n#endif\n};", "memory": "23800" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "\nclass Solution {\npublic:\n int maximalSquare(vector<vector<char>> &matrix) {\n vector<vector<char>> *otherMatrix = &matrix;\n\n vector<vector<char>> *currentMatrix = new vector<vector<char>>(\n matrix.size(), vector<char>(matrix[0].size(), 0));\n\n int maxSquare = 0;\n for(auto chars : matrix) {\n for(char c: chars) {\n if(c == '1') {\n maxSquare = 1;\n break;\n }\n }\n }\n int rowSize = matrix.size();\n int colSize = matrix[0].size();\n int maxSquareSize = min(rowSize, colSize);\n\n int curSquare = 1;\n while (curSquare < maxSquareSize) {\n // row\n for (int i = 0; i < rowSize - curSquare; i++) {\n for (int j = 0; j < colSize - curSquare; j++) {\n int test = ((*otherMatrix)[i][j] - '0') +\n ((*otherMatrix)[i + 1][j] - '0') +\n ((*otherMatrix)[i + 1][j + 1] - '0') +\n (*otherMatrix)[i][j + 1] - '0';\n if (((*otherMatrix)[i][j] - '0') + ((*otherMatrix)[i + 1][j] - '0') +\n ((*otherMatrix)[i + 1][j + 1] - '0') +\n (*otherMatrix)[i][j + 1] - '0' >=\n 4) {\n (*currentMatrix)[i][j] = '1';\n if (curSquare + 1 > maxSquare)\n maxSquare = curSquare + 1;\n } else {\n (*currentMatrix)[i][j] = '0';\n }\n }\n }\n curSquare++;\n auto temp = otherMatrix;\n otherMatrix = currentMatrix;\n currentMatrix = temp;\n }\n return maxSquare * maxSquare;\n }\n};\n", "memory": "23900" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "\nclass Solution {\npublic:\n int maximalSquare(vector<vector<char>> &matrix) {\n vector<vector<char>> *otherMatrix = &matrix;\n\n vector<vector<char>> *currentMatrix = new vector<vector<char>>(\n matrix.size(), vector<char>(matrix[0].size(), 0));\n\n int maxSquare = 0;\n for(auto chars : matrix) {\n for(char c: chars) {\n if(c == '1') {\n maxSquare = 1;\n break;\n }\n }\n }\n int rowSize = matrix.size();\n int colSize = matrix[0].size();\n int maxSquareSize = min(rowSize, colSize);\n\n int curSquare = 1;\n while (curSquare < maxSquareSize) {\n // row\n for (int i = 0; i < rowSize - curSquare; i++) {\n for (int j = 0; j < colSize - curSquare; j++) {\n int test = ((*otherMatrix)[i][j] - '0') +\n ((*otherMatrix)[i + 1][j] - '0') +\n ((*otherMatrix)[i + 1][j + 1] - '0') +\n (*otherMatrix)[i][j + 1] - '0';\n if (((*otherMatrix)[i][j] - '0') + ((*otherMatrix)[i + 1][j] - '0') +\n ((*otherMatrix)[i + 1][j + 1] - '0') +\n (*otherMatrix)[i][j + 1] - '0' >=\n 4) {\n (*currentMatrix)[i][j] = '1';\n if (curSquare + 1 > maxSquare)\n maxSquare = curSquare + 1;\n } else {\n (*currentMatrix)[i][j] = '0';\n }\n }\n }\n curSquare++;\n auto temp = otherMatrix;\n otherMatrix = currentMatrix;\n currentMatrix = temp;\n }\n return maxSquare * maxSquare;\n }\n};\n", "memory": "23900" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int dp[1000][1000];\n int ans=0, m=matrix.size(), n=matrix[0].size();\n\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n if(i==0 or j==0){\n dp[i][j]=matrix[i][j]-'0';\n }\n else if(matrix[i][j]-'0'==1){\n dp[i][j]=1+min({dp[i][j-1], dp[i-1][j], dp[i-1][j-1]});\n }\n else{\n dp[i][j]=0;\n }\n ans=max(ans,dp[i][j]);\n }\n }\n\n return ans*ans;\n }\n};", "memory": "24000" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n\n int height = (int)matrix.size();\n int width = height ? (int)matrix[0].size() : 0;\n vector<int> temp(width * height);\n\n int minSide = 0;\n for (int i = 0; i < height; ++i)\n {\n for (int j = 0; j < width; ++j)\n {\n int val = matrix[i][j] - '0';\n temp[i * width + j] = val;\n\n if (minSide < val) minSide = val;\n\n if (i == 0 || j == 0)\n {\n continue;\n }\n\n if (val)\n {\n int m = std::min(std::min(temp[(i - 1) * width + j], temp[i * width + j - 1]), temp[(i - 1) * width + j - 1]) + 1;\n temp[i * width + j] = m;\n if (minSide < m)\n {\n minSide = m;\n }\n }\n }\n }\n\n return minSide * minSide;\n }\n};", "memory": "24100" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "int maximal_square(std::vector<std::vector<char>> const &grid) {\n auto const n = grid[0].size();\n std::vector<int> result(grid.size() * n);\n int max = 0;\n {\n for (auto j = 0uz; j < grid[0].size(); ++j) {\n if (grid[0uz][j] == '1') {\n max = 1;\n result[j] = 1;\n } else {\n result[j] = 0;\n }\n }\n for (auto i = 1uz; i < grid.size(); ++i) {\n if (grid[i][0uz] == '1') {\n max = 1;\n result[i * n] = 1;\n } else {\n result[i * n] = 0;\n }\n }\n }\n for (auto i = 1uz; i < grid.size(); ++i) {\n for (auto j = 1uz; j < grid[i].size(); ++j) {\n if (grid[i][j] == '0') {\n result[i * n + j] = 0;\n continue;\n }\n\n int left = result[i * n + j - 1uz];\n int top = result[(i - 1uz) * n + j];\n int mid = result[(i - 1uz) * n + j - 1uz];\n\n int count = 1 + std::min(std::min(left, top), mid);\n max = std::max(max, count);\n result[i * n + j] = count;\n }\n }\n return max * max;\n}\n\nclass Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n return maximal_square(matrix);\n }\n};", "memory": "24300" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int row = matrix.size(), col = matrix[0].size(); \n vector<int> prev(col,0);\n int maxi = 0;\n prev[0] = matrix[0][0] == '1' ? 1 : 0;\n maxi = prev[0];\n \n for(int i = 1; i < col; ++i)\n {\n prev[i] = matrix[0][i] == '1' ? 1 : 0;\n maxi = max(maxi, prev[i]);\n } \n //cout << maxi << endl; \n /*for(int i = 1; i < row; ++i)\n {\n dp[i][0] = (matrix[i][0] == '1') ? 1 : 0;\n maxi = max(maxi, dp[i][0]);\n }*/\n //cout << maxi << endl; \n for(int i = 1; i < row; ++i)\n {\n vector<int> curr(col,0);\n for(int j = 0; j < col; ++j)\n {\n if(j == 0)\n {\n curr[j] = (matrix[i][j] == '1') ? 1 : 0;\n\n maxi = max(maxi, curr[j]);\n continue;\n }\n\n if(matrix[i][j] == '1')\n {\n curr[j] = min({prev[j],curr[j-1],prev[j-1]})+1;\n maxi = max(maxi, curr[j]);\n }\n }\n prev = curr;\n }\n \n return maxi*maxi;\n }\n};", "memory": "24400" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int m = matrix.size(), n = matrix[0].size(), ans = 0;\n vector<vector<int>> max_sq(m, vector<int> (n,0));\n if(matrix.size() == 1){\n for(int e : matrix[0]){\n if(e - '0'){\n return e - '0';\n }\n }\n return 0;\n }\n for(int i = 0; i < n; i++){\n max_sq[0][i] = matrix[0][i] - '0';\n if(max_sq[0][i]){\n ans = 1;\n }\n }\n for(int i = 1; i < m; i++){\n for(int j = 0; j < n; j++){\n if(matrix[i][j] - '0'){\n ans = max(ans, 1);\n }\n if(j == 0){\n max_sq[i][j] = matrix[i][j] - '0';\n } else {\n if(matrix[i][j] - '0' && max_sq[i-1][j] && max_sq[i-1][j-1] && max_sq[i][j-1]){\n max_sq[i][j] = min(min(max_sq[i-1][j], max_sq[i-1][j-1]), max_sq[i][j-1]) +1 ;\n ans = max(max_sq[i][j], ans);\n } else {\n max_sq[i][j] = matrix[i][j] - '0';\n }\n }\n }\n }\n return ans*ans;\n }\n};", "memory": "24500" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n int n=matrix.size(),m=matrix[0].size();\n vector<vector<int>> dp(n+1,vector<int>(m+1,0));\n int ans=0;\n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n if(matrix[i-1][j-1]=='1'){\n int top=dp[i-1][j];\n int left=dp[i][j-1];\n int diag=dp[i-1][j-1];\n dp[i][j]=min(top,min(left,diag))+1;\n ans=max(ans,dp[i][j]);\n }\n }\n }\n return ans*ans;\n }\n};", "memory": "24600" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int row = matrix.size(), col = matrix[0].size();\n std::vector<std::vector<int>> m(row, std::vector<int>(col, 0));\n int i = 0, j = col;\n bool has_one = false;\n for (i=0;i<row ;i++)\n for (j=0; j < col; j ++)\n {\n m[i][j] = matrix[i][j] - '0';\n if (m[i][j])\n has_one = true;\n }\n if (!has_one)\n return 0;\n int len = 0;\n bool can_form = true;\n while (can_form)\n {\n len++;\n can_form = false;\n for (i = row - 1; i >= len; i --)\n {\n for (j = col - 1; j >= len; j --)\n {\n if (m[i-1][j-1] && m[i-1][j] && m[i][j-1] && m[i][j])\n {\n m[i][j] = 1;\n can_form = true;\n }\n else\n {\n m[i][j] = 0;\n }\n }\n }\n }\n return len * len;\n }\n};", "memory": "24600" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n if (matrix.empty())\n return 0;\n int n = matrix.size();\n int m = matrix[0].size();\n int maxi = 0;\n vector<vector<int>> dp(n, vector<int>(m, 0));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (!i || !j || matrix[i][j] == '0')\n dp[i][j] = matrix[i][j] - '0';\n else\n dp[i][j] =\n min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;\n maxi = max(maxi, dp[i][j]);\n }\n }\n return maxi * maxi;\n }\n};", "memory": "24700" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int n=matrix.size(),m=matrix[0].size();\n vector<vector<int>> dp(n+1,vector<int>(m+1,0));\n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+(matrix[i-1][j-1]=='1');\n }\n }\n auto isPossible = [&](int ind){\n for(int i=ind;i<=n;i++){\n for(int j=ind;j<=m;j++){\n int one=dp[i][j]-dp[i-ind][j]-dp[i][j-ind]+dp[i-ind][j-ind];\n if(one>=ind*ind) return true;\n }\n }\n return false;\n };\n\n int s=1,e=n,ans=0;\n while(s<=e){\n int mid=s+(e-s)/2;\n if(isPossible(mid)){\n ans=mid;\n s=mid+1;\n }else{\n e=mid-1;\n }\n }\n return ans*ans;\n }\n};", "memory": "24700" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n // the hard part is to find an efficient way to find the max matrix possible\n int m=matrix.size();\n int n=matrix[0].size();\n int maxside=0;\n vector<vector<int>> dp(m+1, vector(n+1, 0));\n for (int i=1; i<=m; i++) {\n for (int j=1; j<=n; j++) {\n if (matrix[i-1][j-1]=='1') {\n dp[i][j]=min(min(dp[i-1][j-1], dp[i-1][j]), dp[i][j-1])+1;\n maxside=max(maxside, dp[i][j]);\n }\n }\n }\n return maxside*maxside;\n }\n};", "memory": "24800" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n vector<vector<int>>dp(n, vector<int>(m,0));\n int ans = 0;\n for(int i = 0; i < n; ++i){\n for(int j = 0; j < m; ++j){\n if(matrix[i][j] == '1'){\n if(i-1 >= 0 && j - 1 >= 0){\n dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]));\n }\n dp[i][j] =dp[i][j] + 1;\n ans = max(ans, dp[i][j]);\n }\n //cout << \" dp \" << dp[i][j] << \" ans \" << ans;\n }\n //cout << endl;\n }\n return ans*ans;\n }\n};", "memory": "24800" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n int maxi = 0;\n vector<vector<int>> dp(n, vector<int>(m, 0));\n for(int i = 0;i < n;i++){\n for(int j = 0;j < m;j++){\n if(matrix[i][j] == '0')continue;\n int left = j == 0?0: dp[i][j-1];\n int up = i == 0?0: dp[i-1][j];\n int diag = (j == 0 || i == 0)?0: dp[i-1][j-1];\n maxi = max(maxi, dp[i][j] = ((left && diag && up)?1+min(diag, min(left, up)):1));\n }\n }\n return maxi*maxi;\n }\n};", "memory": "24900" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int maxSideLength = 0;\n vector<vector<int>>dp(matrix.size()+1,vector<int>(matrix[0].size()+1,0));\n for(int row=matrix.size()-1;row>=0;row--){\n for(int col=matrix[row].size()-1;col>=0;col--){\n int right = dp[row][col + 1];\n int diagonal = dp[row + 1][col + 1];\n int down = dp[row + 1][col];\n if (matrix[row][col] == '1') {\n int currentSideLength = 1 + min({right, diagonal, down});\n dp[row][col] = currentSideLength ;\n maxSideLength = max(maxSideLength, currentSideLength);\n }\n else{\n dp[row][col] = 0;\n }\n }\n }\n return maxSideLength * maxSideLength;\n }\n};", "memory": "24900" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int solveWithMemo(int i,int j, vector<vector<char>>& mat,vector<vector<int>>& dp){\n int n=mat.size();\n int m=mat[0].size();\n if(i>=n || j>=m || mat[i][j]=='0') return 0;\n if(dp[i][j]!=-1) return dp[i][j];\n\n int d= solveWithMemo(i+1,j,mat,dp);\n int r= solveWithMemo(i,j+1,mat,dp);\n int rd= solveWithMemo(i+1,j+1,mat,dp);\n\n return dp[i][j]=1 + min({r,d,rd});\n \n }\n int maximalSquare(vector<vector<char>>& mat) {\n int n=mat.size();\n int m=mat[0].size();\n\n int c=0;\n vector<vector<int>> dp(n, vector<int>(m, -1));\n\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(mat[i][j]=='1') c=max(c, solveWithMemo(i,j,mat,dp));\n }\n }\n return c*c;\n\n }\n};", "memory": "25000" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<vector<char>>& matrix, int i, int j, int& ans, vector<vector<int>>& dp){\n if(i >= matrix.size() || j >= matrix[0].size()) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n int r = solve(matrix, i, j+1, ans , dp);\n int diag = solve(matrix, i+1, j+1, ans , dp);\n int d = solve(matrix, i+1, j, ans, dp);\n if(matrix[i][j] == '1'){\n int s = 1 + min({r, diag, d});\n ans = max(ans, s);\n return dp[i][j] = s;\n }\n return 0;\n }\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n int ans = 0;\n vector<vector<int>> dp(n, vector<int>(m, -1));\n solve(matrix, 0, 0, ans, dp);\n return ans * ans;\n }\n};", "memory": "25000" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int func(vector<vector<int>>& dp,vector<vector<char>>& matrix,int n,int m){\n if(n>=matrix.size() || m>=matrix[0].size())return 0;\n if(dp[n][m]!=-1)return dp[n][m];\n if(matrix[n][m]=='1'){\n dp[n][m]=1+min({func(dp,matrix,n+1,m),func(dp,matrix,n,m+1),func(dp,matrix,n+1,m+1)});\n }\n else{\n dp[n][m]=0;\n }\n return dp[n][m];\n }\n int maximalSquare(vector<vector<char>>& matrix) {\n int n=matrix.size(),m=matrix[0].size();\n vector<vector<int>> dp(n,vector<int>(m,-1));\n int maxi=0;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n maxi=max(maxi,func(dp,matrix,i,j));\n }\n }\n return maxi*maxi;\n }\n};", "memory": "25100" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int f(vector<int>&height)\n {\n stack<int>st;\n int maxi=0;\n int n=height.size();\n for(int i=0;i<=n;i++)\n {\n while(!st.empty() && (i==n || height[st.top()]>=height[i]))\n {\n int ht=height[st.top()];\n st.pop();\n int width=1;\n if(st.empty())\n {\n width=i;\n }\n else\n {\n width=i-st.top()-1;\n }\n int side = min(width, ht);\n maxi=max(maxi,side*side);\n }\n st.push(i);\n \n } \n return maxi;\n\n }\n int maximalSquare(vector<vector<char>>& matrix) \n {\n int m=matrix.size();\n int n=matrix[0].size();\n vector<int>arr(n,0);\n int maxi=INT_MIN;\n for(int i=0;i<m;i++)\n {\n for(int j=0;j<n;j++)\n {\n // cout<<i<<\" \"<<j<<endl;\n if(matrix[i][j]=='1')\n {\n // cout<<i<<\" \"<<j<<endl;\n arr[j]++;\n }\n else if(matrix[i][j]=='0')\n {\n if(arr[j]!=0)\n {\n arr[j]=0;\n\n }\n }\n }\n \n int p=f(arr);\n maxi=max(maxi,p);\n\n }\n return maxi;\n\n\n \n }\n};", "memory": "25200" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n \n\n //find the largest square starting (top left) at (i, j), for all i, j where matrix[i][j] == 1\n //for all i, j, find the one with the largest value (use & to store)\n\n int ans = 0;\n\n int n = matrix.size();\n int m = matrix[0].size();\n\n vector<vector<bool>> vs(n, std::vector<bool>(m, 0));\n vector<vector<int>> dp(n, std::vector<int>(m, 0));\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n ans = max(ans, helper(matrix, i, j, vs, dp));\n }\n }\n\n return ans * ans;\n }\n\n int helper(vector<vector<char>>& matrix, int i, int j, vector<vector<bool>>& vs, vector<vector<int>>& dp) {\n\n \n int n = matrix.size();\n int m = matrix[0].size();\n\n if (i > n - 1 || j > m - 1) {\n return 0;\n } else if (matrix[i][j] == '0') {\n return 0;\n } else if (vs[i][j] == true) {\n return dp[i][j];\n } else {\n int sub_answer = min(helper(matrix, i + 1, j, vs, dp), helper(matrix, i, j + 1, vs, dp));\n sub_answer = min(sub_answer, helper(matrix, i + 1, j + 1, vs, dp)) + 1;\n vs[i][j] = true;\n dp[i][j] = sub_answer;\n return sub_answer;\n }\n\n }\n};", "memory": "25300" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\nint N;\nint M;\nint max_square;\nvector<vector<int>> memo;\nvector<vector<char>> mat;\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n N = matrix.size();\n M = matrix[0].size();\n mat = matrix;\n max_square = 0; \n memo.assign(N, vector<int>(M,0));\n\n int p = solve(N-1, M-1);\n return pow(max_square, 2);\n }\n\n int solve(int i, int j){\n if (i < 0 || j < 0) return 0;\n\n int& ans = memo[i][j];\n if (ans) return ans;\n\n /*\n [\n [\"1\",\"0\",\"1\",\"0\",\"0\"],\n [\"1\",\"0\",\"1\",\"1\",\"1\"],\n [\"1\",\"1\",\"1\",\"1\",\"1\"],\n [\"1\",\"0\",\"0\",\"1\",\"0\"]\n ]\n\n [0 , 0 , 0 , 0 , 0 ]\n [0 , 0 , 0 , 0 , 0 ]\n [0 , 0 , 0 , 0 , 0 ]\n [0 , 0 , 0 , 0 , 0 ]\n\n */\n int t = solve(i-1,j);\n int l = solve(i, j-1);\n int tl = solve(i-1,j-1);\n if (mat[i][j] == '1') ans = 1 + min(min(t,l), tl);\n max_square = max(max_square, ans);\n return ans;\n }\n};", "memory": "25400" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<char>> matrix;\n vector<vector<int>> dp;\n int n, m;\n\n bool isOne(int r, int c) { return (matrix[r][c] - '0' == 1); }\n int minimumValidSide(int r, int c) {\n int horizontal = 0, vertical = 0;\n for (int i = r; i < n && isOne(i, c); ++i, ++horizontal);\n for (int j = c; j < m && isOne(r, j); ++j, ++vertical);\n return min(horizontal, vertical);\n }\n int maxSquareWithUpperLeft(int r, int c) {\n if (r < 0 || r >= n || c < 0 || c >= m || !isOne(r, c))\n return 0;\n if (dp[r][c] != -1)\n return dp[r][c];\n int greatestSubSquare = maxSquareWithUpperLeft(r + 1, c + 1);\n int longestSide = minimumValidSide(r, c);\n return dp[r][c] = longestSide >= greatestSubSquare + 1 ? greatestSubSquare + 1: longestSide;\n }\n int maximalSquare(vector<vector<char>>& input) {\n matrix = input;\n n = matrix.size(), m = matrix[0].size();\n dp.assign(n, vector<int>(m, -1));\n int ans = 0;\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < m; ++j)\n ans = max(ans, maxSquareWithUpperLeft(i, j));\n return ans * ans;\n }\n};", "memory": "25400" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<char>> matrix;\n vector<vector<int>> dp;\n int n, m;\n\n bool isOne(int r, int c) { return (matrix[r][c] - '0' == 1); }\n int minimumValidSide(int r, int c) {\n int horizontal = 0, vertical = 0;\n for (int i = r; i < n && isOne(i, c); ++i, ++horizontal)\n ;\n for (int j = c; j < m && isOne(r, j); ++j, ++vertical)\n ;\n return min(horizontal, vertical);\n }\n int maxSquareWithUpperLeft(int r, int c) {\n if (r < 0 || r >= n || c < 0 || c >= m || !isOne(r, c))\n return 0;\n if (dp[r][c] != -1)\n return dp[r][c];\n int greatestSubSquare = maxSquareWithUpperLeft(r + 1, c + 1);\n int longestSide = minimumValidSide(r, c);\n return dp[r][c] = longestSide >= greatestSubSquare + 1 ? greatestSubSquare + 1: longestSide;\n }\n int maximalSquare(vector<vector<char>>& input) {\n matrix = input;\n n = matrix.size(), m = matrix[0].size();\n dp.assign(n, vector<int>(m, -1));\n int ans = 0;\n for (int i = 0; i < n; ++i)\n for (int j = 0; j < m; ++j)\n ans = max(ans, maxSquareWithUpperLeft(i, j));\n cout << \"ans of 1 1 \" << maxSquareWithUpperLeft(1, 1) << endl;\n return ans * ans;\n }\n};", "memory": "25500" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n // AC - AKS - solved myself in first attempt - Grid Prefix sum + Binary Search on answer - O(nlog(m*n))\n bool ispossible(int n, int m, vector<vector<long long>> &prefix, long long mid){\n long long req = mid*mid;\n for(int i = 1; i <= n; i++){\n for(int j = 1; j <= m; j++){\n int lr = i + mid-1;\n int lc = j + mid-1;\n if(lr <= n and lc <= m){\n long long sum = prefix[lr][lc] - prefix[lr][j-1] - prefix[i-1][lc] + prefix[i-1][j-1];\n if(sum == req) return true;\n }\n }\n }\n return false;\n }\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n\n // prefix[i][j] ==> contain prefix sum untill cell (i,j)\n vector<vector<long long>>prefix(n+1,vector<long long>(m+1,0));\n\n for(int i = 1; i <= n; i++){\n for(int j = 1; j <= m; j++){\n // fill the first row\n if(i == 1) prefix[i][j] = matrix[i-1][j-1] - '0' + prefix[i][j-1];\n\n // fill the first col\n else if(j == 1) prefix[i][j] = matrix[i-1][j-1] -'0' + prefix[i-1][j];\n\n // now get for others\n else prefix[i][j] = matrix[i-1][j-1] - '0' + prefix[i][j-1] + prefix[i-1][j] - prefix[i-1][j-1];\n }\n }\n\n // Now apply Binary Search on the answer\n long long l = 0;\n long long r = min(m,n);\n long long ans = 0;\n while(l <= r){\n long long mid = (l+r) >> 1L;\n if(ispossible(n, m,prefix,mid)){\n ans = mid*mid;\n l = mid+1;\n }\n else r = mid-1;\n }\n return ans;\n }\n};", "memory": "26200" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n // AC - AKS - solved myself in first attempt - Grid Prefix sum + Binary Search on answer - O(n*m) + O(nlog(m*n))\n bool ispossible(int n, int m, vector<vector<long long>> &prefix, long long mid){\n long long req = mid*mid;\n for(int i = 1; i <= n; i++){\n for(int j = 1; j <= m; j++){\n int lr = i + mid-1;\n int lc = j + mid-1;\n if(lr <= n and lc <= m){\n long long sum = prefix[lr][lc] - prefix[lr][j-1] - prefix[i-1][lc] + prefix[i-1][j-1];\n if(sum == req) return true;\n }\n }\n }\n return false;\n }\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n\n // prefix[i][j] ==> contain prefix sum untill cell (i,j)\n vector<vector<long long>>prefix(n+1,vector<long long>(m+1,0));\n\n for(int i = 1; i <= n; i++){\n for(int j = 1; j <= m; j++){\n // fill the first row\n if(i == 1) prefix[i][j] = matrix[i-1][j-1] - '0' + prefix[i][j-1];\n\n // fill the first col\n else if(j == 1) prefix[i][j] = matrix[i-1][j-1] -'0' + prefix[i-1][j];\n\n // now get for others\n else prefix[i][j] = matrix[i-1][j-1] - '0' + prefix[i][j-1] + prefix[i-1][j] - prefix[i-1][j-1];\n }\n }\n\n // Now apply Binary Search on the answer\n long long l = 0;\n long long r = min(m,n);\n long long ans = 0;\n while(l <= r){\n long long mid = (l+r) >> 1L;\n if(ispossible(n, m,prefix,mid)){\n ans = mid*mid;\n l = mid+1;\n }\n else r = mid-1;\n }\n return ans;\n }\n};", "memory": "26300" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool ispossible(int n, int m, vector<vector<long long>> &prefix, long long mid){\n long long req = mid*mid;\n for(int i = 1; i <= n; i++){\n for(int j = 1; j <= m; j++){\n int lr = i + mid-1;\n int lc = j + mid-1;\n if(lr <= n and lc <= m){\n long long sum = prefix[lr][lc] - prefix[lr][j-1] - prefix[i-1][lc] + prefix[i-1][j-1];\n if(sum == req) return true;\n }\n }\n }\n return false;\n }\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n\n vector<vector<long long>>prefix(n+1,vector<long long>(m+1,0));\n\n for(int i = 1; i <= n; i++){\n for(int j = 1; j <= m; j++){\n // fill the first row\n if(i == 1) prefix[i][j] = matrix[i-1][j-1] - '0' + prefix[i][j-1];\n\n // fill the first col\n else if(j == 1) prefix[i][j] = matrix[i-1][j-1] -'0' + prefix[i-1][j];\n\n // now get for others\n else prefix[i][j] = matrix[i-1][j-1] - '0' + prefix[i][j-1] + prefix[i-1][j] - prefix[i-1][j-1];\n\n // cout << \"prefix \" << prefix[i][j] << \" \";\n }\n // cout << endl;\n }\n\n long long l = 0;\n long long r = min(m,n);\n long long ans = 0;\n while(l <= r){\n long long mid = (l+r) >> 1L;\n if(ispossible(n, m,prefix,mid)){\n ans = mid*mid;\n l = mid+1;\n }\n else r = mid-1;\n }\n return ans;\n }\n};", "memory": "26400" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(vector<int>& heights) {\n stack<int>st;\n int n=heights.size();\n int maxArea=0;\n for(int i=0;i<=n;i++){\n while(!st.empty()&&(i==n||heights[st.top()]>heights[i])){\n int height=heights[st.top()];\n st.pop();\n int width=0;\n if(st.empty())width=i;\n else width=i-st.top()-1;\n int len=min(height,width);\n maxArea=max(maxArea,len*len);\n }\n st.push(i);\n }\n return maxArea;\n }\n int maximalSquare(vector<vector<char>>& matrix) {\n vector<int>temp;\n \n for(int i=0;i<matrix[0].size();i++)temp.push_back(matrix[0][i]=='1'?1:0);\n int mx=f(temp);\n for(int i=1;i<matrix.size();i++){\n for(int j=0;j<matrix[i].size();j++){\n if(matrix[i][j]=='1')temp[j]+=1;\n else temp[j]=0;\n }\n mx=max(mx,f(temp));\n }\n return mx;\n }\n};", "memory": "26500" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n int getMaxRectangle(vector<int> &heights) {\n stack<int> st;\n int size = heights.size();\n int maxRectangle = 0;\n \n for (int i=0; i<=size; i++) {\n // cout<<heights[i]<<\" \";\n while (st.size() && (i == size || heights[i] < heights[st.top()])) {\n int index = st.top();\n st.pop();\n if (st.empty()) {\n maxRectangle = max(maxRectangle, min(heights[index]*heights[index], i*i));\n }\n else {\n maxRectangle = max(maxRectangle, min(heights[index]*heights[index], \n (i-st.top()-1)*(i-st.top()-1)));\n }\n }\n st.push(i);\n }\n return maxRectangle;\n }\n\n int maximalRectangle(vector<vector<char>>& matrix) {\n int m = matrix.size();\n int n = matrix[0].size();\n \n vector<int> heights(n, 0);\n int maxRectangle = 0;\n for (int i=0; i<m; i++) {\n for (int j=0; j<n; j++) {\n if (matrix[i][j] == '0') {\n heights[j] = 0;\n }\n else {\n heights[j]++;\n }\n }\n maxRectangle = max(maxRectangle, getMaxRectangle(heights));\n }\n \n return maxRectangle;\n }\n\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n return maximalRectangle(matrix);\n }\n};", "memory": "26600" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n int getMaxRectangle(vector<int> &heights) {\n stack<int> st;\n int size = heights.size();\n int maxRectangle = 0;\n \n for (int i=0; i<=size; i++) {\n // cout<<heights[i]<<\" \";\n while (st.size() && (i == size || heights[i] < heights[st.top()])) {\n int index = st.top();\n st.pop();\n if (st.empty()) {\n maxRectangle = max(maxRectangle, min(heights[index]*heights[index], i*i));\n }\n else {\n maxRectangle = max(maxRectangle, min(heights[index]*heights[index], \n (i-st.top()-1)*(i-st.top()-1)));\n }\n }\n st.push(i);\n }\n return maxRectangle;\n }\n\n int maximalRectangle(vector<vector<char>>& matrix) {\n int m = matrix.size();\n int n = matrix[0].size();\n \n vector<int> heights(n, 0);\n int maxRectangle = 0;\n for (int i=0; i<m; i++) {\n for (int j=0; j<n; j++) {\n if (matrix[i][j] == '0') {\n heights[j] = 0;\n }\n else {\n heights[j]++;\n }\n }\n maxRectangle = max(maxRectangle, getMaxRectangle(heights));\n }\n \n return maxRectangle;\n }\n\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n return maximalRectangle(matrix);\n }\n};", "memory": "26600" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n vector<vector<int>> pref;\n for (int i = 0; i < matrix.size(); i++) {\n vector<int> newv(matrix[0].size(), 0);\n pref.push_back(newv);\n }\n\n int ans = 0;\n for (int i = 0; i < matrix[0].size(); i++) {\n pref[0][i] = (matrix[0][i] == '1') ? 1 : 0;\n if (matrix[0][i] == '1') ans = 1;\n }\n\n for (int i = 0; i < matrix.size(); i++) {\n pref[i][0] = (matrix[i][0] == '1') ? 1 : 0;\n if (matrix[i][0] == '1') ans = 1;\n }\n\n for (int i = 1; i < matrix.size(); i++) {\n for (int j = 1; j < matrix[0].size(); j++) {\n if (matrix[i][j] == '1') {\n pref[i][j] = min(min(pref[i - 1][j], pref[i][j - 1]), pref[i - 1][j - 1]) + 1;\n ans = max(ans, pref[i][j] * pref[i][j]);\n }\n }\n }\n\n return ans;\n\n }\n};", "memory": "26700" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int maxArea=0; \n\n int n= matrix.size(),m=matrix[0].size();\n vector<vector<int>> dp(n,vector<int>(m,0));\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n if(matrix[i][j]=='1' and i!=0 and j!=0)\n dp[i][j]=min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]))+1;\n else \n dp[i][j]=matrix[i][j]-'0';\n maxArea=max(maxArea,dp[i][j]);\n }\n }\n for(auto i:dp)\n {\n for(auto j:i)\n cout<<j<<\" \";\n cout<<endl;\n }\n\n return maxArea*maxArea;\n }\n};", "memory": "26800" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n vector<vector<int>> dp(n, vector<int>(m, 0));\n\n for(int i=0; i<n; i++) {\n for(int j=0; j<m; j++) {\n if(i == 0) dp[i][j] = matrix[i][j]-'0';\n else {\n int prevCol = j-1 >= 0 ? dp[i][j-1] : 0;\n int prevRow = i-1 >= 0 ? dp[i-1][j] : 0;\n int prevDiag = (i-1 >= 0 && j-1 >= 0) ? dp[i-1][j-1] : 0;\n if(matrix[i][j]-'0' != 0) {\n int mini = min(prevCol, min(prevRow, prevDiag));\n dp[i][j] = mini + 1;\n }\n }\n }\n }\n int maxi = 0;\n for(auto i : dp) {\n for(auto j : i) {\n cout << j << \" \";\n maxi = max(maxi, j);\n }\n cout << endl;\n }\n return maxi * maxi;\n }\n};", "memory": "26900" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int m = matrix.size(), n = matrix[0].size();\n vector<vector<int>> dp;\n for (int i = 0; i < m; i++) {\n vector<int> tmp(n);\n dp.push_back(tmp);\n }\n int ans = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (matrix[i][j] == '0') continue;\n int tmp = min(i, j);\n if (i && j) {\n tmp = min(tmp, dp[i - 1][j - 1]);\n }\n if (i) {\n tmp = min(tmp, dp[i - 1][j]);\n }\n if (j) {\n tmp = min(tmp, dp[i][j - 1]);\n }\n dp[i][j] = tmp + 1;\n ans = max(ans, dp[i][j]);\n }\n }\n return ans * ans;\n }\n};", "memory": "27000" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n vector<vector<int>> dp(n, vector<int>(m, 0));\n\n for(int i=0; i<n; i++) {\n for(int j=0; j<m; j++) {\n if(i == 0) dp[i][j] = matrix[i][j]-'0';\n else {\n int prevCol = j-1 >= 0 ? dp[i][j-1] : 0;\n int prevRow = i-1 >= 0 ? dp[i-1][j] : 0;\n int prevDiag = (i-1 >= 0 && j-1 >= 0) ? dp[i-1][j-1] : 0;\n if(matrix[i][j]-'0' != 0) {\n int mini = min(prevCol, min(prevRow, prevDiag));\n dp[i][j] = mini + 1;\n }\n }\n }\n }\n int maxi = 0;\n for(auto i : dp) {\n for(auto j : i) {\n cout << j << \" \";\n maxi = max(maxi, j);\n }\n cout << endl;\n }\n return maxi * maxi;\n }\n};", "memory": "27000" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size(), m = matrix[0].size();\n vector<vector<int>> hor(n,vector<int>(m,0)), dp(n,vector<int>(m,0));\n for(int i = 0;i<n;i++)\n {\n int cnt = 0;\n for(int j = 0;j<m;j++)\n {\n if(matrix[i][j]=='1')\n cnt++;\n else\n cnt = 0;\n hor[i][j] = cnt;\n }\n }\n for(int i = 0;i<m;i++)\n {\n int cnt = 0;\n for(int j = 0;j<n;j++)\n {\n if(matrix[j][i]=='1')\n cnt++;\n else\n cnt = 0;\n hor[j][i] = min(hor[j][i],cnt);\n }\n }\n int ans = 0;\n for(int i = 0;i<n;i++)\n {\n for(int j = 0;j<m;j++)\n {\n if(hor[i][j])\n dp[i][j] = 1;\n if(i>0 && j>0 && dp[i][j])\n {\n int anss = hor[i][j]-1;\n anss = min(anss,dp[i-1][j-1]);\n dp[i][j] = anss+1;\n }\n ans = max(ans,dp[i][j]);\n }\n }\n return ans*ans;\n }\n};", "memory": "27100" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n vector<vector<int>> dp(matrix.size(), vector<int>(matrix[0].size(), 0));\n int ans = 0;\n for (int i = 0; i < matrix.size(); ++i) {\n dp[i][0] = int(matrix[i][0] - '0');\n ans = max(ans, dp[i][0]);\n }\n for (int i = 0; i < matrix[0].size(); ++i) {\n dp[0][i] = int(matrix[0][i] - '0');\n ans = max(ans, dp[0][i]);\n }\n\n cout << endl;\n for (int i = 1; i < matrix.size(); ++i) {\n for (int j = 1; j < matrix[0].size(); ++j) {\n if (matrix[i][j] == '1') {\n dp[i][j] = 1;\n if (int(matrix[i - 1][j] - '0') &&\n int(matrix[i - 1][j - 1] - '0') &&\n int(matrix[i][j - 1] - '0')) {\n dp[i][j] = 1 + min(min((dp[i - 1][j - 1] ),\n (dp[i][j - 1] )),\n (dp[i - 1][j] ));\n }\n }\n ans = max(ans, dp[i][j]);\n }\n }\n\n for (auto f : dp) {\n for (auto g : f) {\n cout << g << \" \";\n }\n cout << endl;\n }\n return ans * ans;\n }\n};", "memory": "27100" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int i, int j, int n, int m, vector<vector<char>>& mat,\n vector<vector<int>>& vis,vector<vector<int>>&dp) {\n if (i >= n || j >= m || mat[i][j] == '0')\n return 0;\n\n \n if(dp[i][j]!=-1)\n return dp[i][j];\n \n vis[i][j] = 1;\n int a = 1 + solve(i + 1, j, n, m, mat, vis,dp);\n int b = 1 + solve(i, j + 1, n, m, mat, vis,dp);\n int c = 1 + solve(i + 1, j + 1, n, m, mat, vis,dp);\n vis[i][j] = 0;\n return dp[i][j] = min(a, min(b, c));\n }\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size(), m = matrix[0].size();\n vector<vector<int>> vis(n, vector<int>(m, 0));\n vector<vector<int>>dp(n+1,vector<int>(m+1,-1));\n int ans = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (matrix[i][j] == '1' && vis[i][j] == 0) {\n vis[i][j]=1;\n int cnt = solve(i, j, n, m, matrix, vis,dp);\n ans = max(ans, cnt);\n }\n }\n }\n return ans*ans;\n }\n};", "memory": "27200" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int largestarea(vector<int>&heights){\n int ans=0;\n stack<int>st;\n int n=heights.size();\n for(int i=0; i<=n; i++){\n while(!st.empty() && (i==n || heights[st.top()] > heights[i])){\n int height = heights[st.top()];\n st.pop();\n int width = st.empty()? i : i-st.top()-1;\n int mini = min(height,width);\n ans = max(ans, mini*mini);\n }\n st.push(i);\n }\n return ans;\n }\n int maximalSquare(vector<vector<char>>& matrix) {\n if(matrix.empty())\n return 0;\n vector<int> hist(matrix[0].size(),0);\n int maxi = INT_MIN;\n for(auto row: matrix){\n for(int i=0; i<row.size(); i++){\n hist[i] = row[i]=='0'? 0 : hist[i]+1;\n }\n maxi = max(maxi, largestarea(hist));\n }\n return maxi;\n }\n};", "memory": "27300" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int largestSquare(vector<int>& heights) {\n stack<int> st;\n int maxArea = 0;\n\n heights.push_back(0); // Add a zero height to flush out remaining elements in the stack.\n\n for (int i = 0; i < heights.size(); ++i) {\n while (!st.empty() && heights[i] < heights[st.top()]) {\n int height = heights[st.top()];\n st.pop();\n int width = st.empty() ? i : i - st.top() - 1;\n int side = min(height, width);\n maxArea = max(maxArea, side * side);\n }\n st.push(i);\n }\n\n return maxArea;\n }\n\n int maximalSquare(vector<vector<char>>& matrix) {\n if (matrix.empty())\n return 0;\n\n int maxArea = 0;\n int n = matrix.size();\n int m = matrix[0].size();\n\n vector<int> height(m, 0);\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n // Update the height of the histogram.\n if (matrix[i][j] == '1')\n height[j]++;\n else\n height[j] = 0;\n }\n // Calculate the largest square area for the current histogram.\n int area = largestSquare(height);\n maxArea = max(maxArea, area);\n }\n\n return maxArea;\n }\n};\n", "memory": "27400" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\nint squares(vector<int> heights)\n{\n int n=heights.size();\n int count=0;\n stack<int> st;\n for(int i=0;i<=n;i++)\n {\n while(!st.empty()&&(i==n || heights[st.top()]>=heights[i]))\n {\n int height=heights[st.top()];\n int width;\n st.pop();\n if(st.empty())width=i;\n else width=i-st.top()-1;\n int temp=min(height,width);\n temp=temp*temp;\n //cout<<height*width<<\" \";\n count=max(count,temp);\n }\n st.push(i);\n }\n // cout<<endl;\n return count;\n}\n int maximalSquare(vector<vector<char>>& matrix) {\n int ans=0;\n int m=matrix.size(),n=matrix[0].size();\n vector<int> height(n,0);\n for(int i=0;i<m;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(matrix[i][j]=='1')height[j]++;\n else height[j]=0;\n // cout<<height[j]<<\" \";\n }\n //cout<<endl;\n ans=max(ans,squares(height));\n }\n return ans;\n }\n};", "memory": "27500" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int helper(vector<int> & heights){\n int n = heights.size();\n stack<int> st;\n int maxArea=0;\n for(int i=0; i<n; i++){\n while(!st.empty() && heights[st.top()] >= heights[i]){\n int element = heights[st.top()];\n st.pop();\n int nse = i;\n int pse = (st.empty()) ? -1 : st.top();\n int mini = min(element,nse-pse-1);\n\n maxArea = max(maxArea, mini*mini);\n }\n st.push(i);\n }\n\n while(!st.empty()){\n int ele = heights[st.top()];\n st.pop();\n int nse = n;\n int pse = (st.empty()) ? -1 : st.top();\n int mini = min(ele,nse-pse-1);\n\n maxArea = max(maxArea, mini*mini);\n }\n\n return maxArea;\n }\n\n\n\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n vector<vector<int>> mat(n, vector<int> (m,0));\n for(int j=0; j<m; j++){\n int sum=0;\n for(int i=0; i<n; i++){\n if(matrix[i][j]=='1'){\n sum+=(matrix[i][j]-48);\n }\n else{\n sum=0;\n }\n mat[i][j]=sum;\n // cout<<i<<\" \"<<j<<\" \"<<sum<<endl;\n }\n }\n\n int maxArea=0;\n for(int i=0; i<n; i++){\n maxArea = max(maxArea, helper(mat[i]));\n }\n return maxArea;\n\n }\n};", "memory": "27600" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int largestRectangleArea(vector<int>& heights) {\n int n = heights.size();\n if(n==1) return heights[0];\n stack<int> st;\n st.push(-1);\n int ans=0;\n int height =0;\n int width = 0;\n for(int i=0;i<n;i++){\n while(st.top()!=-1 && heights[st.top()]>=heights[i]){\n height = heights[st.top()];\n st.pop();\n width = i-st.top()-1;\n int m = min(height,width);\n ans = max(ans,m*m); \n }\n st.push(i);\n }\n while(st.top()!=-1){\n height = heights[st.top()];\n st.pop();\n width = heights.size()-st.top()-1;\n int m = min(height,width);\n ans = max(ans,m*m); \n }\n // cout<<ans<<\" \"<<heights[0]<<\"\\n\";\n return ans;\n }\n\n int maximalSquare(vector<vector<char>>& matrix) {\n int n = matrix.size();\n int m = matrix[0].size();\n vector<vector<int>> dp(n+1,vector<int>(m+1,0));\n\n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n dp[i][j] = (matrix[i-1][j-1]=='1')? dp[i-1][j]+matrix[i-1][j-1]-'0':matrix[i-1][j-1]-'0';\n // cout<<dp[i][j]<<\" \";\n }\n //cout<<\"\\n\";\n }\n\n int ans=0;\n for(int i=1;i<=n;i++){\n ans = max(ans,largestRectangleArea(dp[i]));\n }\n return ans;\n\n\n }\n};", "memory": "27700" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int n,m;\n vector<vector<char>> matrix;\n vector<vector<int>>memo;\n int dp(int i,int j){\n if(i<0 || j<0 || matrix[i][j]=='0')return 0;\n\n if(memo[i][j]==-1){ \n memo[i][j]=1+min({dp(i,j-1),dp(i-1,j),dp(i-1,j-1)});\n }\n return memo[i][j];\n }\n int maximalSquare(vector<vector<char>>& matrix) {\n n=matrix.size();\n m=matrix[0].size();\n this->matrix=matrix;\n vector<vector<int>>memo(n,vector<int>(m,-1));\n this->memo=memo;\n int ans=0;\n for(int x=0;x<n;x++){\n for(int y=0;y<m;y++){\n int curr=pow(dp(x,y),2);\n ans=max(curr,ans);\n }\n }\n return ans;\n }\n};", "memory": "27800" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int n,m;\n vector<vector<char>> matrix;\n vector<vector<int>>memo;\n int dp(int i,int j){\n if(i<0 || j<0 || matrix[i][j]=='0')return 0;\n\n if(memo[i][j]==-1){ \n memo[i][j]=1+min({dp(i,j-1),dp(i-1,j),dp(i-1,j-1)});\n }\n return memo[i][j];\n }\n int maximalSquare(vector<vector<char>>& matrix) {\n n=matrix.size();\n m=matrix[0].size();\n this->matrix=matrix;\n vector<vector<int>>memo(n,vector<int>(m,-1));\n this->memo=memo;\n int ans=0;\n for(int x=0;x<n;x++){\n for(int y=0;y<m;y++){\n int curr=pow(dp(x,y),2);\n ans=max(curr,ans);\n }\n }\n return ans;\n }\n};", "memory": "27800" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n int getMaxArea(vector<int>& hist)\n {\n int maxArea = 0;\n stack<pair<int, int>> stk;\n for (int i = 0; i < hist.size(); ++i)\n {\n int start = i;\n while (!stk.empty() && stk.top().second > hist[i])\n {\n int index = stk.top().first;\n int height = stk.top().second;\n stk.pop();\n int sideLength = min(height, i - index);\n maxArea = max(maxArea, sideLength * sideLength);\n start = index;\n }\n stk.push({start, hist[i]});\n }\n return maxArea;\n }\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n int res = 0;\n int m = matrix.size();\n int n = matrix[0].size();\n vector<int> hist (n + 1, 0);\n for (int i = 0; i < m; ++i)\n {\n for (int j = 0; j < n; ++j)\n {\n if (matrix[i][j] == '1')\n {\n hist[j] += 1;\n }\n else\n {\n hist[j] = 0;\n }\n }\n res = max(res, getMaxArea(hist));\n }\n return res;\n }\n};", "memory": "27900" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int helper(vector<int>& height){\n stack<pair<int,int>> s;\n auto max_area = height[0] ? 1 : 0, start = 0;\n for (auto i = 0; i < height.size()-1; i++){\n if (height[i] <= height[i+1]) {\n s.push({height[i], start});\n start = i+1;\n }\n else {\n auto minim = min(i-start+1, height[i]);\n max_area = max(minim*minim, max_area);\n while (not s.empty() and s.top().first > height[i+1]){\n auto [heights, front] = s.top();\n s.pop();\n minim = min(i-front+1, heights);\n max_area = max(max_area, minim*minim);\n start = front;\n }\n }\n }\n auto minim = min((int)height.size()-start, height[height.size()-1]);\n max_area = max(minim*minim, max_area);\n while (not s.empty()){\n auto [heights, front] = s.top();\n s.pop();\n minim = min((int)height.size()-front, heights);\n max_area = max(max_area, minim*minim);\n }\n return max_area;\n }\n int maximalSquare(vector<vector<char>>& matrix) {\n auto max_area = 0;\n vector<int> hist(matrix[0].size());\n for (auto i = 0; i < matrix.size(); i++){\n for (auto j = 0; j < matrix[0].size(); j++){\n if (matrix[i][j] == '1') hist[j]++;\n else hist[j] = 0;\n }\n max_area = max(max_area, helper(hist));\n }\n return max_area;\n }\n};", "memory": "28000" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n struct Memo {\n int up;\n int left;\n int square;\n };\n int m, n;\n int maximalSquare(vector<vector<char>>& matrix) {\n m = matrix.size();\n n = matrix[0].size();\n int max_square = 0;\n\n vector<vector<Memo>> memo;\n memo.resize(m);\n for (int i = 0; i < m; i++) {\n memo[i].resize(n);\n }\n\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (matrix[i][j] == '0') {\n memo[i][j] = { 0, 0, 0 };\n continue;\n }\n \n Memo &current = memo[i][j] = { 1, 1, 1 };\n if (i > 0)\n current.up += memo[i-1][j].up;\n if (j > 0)\n current.left += memo[i][j-1].left;\n if (i > 0 && j > 0)\n current.square = min(min(current.up, current.left), memo[i-1][j-1].square + 1);\n max_square = max(current.square, max_square);\n }\n }\n\n return max_square * max_square;\n }\n};", "memory": "28100" }
221
<p>Given an <code>m x n</code> binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, <em>find the largest square containing only</em> <code>1</code>&#39;s <em>and return its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg" style="width: 400px; height: 319px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 4 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg" style="width: 165px; height: 165px;" /> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> matrix = [[&quot;0&quot;]] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == matrix.length</code></li> <li><code>n == matrix[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>matrix[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximalSquare(vector<vector<char>>& matrix) {\n struct Field {\n int up {0};\n int left {0};\n int diag {0};\n };\n\n int m = matrix.size();\n int n = matrix[0].size();\n\n int max_edge = 0;\n \n vector<vector<Field>> fields;\n for (int i = 0; i < m; i ++) {\n fields.push_back(vector<Field>(n));\n for (int j = 0; j < n; j ++) {\n //cout << i << \",\" << j << endl;\n if (matrix[i][j] == '0')\n fields[i][j].left = fields[i][j].up = 0;\n else {\n fields[i][j].left = (i == 0 ? 1 : fields[i-1][j].left + 1);\n fields[i][j].up = (j == 0 ? 1 : fields[i][j-1].up + 1);\n fields[i][j].diag = min(fields[i][j].up, fields[i][j].left);\n if (i > 0 && j > 0)\n fields[i][j].diag = min(fields[i][j].diag, fields[i-1][j-1].diag + 1);\n max_edge = max(max_edge, fields[i][j].diag);\n }\n //cout << \" \" << (char)(matrix[i][j] - '0'+' ') << fields[i][j].up << \"/\" << fields[i][j].left << \" / \" << fields[i][j].diag;\n }\n //cout << endl;\n }\n return max_edge * max_edge;\n }\n};", "memory": "28200" }