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> wher...
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: ...
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> wher...
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 ...
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> wher...
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...
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> wher...
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)...
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> wher...
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 ...
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> wher...
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; //...
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> wher...
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...
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> wher...
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...
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> wher...
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 }\...
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> wher...
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 ...
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> wher...
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 (...
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> wher...
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!...
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> wher...
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(a...
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> wher...
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.s...
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> wher...
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 \...
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> wher...
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 totalSea...
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> wher...
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(ro...
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> wher...
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 ...
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> wher...
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+=ce...
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> wher...
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]...
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> wher...
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 totalSea...
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> wher...
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 ...
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> wher...
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>...
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> wher...
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 ...
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> wher...
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 fo...
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> wher...
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>&...
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> wher...
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 , ...
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> wher...
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 ...
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> wher...
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 ...
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> wher...
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]...
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> wher...
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]...
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> wher...
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), co...
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> wher...
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> visit...
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> wher...
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 // ...
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> wher...
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> vi...
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> wher...
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 qu...
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> wher...
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 lo...
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.l...
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 ...
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.l...
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 +...
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.l...
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 <...
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.l...
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...
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.l...
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 <...
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.l...
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 ...
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.l...
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 ...
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.l...
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 ...
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.l...
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] << en...
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.l...
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 ...
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.l...
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 is...
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.l...
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 (i...
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.l...
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 (i...
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.l...
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(i...
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.l...
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...
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.l...
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...
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.l...
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...
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.l...
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[...
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.l...
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 ...
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.l...
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 ...
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.l...
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 <...
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.l...
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 ...
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.l...
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 ...
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.l...
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 ...
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.l...
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 < ...
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.l...
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-...
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.l...
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));...
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.l...
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 ...
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.l...
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 ...
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.l...
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--)...
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.l...
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,m...
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.l...
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 = solv...
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.l...
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,matri...
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.l...
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...
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.l...
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 ...
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.l...
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<...
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.l...
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); +...
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.l...
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); +...
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.l...
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 f...
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.l...
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 ...
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.l...
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 ...
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.l...
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...
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.l...
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 || hei...
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.l...
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 || hei...
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.l...
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 ...
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.l...
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 ...
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.l...
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...
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.l...
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 ...
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.l...
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...
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.l...
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 = ...
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.l...
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...
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.l...
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 ...
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.l...
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 ...
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.l...
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(...
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.l...
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 s...
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.l...
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 ...
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.l...
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 ...
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.l...
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 retur...
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.l...
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 retur...
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.l...
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 ...
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.l...
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 ...
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.l...
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...
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.l...
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 ...