id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long func(vector<long long>& a, vector<long long>& b, int i, int j, vector<vector<long long>>& dp){\n if(j> b.size() || i>3) return 0;\n if(dp[i][j]!=INT_MIN) return dp[i][j];\n if(a.size()-i == b.size()-j){\n return dp[i][j] = a[i]*b[j] +... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long func(vector<int>&a,vector<int>&b,int index,long long kitne,vector<vector<long long>>&dp){\n if(kitne<4 && index>=b.size())return INT_MIN;\n if(kitne==4)return 0;\n if(dp[index][kitne]!=-1)return dp[index][kitne];\n long long maxi=max((lon... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n int n ;\n long long solve(int i , int c , vector<int>&a , vector<int>&b ,vector<vector<long long>>&dp){\n if(c == 4)return 0;\n if(i == n ){\n return -1e10;\n }\n if(dp[i][c] != -1)return dp[i][c];\n // if(_take == INT_MIN)_ta... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long ans = INT_MIN;\n long long solve(int i,int j,vector<int> &a,long long temp,vector<int>& b,vector<vector<long long>> &dp){\n // if(i>=b.size()) return;\n if(j==4)\n {\n \n return 0;\n \n }\n ... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long find(vector<vector<long long>>&arr,int i,int j,vector<vector<long long>>&dp,int m){\n if(i>=4) return 0;\n if(j>=m) return -1e9;\n \n if(dp[i][j] != -1) return dp[i][j];\n \n long long Take = arr[i][j]+find(arr,i+1,j+1,dp,m)... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\n int n, m;\n vector<vector<long long>>dp;\npublic:\n long long f(vector<int>&a, vector<int>&b, int i , int j){\n if(i>=n)return 0;\n if(j>=m)return INT_MIN;\n if(dp[i][j]!=-1)return dp[i][j];\n return dp[i][j]=max(a[i]*1ll*b[j]+f(a, b, i+1, j+1), f(a, ... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "#define ll long long \nclass Solution {\npublic:\n long long f(int i, int j, vector<int> &a, vector<int> &b, int n, \n vector<vector<ll>> &dp, vector<vector<ll>> &vis){\n if(i == 4) return 0;\n if(n-j < 4-i) return LONG_MIN;\n\n if(vis[i][j]) return dp[i][j];\n\n long ... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n\nlong long dpp(int idx,int id1,vector<int>& a,vector<int>& b,vector<vector<long long>>& dp){\n if(idx>=4) return 0;\n if(dp[idx][id1]!=-1) return dp[idx][id1];\n if(id1>=b.size() && idx<4) {\n // flag=false;\n return 0;\n }\n int len=a.size()-idx+1;\... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\n using ll=long long;\n \n ll dp[10][100005][10];\n ll f(ll i, ll j, ll k, vector<int>&a, vector<int>&b, ll n){\n if(j==n){\n if(k>0){\n return -1e12;\n }\n else{\n return 0;\n }\n }\n ... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& a, vector<int>& b) {\n int n = b.size();\n vector<vector<long long>> dp(n + 1);\n for (int i = 0; i <= n; i++) dp[i].resize(5);\n for (int i = 0; i <= 4; i++) dp[0][i] = (i == 0 ? 0 : -1e18);\n for (int i = 1;... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "// **ank**//\n#include <bits/stdc++.h>\nusing namespace std;\n#define ll long long int\n#define all(v) v.begin(), v.end()\n#define maxi(v) *max_element(v.begin(), v.end())\n#define vecsum(v) accumulate(v.begin(), v.end(), 0LL)\n#define mini(v)... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\n #define ll long long\npublic:\n long long maxScore(vector<int>& a, vector<int>& b) {\n map<ll,ll>mp;\n for(auto i:b)\n mp[i]++;\n // if(mp.size()>=4){\n\n // } \n ll n=a.size();\n ll m=b.size();\n vector<vector<ll>>dp(n... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "#define ll long long\nclass Solution {\npublic:\n long long maxScore(vector<int>& a, vector<int>& b) {\n \n int n = b.size();\n vector<vector<ll>> dp(n,vector<ll>(4,-1e12));\n\n dp[0][0] = max((ll)a[0]*(ll)b[0],(ll)-1e12);\n dp[0][1] = -1e12;\n dp[0][2] = -1e12;... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& a, vector<int>& b) {\n int n = b.size();\n if (n < 4) return LLONG_MIN; // Not enough elements in b to choose 4 indices\n\n // dp array to store the maximum scores\n vector<vector<long long>> dp(n, vector<long long>(... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long dp[100005][5];\n long long maxScore(vector<int>& a, vector<int>& b) {\n int n = b.size();\n vector<vector<long long>>dp(n+1, vector<long long>(5, LONG_LONG_MIN));\n for(int i=0;i<=n;i++) dp[i][0] = 0;\n for(int i=1;i<=n;i++){\n ... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& a, vector<int>& b) {\n int n = b.size();\n\n vector<vector<long long int>>dp(n+1,vector<long long>(5,INT_MIN));\n for(int cnt=0;cnt<=n;cnt++) dp[cnt][0] = 0;\n vector<int>temp1(5);\n vector<int>temp2(n+1);\n int i=1;\n for... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\n vector<int> arr, vec;\n vector<vector<long long>> dp;\npublic:\n long long maxScore(vector<int>& a, vector<int>& b) {\n int m = b.size();\n int n = a.size();\n\n arr = a;\n vec = b;\n dp = vector<vector<long long>>(m, vector<long long>(n, INT_M... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long maxiscore(int n, int m,vector<int>& a, vector<int>& b,vector<vector<long long>> &dp){\n if(n<0){\n return 0;\n }\n if(m<0){\n return -1e12;\n }\n if(dp[n][m] != -1){\n return dp[n][m];\n }\n ... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "#define ll long long\nclass Solution {\npublic:\n long long maxScore(vector<int>& a, vector<int>& b) {\n vector<vector<ll>> dp(4, vector<ll>(b.size(), LLONG_MIN));\n for(ll i = 0; i < a.size(); i++) {\n priority_queue<ll> pq;\n for(ll j = 0; j < b.size(); j++) {\n ... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "#define ll long long\nll mi = LLONG_MIN + 1e10;\nclass Solution {\npublic:\n long long maxScore(vector<int>& a, vector<int>& b) {\n int al = a.size();\n int bl = b.size();\n \n vector<vector<ll>> dp(4, vector<ll>(bl,mi));\n \n \n for(int i=0;i<bl;i++){\n ... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": " // Author: Niraj Umaretiya\n // Date: 2024-09-15\n // Time: 08:03:25\n #include <bits/stdc++.h>\n using namespace std;\n\n class Solution\n {\n public:\n long long maxScore(vector<int> &a, vector<int> &b) \n {\n int n = b.size();\n vector<long lo... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "\t#include<bits/stdc++.h>\n\n\t#define ll long long\n\t#define ld float\n\t#define float long double\n\t#define pii pair<int,int>\n\t#define pb push_back\n\t#define inf 1000000000\n\t#define fi first\n\t#define se second\n\t#define all(a) (a).begin(),(a).end()\n\t#define si(a) (int)((a).size())\n\t#define ... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& a, vector<int>& b) {\n int n=a.size();\n int m=b.size();\n vector<vector<long long>>dp(m,vector<long long>(5,-1));\n auto solve=[&](long long ind,long long used, auto &&self)->long long{\n if(used==n){\n ... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\n long long fn(int indx, vector<int>&a, vector<int>&b, vector<vector<long long>>&dp, int rest)\n {\n auto&ret = dp[indx][rest];\n if(ret != INT_MIN/2)\n return ret;\n long long c1 = INT_MIN/2;\n long long c2 = INT_MIN/2;\n if(b.size()-i... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long maxScoreHelper(int idx, int count, vector<int>& a, vector<int>& b, vector<vector<long long>>& dp) {\n \n if (count == 4) return 0;\n \n if (idx >= b.size()) return INT_MIN;\n \n if (dp[idx][count] != -1) return dp[idx][count... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\n using ll = long long;\n ll f(int ind,int count,vector<int>&a,vector<int>&b,vector<vector<ll>>&dp){\n if(count == 4)return 0;\n if(ind == b.size()){\n if(count == 4){\n return 0;\n }else return -1e9;\n }\n if(dp[ind][c... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<long long>> dp;\n vector<int> a1, b1;\n int n;\n\n long long rec(int level, int level1) {\n // Base cases\n if (level == n && level1 == 4) return 0; // We have selected 4 elements\n if (level == n || level1 > 4) return -1e9; // Exc... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long maxScore(vector<int>& a, vector<int>& b) {\n \n int n = b.size();\n \n vector<vector<long long int>> dp(n+1, vector<long long int>(4,-1));\n \n \n auto cal = [&](auto &self, int x, int y)->long long int {\n ... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\nint n,m;\nlong long solve(int i,int j,vector<int>&a,vector<int>&b,vector<vector<long long>>&dp)\n{\n if(j==m)\n {\n return 0;\n }\n if(i==n)\n {\n return -1e18;\n }\n if(dp[i][j]!=-1)\n {\n return dp[i][j];\n }\n long long ans=0;... |
3,518 | <p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p>
<p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s... | 3 | {
"code": "class Solution {\npublic:\n long long solve(vector<int>& a, vector<int>& b, int ind, int len,vector<vector<long long>>& dp){\n if(len==4) return 0;\n if(ind>=b.size()) return LLONG_MIN;\n if(dp[ind][len]!=LLONG_MIN) return dp[ind][len];\n long long take = solve(a, b, ind + 1,... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 0 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& command) {\n int i=0;\n int j=0;\n for(int k=0;k<command.size();k++)\n {\n if(command[k]==\"UP\")\n i--;\n else if(command[k]==\"DOWN\")\n i++;\n ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 0 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int x=0,y=0;\n for(int i=0;i<commands.size();i++){\n if(commands[i] == \"RIGHT\"){\n y++;\n }\n if(commands[i] == \"LEFT\"){\n y--;\n ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 0 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int x=0;\n for(int i=0;i<commands.size();i++){\n if(commands[i]==\"DOWN\"){\n x+=n;\n }\n else if(commands[i]==\"RIGHT\"){\n x+=1;\n }\n else if(... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 0 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int ans=0;\n for(auto it:commands){\n if(it==\"RIGHT\"){\n ans+=1; \n }\n else if(it==\"LEFT\"){\n ans-=1;\n }\n ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 0 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int x = 0, y = 0;\n for(auto it: commands){\n if (it == \"UP\") y--;\n else if (it == \"DOWN\") y++;\n else if (it == \"LEFT\") x--;\n else x++;\n }\... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 0 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int res = 0;\n for (string& cmd : commands){\n if (cmd == \"RIGHT\") {\n res++;\n } else if (cmd == \"LEFT\"){\n res--;\n } else if (cmd ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 0 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int size=commands.size();\n int ans=0;\n for(int i=0;i<size;i++){\n if(commands[i]==\"DOWN\"){\n ans=ans+n;\n }\n else if(commands[i]==\"UP\"){\n... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 0 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int length = commands.size();\n int row = 0 , column = 0;\n\n for(int i = 0 ; i < length ; i++){\n if(commands[i] == \"UP\"){\n row = row - 1 ; \n }\n ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 2 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int row=0;\n int col=0;\n int ans=0;\n\n for(auto it:commands){\n if(it==\"DOWN\"){\n ans=ans+n;\n }\n\n if(it==\"RIGHT\"){\n ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 2 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n // Starting position of the snake\n int x = 0, y = 0;\n \n // Move the snake as per the commands\n for (const string& command : commands) {\n if (command == \"UP\") {\n... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "int speed = []() {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n return 0;\n }();\nclass Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int i = 0, j = 0;\n for(auto it: commands){\n if(it =... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n vector<int> ans(2,0);\n for(string i:commands){\n if(i==\"RIGHT\") ans[0]+=1;\n else if(i==\"LEFT\") ans[0]-=1;\n else if(i==\"UP\") ans[1]-=n;\n else if(i=... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "auto fast = (ios::sync_with_stdio(0), cout.tie(0), cin.tie(0), false);\nclass Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int8_t ans = 0;\n array<int8_t, 96> adjustments;\n adjustments['U'] = -n;\n adjustments['R'] = 1;\n adjustm... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& cmd) {\n int i = 0,j = 0;\n for(auto x : cmd){\n string c = x;\n if(x == \"UP\"){\n i = i - 1;\n }\n if(x == \"DOWN\"){\n i = i + 1;\n ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n\n int row = 0,col=0;\n\n int** arr = new int*[n];\n for (int i = 0; i < n; ++i) {\n arr[i] = new int[n];\n }\n int count = 0;\n for (int i = 0; i < n; i++)\n {\n for (int j = 0; j... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int ans = 0;\n array<char, 256> adjustments;\n adjustments['U'] = -n;\n adjustments['R'] = 1;\n adjustments['D'] = n;\n adjustments['L'] = -1; \n for (const string& ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "int speedup = [] { return ios::sync_with_stdio(0), cin.tie(0), 0; }();\nclass Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int ans = 0;\n array<char, 256> adjustments;\n adjustments['U'] = -n;\n adjustments['R'] = 1;\n adjustments... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "int speedup = [] { return ios::sync_with_stdio(0), cin.tie(0), 0; }();\nclass Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int8_t ans = 0;\n array<int8_t, 256> adjustments;\n adjustments['U'] = -n;\n adjustments['R'] = 1;\n adjust... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n std::unordered_map<char, std::pair<int, int>> directions = {\n {'U', {-1, 0}},\n {'R', {0, +1}},\n {'D', {+1, 0}},\n {'L', {0, -1}}\n };\n \n ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n int len = commands.size();\n int r = 0, c = 0;\n map<string, int> mp{{\"UP\", 0}, {\"RIGHT\", 1}, {\"DOWN\", 2}, {\"LEFT\", 3}};\n\n for (int i = 0; i < len; ++i)\n {\n ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "class Solution {\npublic:\n \n int finalPositionOfSnake(int n, vector<string>& commands) {\n int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};\n map<string,int> m;\n m[\"RIGHT\"]=1;\n m[\"DOWN\"]=0;\n m[\"UP\"]=2;\n m[\"LEFT\"]=3;\n int i=0;\n int j=0;... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n vector<vector<int>>mat(n,vector<int>(n,0));\n for(int i=0;i<n;i++){\n for(int j=0;j<n;j++){\n mat[i][j]=(i*n)+j;\n }\n }\n int r=0,c=0;\n for(... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n vector<vector<int>>v(n,vector<int>(n,0));\n int count = 0;\n for(int i = 0;i<n;i++){\n for(int j = 0;j<n;j++){\n v[i][j] = count;\n count++;\n ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n\n vector<vector<int>> matrix(n , vector<int>(n , 0));\n int ans = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++){\n matrix[i][j] = ans;;\n ... |
3,533 | <p>There is a snake in an <code>n x n</code> matrix <code>grid</code> and can move in <strong>four possible directions</strong>. Each cell in the <code>grid</code> is identified by the position: <code>grid[i][j] = (i * n) + j</code>.</p>
<p>The snake starts at cell 0 and follows a sequence of commands.</p>
<p>You are... | 3 | {
"code": "class Solution {\npublic:\n int finalPositionOfSnake(int n, vector<string>& commands) {\n vector<vector<int>>v(n,vector<int>(n,0));\n int count = 0;\n for(int i = 0;i<n;i++){\n for(int j = 0;j<n;j++){\n v[i][j] = count;\n count++;\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC optimize(\"unroll-loops\")\n#pragma GCC optimize(\"inline\")\n#include<bits/stdc++.h>\nusing namespace std;\nvoid*wmem;\nchar memarr[96000000];\ntemplate<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){\n static int skip[16] = {0, 15, 14, 13, ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "// class Solution {\n// public:\n// int goodNodes = 0;\n// int findGoodNodes(vector<vector<int>> &adjList, vector<bool> &visited, int src) {\n// visited[src] = true;\n\n// int totalNodes = 1;\n// int prevChild = -1;\n\n// bool validGoodNode = true;\n// for(au... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\n static pair<unsigned, unsigned> dfs(unsigned u, unsigned p, const auto adj, const auto epos) {\n auto res = 0u;\n auto size = 1u;\n bool ok = true;\n auto firstsize = 0u;\n for (unsigned j = epos[u], l = epos[u + 1]; j != l; ++j) {\n const... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\n static pair<unsigned, unsigned> dfs(unsigned u, unsigned p, const auto adj, const auto epos) {\n auto res = 0u;\n auto size = 1u;\n bool ok = true;\n auto firstsize = 0u;\n for (unsigned j = epos[u], l = epos[u + 1]; j != l; ++j) {\n auto ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\n static pair<unsigned, unsigned> dfs(unsigned u, unsigned p, const auto adj, const auto epos) {\n auto res = 0u;\n auto size = 1u;\n bool ok = true;\n auto firstsize = 0u;\n for (unsigned j = epos[u], l = epos[u + 1]; j != l; ++j) {\n auto ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "#define MAX_ARR 100010\n\nint ret;\n\nvector<int> nex[MAX_ARR];\n\nint Trav ( int nod, int par ) {\n bool good = true;\n int i;\n int siz = 1;\n int subsize = -1;\n\n for ( i = 0; i < nex[nod].size(); i++ ) {\n int cur = nex[nod][i];\n if ( cur == par ) {\n continue;... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "const int SIZE=1E5+10;\nvector<int> adj[SIZE];\nclass Solution {\npublic:\n int n;\n vector<int> szs;\n void sub(int u,int p=-1) {\n szs[u]=1;\n for(auto &v:adj[u]) {\n if (v==p) continue;\n sub(v,u);\n szs[u]+=szs[v];\n }\n }\n int ans{}... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "#include \"bits/stdc++.h\"\nusing namespace std;\n \ntypedef long long ll;\ntypedef long double ld;\nconst int MN=1e5+10;\nconst int MOD=1e9+7;\nconst int bits=31;\nusing pi = pair<ll, ll>;\nusing ti = tuple<ll, ll, ll>;\n#define pb push_back\n#define mp make_pair\n#define mt make_tuple\n#define MAX LLONG_... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "#define MAXN 100005\n\n\nclass Solution {\npublic:\n int heads[MAXN];\n int vers[2*MAXN];\n int nexts[2*MAXN];\n int tot;\n\n void add(int i,int j){\n vers[++tot]=j,nexts[tot]=heads[i],heads[i]=tot;\n vers[++tot]=i,nexts[tot]=heads[j],heads[j]=tot;\n }\n\n int dfs(int nod... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\n private:\n int dfs(vector<int>adj[],int *count,int node){\n //cout<<\"Reached at node \"<<node<<endl;\n int nodei=0;\n int flag=true;\n int node0;\n for(int i=0;i<adj[node].size();i++){\n if(flag==true){\n if(i==0){\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "int n;\nvector<vector<int>> adj; \nint ans = 0;\n\nint dfs(int u, int a) {\n int d = 0;\n int k = -1;\n bool ok = true;\n for (int v: adj[u])\n if (v != a) {\n int w = dfs(v, u);\n if (k == -1) {\n k = w;\n } else {\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "vector<vector<int>> g;\nvector<int> parent;\nvector<int> sz;\nint numOfGood = 0;\nvoid dfs(int node, int par) {\n parent[node] = par;\n sz[node] = 1;\n bool isGood = true;\n int childSize = -1;\n\n for (auto v : g[node]) {\n if (v != par) {\n dfs(v, node);\n sz[n... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n\n int dfs(int node,vector<vector<int>>& adj,int &ans){\n int sz=0;\n int psz=sz;\n bool issame=1;\n int tot=0;\n for(int i=0;i<adj[node].size();i++){\n sz=dfs(adj[node][i],adj,ans);\n tot+=sz;\n if(i!=0 and s... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "typedef long long ll;\nvector<ll>gr[100009];\nll subtree[100009];\nll ans;\nvoid dfs(ll node, ll parr) {\n \n ll sum = 0, maxi = 0, mini = 1e6;\n for(ll i = 0; i < gr[node].size(); i++){\n ll child = gr[node][i];\n if(child == parr) continue;\n dfs(child, node);\n sum+=... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> child;\n vector<int> tbl;\n int count(int node)\n {\n if(tbl[node] != -1) return tbl[node];\n int cnt=0;\n for(int i: child[node])\n {\n cnt += count(i); \n }\n return tbl[node] = 1 + cnt;\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> child;\n vector<int> tbl;\n int countGoodNodes(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n child.resize(n);\n tbl.resize(n, -1);\n if (edges[0][0] == 0)\n for(int i = 0; i < n - 1; i++)\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n int count_node(int v,vector<vector<int>>&adjl,int& count)\n {\n int prev=-1;\n\n bool check=true;\n int total_node=0;\n \n for(auto adjv:adjl[v])\n {\n int cur=count_node(adjv,adjl,count);\n total_node+=cur;\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "const int N = 1e5 + 5;\nvector<vector<int>> adj(N);\nvector<pair<int, int>> s(N);\nvector<bool> ok(N), vis(N);;\nclass Solution {\npublic:\n int dfs(int v, int parent) {\n vis[v] = true;\n int cnt = 0;\n for (auto u : adj[v]) {\n if (!vis[u]) {\n cnt += dfs... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\n vector<vector<int> > adj;\n vector<int> size;\n int ans = 0;;\npublic:\n int dfs(int node){\n int k=-1;\n bool flag = true;\n int i;\n for(i=0;i<adj[node].size();i++){\n // if(adj[node][i]!=parent)\n size[node]+=dfs(adj[node][... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n void func(vector<vector<int>>& adj, vector<int>& size, int i) {\n int s = 1;\n for (int j = 0; j < adj[i].size(); j++) {\n if (size[adj[i][j]] == -1) {\n func(adj, size, adj[i][j]);\n }\n s += size[adj[i][j]];\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n void count(int i, vector<vector<int>>& graph, vector<int>& hash, int& ans) {\n int n = graph[i].size();\n if (n == 0) {\n hash[i] = 0;\n ++ans;\n return;\n }\n\n for (int j = 0; j < n; j++) {\n int next =... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "\nconst int N = 1e5+2;\nvector<int> adj[N];\nint dp[N];\nint ans=0;\n\nint dfs(int cur,int par){\n int sz = 1;\n int a=-1;\n bool ok = true;\n for(int child: adj[cur]){\n if(child != par){\n sz += dfs(child, cur);\n if(ok){\n if(a==-1)a=dp[child];\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "vector<int>adj[int(1e5)+1];\nvector<int>cnt(int(1e5)+1,0);\nvector<int>checkGoodNode(int(1e5)+1,-1);\nvector<int>parent(int(1e5)+1,-1);\nvoid reset(){\n for(int i=0;i<=int(1e5);i++){\n adj[i].clear();\n cnt[i]=0;\n checkGoodNode[i]=-1;\n parent[i]=-1;\n }\n\n}\nvoid dfs(in... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n\n // int countGoodNodes(vector<vector<int>>& edges) {\n // int n = edges.size();\n // vector<pair<int, int>> outNodes(n + 1);\n // vector<pair<int, int>> parent(n + 1);\n // int cnt = 0;\n // queue<pair<int, int>>q;\n\n // for(int i =... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n int ans = 0;\n struct node\n {\n int val;\n node* next;\n node(int v, node* n) : val(v), next(n) {}\n };\n int dfs(node** adjList, int& n, int from, int currentVal)\n {\n node* current = adjList[currentVal];\n int total = 1;\n... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n int s[100001],n,ret=0;\n vector<int> nodes[100001];\n\n void solve(int k){\n s[k]=1;\n int p=-1;\n for (int i=0;i<nodes[k].size();i++) {\n if (s[nodes[k][i]]!=-1) p=nodes[k][i];\n else{\n solve(nodes[k][i]);\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n const static int maxN = 1e5 + 10;\n vector<int> edge[maxN];\n int nodesz[maxN];\n int ans = 0;\n \n void dfs(int x, int p){\n int sz = 1;\n for(auto &u: edge[x]){\n if(u != p){\n dfs(u, x);\n sz += nodesz[u... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n int ans;\n int dp[200007];\n int dfs(int node, int par, vector<int> adj[], int n){\n \n vector<int> s;\n int val = 1;\n int c = -1;\n bool flag = true;\n for(auto child : adj[node]){\n if(child != par){\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\n int ans=0;\npublic:\n int dfs(int src,vector<int> &v,vector<int> adj[]){\n v[src]=1;\n int size=-1,totalsize=0;\n bool node=true;\n for(auto nbr:adj[src]){\n if(v[nbr]!=1){\n int s=dfs(nbr,v,adj);\n totalsize+=s;\... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n int dfs(int curr,int par,vector<int> &nodes,vector<int> adj[])\n {\n int sz=1;\n for(auto it:adj[curr])\n {\n if(it!=par)\n sz+=dfs(it,curr,nodes,adj);\n }\n return nodes[curr]=sz;\n }\n int countGoodNodes(vect... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n void dfs(int node, int parent, vector<int> adj[], vector<int> &subtreeSize, int &goodNodes) {\n subtreeSize[node] = 1; // Include the node itself in the subtree size\n bool isGood = true; // Assume this node is good unless proven otherwise\n int childSubt... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n int dfs(vector<int> &subtreeSize, vector<int> adj[], vector<int> &vis, int u) {\n vis[u] = 1;\n int sum = 1;\n for (int v : adj[u]) {\n if (!vis[v]) {\n sum += dfs(subtreeSize, adj, vis, v);\n }\n }\n sub... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> v;\n int p;\n vector<int> vs;\nint dfs(int n){\n if(vs[n]!=-1)\n return vs[n];\n int s=0;\n int t=0,k=-1;\n for(int i=0;i<v[n].size();i++){\n int a=dfs(v[n][i]);\n if(k!=-1){\n if(a!=k)\... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n int dfs(vector<int> adj[], vector<int>& visited, vector<int>& sub_size, int node) {\n int ans=0,mx=0,mn=INT_MAX;\n sub_size[node]=1;\n for (int v : adj[node]) {\n if (!visited[v]) {\n visited[v]=1;\n ans+=dfs(adj, ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>>graph;\n \n int dfs(int cur, int par, vector<int> &size){\n int sz = 1;\n \n for(auto nbr:graph[cur]){\n if(nbr != par) sz += dfs(nbr,cur, size);\n }\n size[cur] = sz;\n return sz;\n \n }\... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\nvoid findnode(vector<int>arr[],vector<int>& prr,vector<int>& trr,\nint node,int &count,int parent){\n prr[node]=1;\n int answer=0;\n for(auto it:arr[node]){\n if(!prr[it]){\n answer++;\n findnode(arr,prr,trr,it,count,node);\n }\n }\... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n int ans ;\n int dfsTraversal(vector<vector<int>>&list,int node,int parent){\n bool isGood = true;\n int totalNode = 0;\n int subTreeSize = -1;\n for(auto &i : list[node]){\n if(i==parent) continue;\n\n int newCount = dfsTra... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n vector<vector<int>> neighbors(n);\n for (auto& e : edges) {\n neighbors[e[0]].push_back(e[1]);\n neighbors[e[1]].push_back(e[0]);\n }\n \n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> graph;\n int dfs(int curr, int par, vector<int>& size) {\n int sz = 1;\n for(int nbr : graph[curr]){\n if(nbr != par) sz += dfs(nbr, curr, size);\n }\n size[curr] = sz;\n return sz;\n }\n\n int countGo... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> adj;\n vector<int> child;\n int countGoodNodes(vector<vector<int>>& edges) {\n int n = edges.size() + 1;\n adj.resize(n+1);\n for(auto& e : edges) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\nint ans = 0 ;\n int DFS( int src, vector<int>* adj ,vector<bool>& vis )\n {\n vis[src] = true;\n int count = 0;\n int childSize = 0;\n int prev = -1,curr;\n bool flag = true;\n for(int i =0 ; i<adj[src].size();i++)\n { ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\nint ans = 0 ;\n int DFS( int src, vector<int>* adj ,vector<bool>& vis )\n {\n vis[src] = true;\n int count = 0;\n int childSize = 0;\n int prev = -1,curr;\n bool flag = true;\n for(int i =0 ; i<adj[src].size();i++)\n { ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n void dfs(int node, int parent, vector<pair<vector<int>, int>> &adj, int &ans){\n int count = 0;\n for(auto it : adj[node].first){\n if(it == parent) continue;\n dfs(it, node, adj, ans);\n count += adj[it].second;\n }\n ... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n int countGoodNodes(vector<vector<int>>& edges) {\n int n=edges.size()+1;\n vector<vector<int>>adj(n);\n for(auto& edge:edges){\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n vector<int>size(... |
3,486 | <p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that th... | 0 | {
"code": "class Solution {\npublic:\n int dfs(int curr, vector<int> adj[], vector<int>&mp, int parent)\n {\n int cnt=1; \n for (int nei:adj[curr])\n {\n if (nei!=parent)\n {\n cnt+=dfs(nei,adj,mp,curr);\n }\n }\n mp[curr]=cnt;... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.