id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n void DFS(int idx, vector<vector<int>>& g, vector<int>& vis,\n vector<int>& order) {\n vis[idx] = 1;\n for (auto child : g[idx]) {\n if (vis[child])\n continue;\n DFS(child, g, vis, order);\n }\n orde... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n vector<int> topoSort(int k, vector<vector<int>>& graph){\n vector<vector<int>>adjList(k + 1);\n vector<int>indeg(k + 1, 0);\n vector<int>topo;\n for(auto &v : graph){\n adjList[v[0]].push_back(v[1]);\n indeg[v[1]]++;\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& row, vector<vector<int>>& col) {\n \n vector<vector<int>> ans(k, vector<int>(k, 0));\n\n vector<int> tsort_row;\n vector<int> tsort_col;\n\n vector<vector<int>> adj_row(k);\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n vector<int> topo(int k,vector<vector<int>>& g,vector<int> ind){\n queue<int> q;\n vector<int> res;\n for(int i=0;i<k;i++){\n if(ind[i]==0){\n q.push(i);\n }\n }\n\n while(q.size()>0){\n res.pus... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "#include <vector>\n#include <queue>\n#include <unordered_map>\n#include <unordered_set>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n vector<vector<int>> matrix(k, vector<int>... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)\n#define ull unsigned long long int\n#define yes cout << \"YES\" << endl\n#define no cout << \"NO\" << endl\n#define ll long long int\n#define mii unordered_map<int,int>\n#define loop(i,s, e) for (int i = s; i < e; i++)\n#define s... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n struct node\n {\n int val;\n node* next;\n node(int v, node* n): val(v), next(n) {}\n };\n void topologicalDFS(int val, int& k, int& sorted_count, node**& adjList, int* conditionCount, int*& sorted_result)\n {\n node* current = adjList[... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n vector<int> topSort(const vector<vector<int>>& g, int n) {\n vector<int> indeg(n, 0);\n for (int u = 0; u < n; ++u) {\n for (int v : g[u]) {\n indeg[v]++;\n }\n }\n\n queue<int> q;\n for (int i = 0; i < n... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n bool cycle(unordered_map<int, vector<int>> m, vector<int> &v, vector<int> &v2, vector<int> &st, int p){\n v[p] = 1;\n for(auto &x: m[p]){\n if(!(!v[x] && (v2[x] || cycle(m, v, v2, st, x)))) \n return false;\n }\n v[p] = 0;... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int u,unordered_map<int,vector<int>>& adj,stack<int>& st,bool& hascycle,vector<int>& visited)\n {\n visited[u]=1;\n for(auto &v:adj[u])\n {\n if(visited[v]==1)\n {\n hascycle=true;\n return;\... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& r, vector<vector<int>>& c) {\n vector<vector<int>> ans;\n \n vector<int> row_indegree(k,0);\n vector<int> row_adj[k];\n for(int i=0;i<r.size();i++){\n row_indegree[r[i][1... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>> &rowConditions, vector<vector<int>> &colConditions)\n{\n vector<int> adj1[k], adj2[k];\n vector<int> indegree1(k, 0), indegree2(k, 0);\n for (auto it : rowConditions)\n {\n int u = it[0] - 1;\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n vector<int>khan_algo(int n,vector<vector<int>>&arr)\n {\n vector<int>adj[n+1];\n vector<int>indeg(n+1,0);\n for(auto vec:arr)\n {\n int u=vec[0];\n int v=vec[1];\n adj[u].push_back(v);\n indeg[v]++;\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\n bool topoSort(vector<int>g[], vector<int>&topo,int k, vector<int>&indegree){\n queue<int>q;\n for(int i=1;i<=k;i++){\n if(indegree[i]==0){\n q.push(i);\n }\n }\n while(!q.empty()){\n int first = q.front();\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n vector<int> khan(int k,vector<vector<int>>& Conditions){\n vector<int>graph[k+1];\n vector<int>degree(k+1);\n for(auto i:Conditions){\n graph[i[0]].push_back(i[1]);\n degree[i[1]]++;\n }\n queue<int>q;\n for(int i=1;i<k;i++){\n if(... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\nvector<int> khan(int k,vector<vector<int>>& Conditions){\n vector<int>graph[k+1];\n vector<int>degree(k+1);\n for(auto i:Conditions){\n graph[i[0]].push_back(i[1]);\n degree[i[1]]++;\n }\n queue<int>q;\n for(int i=1;i<k;i++){\n if(degree[i]=... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n vector<int> khan(int k,vector<vector<int>>& Conditions){\n vector<int>graph[k+1];\n vector<int>degree(k+1);\n for(auto i:Conditions){\n graph[i[0]].push_back(i[1]);\n degree[i[1]]++;\n }\n queue<int>q;\n for(int i=1;i<k;i++){\n if(... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 2 | {
"code": "class Solution {\npublic:\n vector<int> khan(int k,vector<vector<int>>& Conditions){\n vector<int>graph[k+1];\n vector<int>degree(k+1);\n for(auto i:Conditions){\n graph[i[0]].push_back(i[1]);\n degree[i[1]]++;\n }\n queue<int>q;\n for(int i=1;i<k;i++){\n if(... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\nvector<int> khan(int k,vector<vector<int>>& Conditions){\n vector<int>graph[k+1];\n vector<int>degree(k+1);\n for(auto i:Conditions){\n graph[i[0]].push_back(i[1]);\n degree[i[1]]++;\n }\n queue<int>q;\n for(int i=1;i<k;i++){\n if(degr... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n \n vector<int> generateTopologicalSort(const vector<vector<int>>& a, int k){\n vector<vector<int>>adj(k);\n vector<int>inDeg(k, 0);\n vector<int>order;\n \n for(auto it: a){\n adj[it[0]-1].push_back(it[1]-1);\n inDeg... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int> topoSort(vector<vector<int>>& graph, int k) {\n vector<int> inDegree(k + 1, 0), order;\n queue<int> q;\n\n // Compute in-degrees\n for (int i = 1; i <= k; i++) {\n for (int j : graph[i]) {\n inDegree[j]++;\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int> topologicalSort(vector<vector<int> >& adj)\n{\n int V=adj.size();\n // Vector to store indegree of each vertex\n vector<int> indegree(V);\n for (int i = 0; i < V; i++) {\n for (auto it : adj[i]) {\n indegree[it]++;\n }\n }\n... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\n vector<int> topoSort(vector<vector<int>> &edges, int k)\n {\n vector<int> adj[k + 1];\n vector<int> degree(k + 1, 0);\n for(auto edge : edges)\n {\n adj[edge[0]].push_back(edge[1]);\n degree[edge[1]]++;\n }\n queue<int... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int> topoSort(vector<vector<int>> &graph, int k) {\n vector<int> inDegree(k+1, 0), order;\n queue<int> q;\n for(int i =1;i<=k;i++){\n for(int j:graph[i]){\n inDegree[j]++;\n }\n }\n for(int i=1;i<=... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n unordered_map<int,list<int>>mp1;\n unordered_map<int,list<int>>mp2;\n vector<int>indegree1(k+1,0);\n vector<int>indegree2(k+1,0);\n\n... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\n int n;\n vector<int> topo(vector<int> adjL[]){\n vector<int> inDegree(n, 0);\n for(int i = 1; i < n; i++){\n for(auto it : adjL[i]){\n inDegree[it]++;\n }\n }\n\n queue<int> q;\n for(int i = 1; i < n; i++){\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int> topo(int n,vector<vector<int>> &edges){\n vector<vector<int>> graph(n+1);\n vector<int> inDegree(n+1);\n for(auto it : edges){\n int u = it[0];\n int v = it[1];\n graph[u].push_back(v);\n inDegree[v]... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n vector<int> rowTopo = topoSort(rowConditions, k);\n vector<int> colTopo = topoSort(colConditions, k);\n\n if(rowTopo.empty() || colTopo.empt... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n vector<int> rowTopo = topoSort(rowConditions, k);\n vector<int> colTopo = topoSort(colConditions, k);\n\n if(rowTopo.empty() || colTopo.empt... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n void dfs(int u, stack<int>& st, unordered_map<int, vector<int>>& adj, vector<int>& vis, bool& flag) {\n vis[u] = 1;\n for (auto it : adj[u]) {\n if (vis[it] == 0) {\n dfs(it, st, adj, vis, flag);\n } else if (vis[it] == 1) {\... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n unordered_map<int,int> topoSort(int k, vector<vector<int>>& grid){\n int n = grid.size();\n vector<int> indegree(k);\n unordered_map<int,vector<int>> map;\n for(auto i : grid) map[i[1]-1].push_back(i[0]-1);\n for(int i=0;i<n;i++) indegree[gr... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int> topo(vector<vector<int>>& conditions, int k){\n vector<int> inodes(k + 1);\n\n unordered_map<int, vector<int>> edges;\n\n for(vector<int> condition : conditions){\n edges[condition[0]].push_back(condition[1]);\n inodes[co... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "#include <vector>\n#include <queue>\n#include <unordered_map>\n#include <unordered_set>\n#include <algorithm>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n vector<int> rowOrde... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n\n void dfs(vector<bool>& visited, stack<int>&st, int num, unordered_map<int, vector<int>>&m){\n //cout<<\"hello\";\n for(int i =0; i<m[num].size();i++){\n if(visited[m[num][i]-1]==false){\n visited[m[num][i]-1] = true;\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\nprivate:\n vector<int> topoSortKahn(int k, vector<vector<int>> adj){\n vector<int> indegree(k+1, 0);\n for(int i=1;i<=k;i++){\n for(int j:adj[i])\n indegree[j]++;\n }\n queue<int> q;\n vector<int> order;\n for(int i=1;... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n vector<int> rowOrder = topologicalSort(k, rowConditions);\n vector<int> colOrder = topologicalSort(k, colConditions);\n \n if (rowOrd... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n\n vector<int> findOrder(int k, vector<vector<int>>& mat) {\n unordered_map<int, int> inDegree;\n unordered_map<int, vector<int>> adjList;\n \n // Build the graph and in-degree array\n for (auto p : mat) {\n int u = p[0];\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildGraph(int k, vector<vector<int>>& conditions){\n vector<vector<int>>adj(k+1);\n for(auto it:conditions){\n int u=it[0];\n int v=it[1];\n adj[u].push_back(v);\n }\n return adj;\n }\n \n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int> f(int k,vector<vector<int>>& r){\n vector<int> cnt(k+1,0);\n vector<int> adj[k+1];\n\n for(auto x:r){\n adj[x[0]].push_back(x[1]);\n cnt[x[1]]++;\n }\n\n vector<int> row;\n queue<int> q;\n\n fo... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class graph{\n int verts;\n map<int,vector<int>>adjlist;\n queue<int>roots;\n map<int,int>indegree;\n bool flag;\npublic:\n graph(int k,vector<vector<int>>& conditions);\n bool getflag();\n map<int,int> topologicalsort();\n};\ngraph::graph(int k,vector<vector<int>>& conditions){\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n void find(vector<int>& ans, vector<vector<int>>& row, int k) {\n vector<int> indegree(k + 1, 0);\n vector<int> adj[k + 1];\n for (auto it : row) {\n int u = it[0];\n int v = it[1];\n\n adj[u].push_back(v);\n ind... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n bool dfs(unordered_set<int> g[], const int& i, int& v, int& kval, int mat[]) {\n mat[i] = -2;\n for (auto& j : g[i]) {\n if (mat[j]==-2 || (mat[j]==-1 && dfs(g, j, v, kval, mat)))\n return true;\n }\n mat[i] = kval--;\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int> topoSort(int k,vector<int>adj[]){\n vector<int>indegree(k+1,0);\n for(int i = 1;i<=k;++i){\n for(auto it:adj[i]){\n indegree[it]++;\n }\n }\n \n queue<int>q;\n vector<int>topo;\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rc, vector<vector<int>>& cc) {\n vector<vector<int>>res(k,vector<int>(k));\n vector<int>RoAse(k),ColAse(k),InDeg(k+1);\n\n vector<vector<int>>adj(k+1),adj2(k+1);\n for(auto to : rc){\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int> topo_sort(vector<int> adj[],int k) {\n vector<int>indegree(k+1,0);\n\n for(int i = 1;i<=k;++i) {\n for(auto it : adj[i]) {\n indegree[it]++;\n }\n }\n\n queue<int>q;\n for(int i = 1;i<=k;++i) ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int> topo(vector<vector<int>> &edges, int n){\n vector<int> adj[n];\n vector<int> ind(n,0);\n vector<int> vis(n,0);\n for(auto it : edges){\n int u = --it[0];\n int v = --it[1];\n adj[u].push_back(v);\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& row, vector<vector<int>>& col) {\n vector<vector<int>>ans(k,vector<int>(k,0));\n vector<int>indegree1(k+1,0);\n vector<int>indegree2(k+1,0);\n vector<int>adj1[k+1];\n vector<int>adj... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "using ll = long long;\nusing pl = pair<ll,ll>;\nusing vl = vector<ll>;\nusing vvl = vector<vector<ll>>;\nusing vll = vector<pair<ll,ll>>;\n\n#define all(a) a.begin(),a.end()\n#define rall(a) a.rbegin(),a.rend()\n\nvoid read(vl& a){for(auto &x : a) cin >> x;}\nvoid read(vll& a){for(auto &x : a) cin >> x.fir... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int> toposort(vector<vector<int>>&cond,int k){\n vector<int>indegree(k,0);\n for(auto i:cond){\n for(auto j:i){\n indegree[j]++;\n }\n }\n queue<int>q;\n for(int i=0;i<k;i++){\n if(indeg... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int> topologicalSort(int k, vector<vector<int>> conditions) {\n vector<int> indegree;\n vector<int> results;\n indegree.resize(k + 1);\n queue<int> q;\n unordered_map<int, vector<int>> adjList;\n for(int i = 0; i < conditions.s... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rc,\n vector<vector<int>>& cc) {\n map<int, vector<int>> down;\n map<int, vector<int>> right;\n vector<int> downid(k + 1);\n vector<int> rightid(k + 1);\... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n\n bool detectCycle(int currNode,vector<vector<int>>&adj,vector<bool>&visited,vector<bool>&pathVisited)\n {\n visited[currNode]=true;\n pathVisited[currNode ]=true;\n\n for(auto nbr:adj[currNode])\n {\n if(!visited[nbr])\n {... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<int>topoSort(vector<vector<int>>&adj,int k){\n queue<int>q;\n vector<int>indegree(k+1,0);\n for(auto it:adj){\n for(auto jt:it){\n indegree[jt]++;\n }\n }\n vector<int>topo;\n for(int i=1;i<... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n bool dfs(int node, vector<int> adj[],vector<bool> &visited,\n vector<bool>& recStack) {\n visited[node] = true;\n recStack[node] = true;\n\n for (int neighbor : adj[node]) {\n if (!visited[neighbor] &&\n dfs(neighbor,... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n vector<vector<int>> matrix(k, vector<int>(k, 0));\n \n vector<int> rowOrder = topologicalSort(k, rowConditions);\n vector<int> colOrd... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\nprivate:\nbool ivalid = false;\nvoid dfs(vector<vector<int>> &v, vector<bool> &vis, int i) {\n if(ivalid) return;\n vis[i] = true;\n for(int j = 0; j < v[i].size(); j++)\n if(!vis[v[i][j]])\n dfs(v, vis, v[i][j]);\n else\n ivalid = true;\n}\nvo... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\nprivate:\nvoid topoSort(vector<vector<int>> &v, vector<bool> &vis, int i, vector<int> &sol) {\n vis[i] = true;\n for(int j = 0; j < v[i].size(); j++)\n if(!vis[v[i][j]])\n topoSort(v, vis, v[i][j], sol);\n sol.push_back(i);\n}\nvector<int> sortFunc(vector<vector... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n\n vector<int>topological(int k,vector<int>adj[],vector<vector<int>>& V)\n {\n vector<int>indeg(k,0);\n for(auto it:V)\n {\n indeg[it[1]]++;\n }\n queue<int>que;\n for(int i=1;i<k;i++)\n {\n if(indeg[i]==0)\n que.push(i);\n }\n vec... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n bool topoSort(vector<vector<int>> conditions, vector<int> &cond, int k){\n map<int, vector<int>> adj;\n map<int, int> inDegree;\n \n for(int i = 0; i < conditions.size(); i++){\n inDegree[conditions[i][1]]++;\n if(!inDegree.co... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n bool hasCycle(int s, set<int> adj[], vector<int>& mark) {\n mark[s] = 1;\n for(auto v: adj[s]) {\n if(mark[v]==2){continue;}\n if(mark[v]==1 || hasCycle(v, adj, mark)){return true;}\n }\n mark[s] = 2;\n return false;\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n // bool hasCycle(int s, set<int> adj[], vector<int>& mark) {\n // mark[s] = 1;\n // for(auto v: adj[s]) {\n // if(mark[v]==2){continue;}\n // if(mark[v]==1 || hasCycle(v, adj, mark)){return true;}\n // }\n // mark[s] = 2;\n ... |
2,472 | <p>You are given a <strong>positive</strong> integer <code>k</code>. You are also given:</p>
<ul>
<li>a 2D integer array <code>rowConditions</code> of size <code>n</code> where <code>rowConditions[i] = [above<sub>i</sub>, below<sub>i</sub>]</code>, and</li>
<li>a 2D integer array <code>colConditions</code> of size <... | 3 | {
"code": "class Solution {\npublic:\n\n void dfs(vector<vector<int>>&adj , int source , vector<int>&sort , vector<bool>&vis , vector<bool>&dfsvis ){\n \n dfsvis[source] = true;\n for(auto i:adj[source]){\n if(dfsvis[i] == true){\n sort.clear();\n retur... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 0 | {
"code": "class Solution {\npublic:\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n ios_base::sync_with_stdio(false);\n int n = books.size();\n int dp[n+1];\n dp[0] = 0;\n dp[1] = books[0][1];\n for(int i=2; i<=n; i++)\n {\n int re... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 0 | {
"code": "class Solution {\npublic:\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n int n = books.size();\n int f[n+1];\n f[0]=0;\n for(int i=1;i<=n;i++){\n int w=books[i-1][0], h=books[i-1][1];\n f[i]=f[i-1]+h;\n for(int j =i-1;j... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 0 | {
"code": "class Solution {\npublic:\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n int n = books.size();\n int f[n + 1];\n f[0] = 0;\n for (int i = 1; i <= n; ++i) {\n int w = books[i - 1][0], h = books[i - 1][1];\n f[i] = f[i - 1] + h;\n ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 0 | {
"code": "class Solution {\npublic:\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n\n int n = books.size();\n vector<int> dp(n + 1, 0);\n\n dp[0] = 0;\n dp[1] = books[0][1];\n\n for (int i = 1; i <= n; i++) {\n int rw = shelfWidth - books[i - 1]... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n#define dbg(...) __f(#__VA_ARGS__,__VA_ARGS__)\n// trace std::pair\ntemplate<class L,class R> std::ostream& operator<<(std::ostream& os,std::pair<L,R>& P){\n return os<<\"{\"<<P.first<<\":\"<<P.second<<\"}\";\n}\n// trace std::vector\ntemplate<class T> std... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n int INF = INT_MAX;\n int n, k;\n vector<vector<int>> books;\n vector<int> dp;\n\n int minHeightShelves(vector<vector<int>>& _books, int shelfWidth) {\n books = _books; n = books.size(), k = shelfWidth;\n dp = vector<int>(n, -1);\n return f(0);... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\nprivate:\nint memory[1001];\nvector<vector<int>> bookss;\nint shelfwidth;\nint minheight(int idx){\n if(idx >= (int)bookss.size()){\n return 0;\n }\n auto&ref = memory[idx];\n if(ref != -1){\n return ref;\n }\n int totalwidth = 0;\n int maxheight = 0;\n ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n static const int MAXN = 1e3 + 5;\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n int N = books.size();\n int cache[MAXN];\n memset(cache, 0x3f, sizeof(cache));\n\n function<int(int)> solve = [&](int x) -> int {\n ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n int sz = books.size();\n if (!sz) {\n return 0;\n }\n vector<vector<int>> cur;\n // total height, width left, cur last shelf height\n cur.push_back({books... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n int sz = books.size();\n if (!sz) {\n return 0;\n }\n vector<vector<int>> cur;\n // total height, width left, cur last shelf height\n cur.push_back({books... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n int helper(vector<vector<int>>& books, int width, vector<int> &dp, int index) {\n const int n = books.size();\n if (index >= n) return 0;\n if (dp[index] != -1) return dp[index];\n\n int sum = 0;\n int h = 0;\n int ans = INT_MAX;\n\n ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\n vector<vector<int>> arr;\n int n;\n int wid;\n vector<int> dp;\n int find(int s)\n {\n // cout<<s<<\" \"<<n;\n if(s == n)\n return 0;\n // cout<<\"adf\";\n int maxHeight = 0;\n int width = 0;\n int ans = INT_MAX;\n ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n unordered_map<int,int> map;\n int helper(int i, vector<vector<int>>& books, int& shelfWidth){\n if (i == books.size()) return 0;\n if (map.find(i) != map.end()) return map[i];\n int curr_width = books[i][0], max_height = books[i][1];\n int j = i... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n map<int,int> cache;\n int helper(int i, vector<vector<int>>& books, int shelfWidth){\n if(i >= books.size()){\n return 0;\n }\n\n auto it = cache.find(i);\n if(it!=cache.end()){\n return it->second;\n }\n\n in... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\n unordered_map<int, int> dp;\npublic:\n int helper(int idx, vector<vector<int>>& books, int shelfWidth) {\n if(idx>=books.size()) return 0;\n if(dp.find(idx)!=dp.end()) return dp[idx];\n int height = INT_MAX;\n int minHeight=INT_MAX, maxHeight=INT_MI... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\n unordered_map<int, int> dp;\npublic:\n int helper(int idx, vector<vector<int>>& books, int shelfWidth) {\n if(idx>=books.size()) return 0;\n if(dp.find(idx)!=dp.end()) return dp[idx];\n\n int height = INT_MAX, curMaxHeight=INT_MIN, width=0;\n\n for(... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> books;\n unordered_map<int,int> dp;\n\n int minHeightShelves(int i, int n, int shelfWidth) {\n if (i == n){\n return 0;\n }\n\n if (dp.find(i) != dp.end()){\n return dp[i];\n }\n \n int ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n map<int,int> mp;\n int solve(vector<vector<int>>& books, int &shelfWidth, int i){\n if(i==books.size()) return 0;\n if(mp.find(i) != mp.end()) return mp[i];\n int curr_width = 0;\n int max_height = 0;\n int res = INT_MAX;\n for(int... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> books_ref;\n int width_ref;\n unordered_map<int, int> memo;\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n // init vals\n books_ref = books;\n width_ref = shelfWidth;\n memo[books.size()] = 0;\n ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\nprivate:\n vector<vector<int>> arr;\n map<pair<int, int>, int> dp;\n int k;\n\npublic:\n int dfs(int i, int sum, int heights) {\n if (i == arr.size()) {\n return heights;\n }\n\n if (dp.find({i, sum}) != dp.end()) {\n return dp[{i, su... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n int shelfWidth;\n unordered_map<int,int> dp;\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n this->shelfWidth = shelfWidth;\n return minHeightShelvesHelper(books, 0);\n }\n\n int minHeightShelvesHelper(vector<vector<int>>& book... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\nprivate:\n int N;\n unordered_map<int, int> cache;\n\npublic:\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n N = books.size();\n return helper(0, shelfWidth, books);\n }\n int helper(int i, int shelfWidth, vector<vector<int>>& books) {\... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth, int idx, vector<int>& dp) {\n if(idx == books.size())\n return 0;\n if(dp[idx] != -1)\n return dp[idx];\n vector<int> currBook = books[idx];\n int currWidth = cu... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n map<array<int,3>,int> mp;\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n return solve(books,shelfWidth,1,books[0][0],books[0][1])+books[0][1];\n }\n\n int solve(vector<vector<int>>& books, int shelf, int i, int lastwid, int lastlen) {\n... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "\nstruct Shape {\n int width;\n int height;\n \n Shape(int width, int height): width(width), height(height) {}\n};\n\nclass Solution {\npublic:\n int func2(int pos, const Shape& level_shape, int width, const std::vector<std::vector<int>>& books, std::vector<std::unordered_map<int, int>>& dp)... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "\nstruct Shape {\n int width;\n int height;\n \n Shape(int width, int height): width(width), height(height) {}\n};\n\nclass Solution {\npublic:\n int func(int pos, const Shape& level_shape, int width, const std::vector<std::vector<int>>& books, std::vector<std::unordered_map<int, int>>& dp) ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "int recCall(int curInd, int curHeight, int curWidth, unordered_map<long long, int> &Mp, vector<vector<int>> &books, int &shelfWidth){\n if(curInd==books.size()){\n return 0;\n }\n if(Mp.find((long long)1000000*curInd+1000*curHeight+curWidth)!=Mp.end()){\n return Mp[(long long)1000000... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "int recCall(int curInd, int curHeight, int curWidth, unordered_map<long long, int> &Mp, vector<vector<int>> &books, int &shelfWidth){\n if(curInd==books.size()){\n return 0;\n }\n if(Mp.find((long long)1000000*curInd+curWidth)!=Mp.end()){\n return Mp[(long long)1000000*curInd+curWidt... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n int n = books.size();\n queue<pair<int, int>> q; // {index of the current book, total height so far}\n vector<int> visited(n + 1, INT_MAX); // visited[i] stores the minimum height to arr... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) \n {\n int n = books.size();\n queue<pair<int, int>> q; // {index of the current book, total height so far}\n vector<int> visited(n + 1, INT_MAX); // visited[i] stores the minimum height ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {\n std::vector<std::unordered_map<int, int>> memo(books.size());\n\n std::function<int(int, int)> helper = [&](int index, int freeShelfWidth) -> int {\n if (index >= books.size()) {\n ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\nprivate:\n int helper(vector<vector<int>>& books, int shelfWidth, vector<unordered_map<int, int>>& dp, int idx, int maxHt, int width){\n if(idx >= books.size()) return maxHt;\n if(dp[idx][width]) return max(maxHt, dp[idx][width]);\n\n int res = maxHt + helper(books... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\nprivate:\n int helper(vector<vector<int>>& books, int shelfWidth, vector<unordered_map<int, int>>& dp, int idx, int maxHt, int width){\n if(idx >= books.size()) return maxHt;\n if(dp[idx][width]) return dp[idx][width];\n\n int res = maxHt + helper(books, shelfWidth... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n int dp (vector<vector<int>>& books, int &sw, int i, int j, vector <int> &width, vector <int> &height, unordered_map <int, unordered_map<int, int>> &tmp)\n {\n if (j != 0 and width[j-1] == sw) return 10000000;\n if (i < 0) return height[j];\n if (tmp[i]... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n\nunordered_map<string,int>mp;\nint solve(int idx,int currheight,int currwidth,vector<vector<int>>& books, int shelfWidth){\n\n if(idx<0)return currheight;\n string str= to_string(idx)+\" \"+to_string(currheight)+\" \"+to_string(currwidth);\n\n if(mp.find(str)!=mp.end())... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "int dp[1001][1001];\nclass Solution {\npublic:\n int solve(vector<vector<int>>& books, int shelfw, int i, int lvl, int n, int height, int width){\n if(i==n) return height;\n if(dp[i][width]!=-1) return dp[i][width];\n int ans=INT_MAX;\n if(width>0){\n ans=min(ans, ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n\n int memo[1001][1001];\n\n int func(int i , vector<vector<int>> &books , int width , int sum , int maxi)\n { \n if(i >= books.size()){\n return maxi ;\n }\n\n if(memo[i][sum] != -1){\n return memo[i][sum];\n }\n\n ... |
1,196 | <p>You are given an array <code>books</code> where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer <code>shelfWidth</code>.</p>
<p>We want to place these books in order onto bookcase shelves th... | 2 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n\n int f(int n, vector<vector<int>> &books, int shelfWidth, int idx) {\n // Base case: if we have placed all books\n if (idx == n) {\n return 0;\n }\n\n // If this state is already computed, return the saved resul... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.