id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
319 | <p>There are <code>n</code> bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.</p>
<p>On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the <code>i<sup>th</sup></code> round, you toggle every <code>i</co... | 0 | {
"code": "class Solution {\npublic:\n int bulbSwitch(int n) {\n \n return (int)sqrt(n);\n }\n};",
"memory": "7100"
} |
319 | <p>There are <code>n</code> bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.</p>
<p>On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the <code>i<sup>th</sup></code> round, you toggle every <code>i</co... | 0 | {
"code": "class Solution {\npublic:\n int bulbSwitch(int n) {\n return sqrt(n);\n }\n};",
"memory": "7200"
} |
319 | <p>There are <code>n</code> bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.</p>
<p>On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the <code>i<sup>th</sup></code> round, you toggle every <code>i</co... | 0 | {
"code": "class Solution {\npublic:\n int bulbSwitch(int n) {\n return static_cast<int>(sqrt(n));\n \n }\n};",
"memory": "7200"
} |
319 | <p>There are <code>n</code> bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.</p>
<p>On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the <code>i<sup>th</sup></code> round, you toggle every <code>i</co... | 0 | {
"code": "class Solution {\npublic:\n int bulbSwitch(int n) {\n return static_cast<int>(sqrt(n)); \n }\n};",
"memory": "7300"
} |
319 | <p>There are <code>n</code> bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.</p>
<p>On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the <code>i<sup>th</sup></code> round, you toggle every <code>i</co... | 0 | {
"code": "class Solution {\npublic:\n int bulbSwitch(int n) {\n return (int)sqrt(n);\n }\n};",
"memory": "7300"
} |
319 | <p>There are <code>n</code> bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.</p>
<p>On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the <code>i<sup>th</sup></code> round, you toggle every <code>i</co... | 0 | {
"code": "class Solution {\npublic:\n int bulbSwitch(int n) {\n return sqrt(n);\n int ans = 0; \n vector<int>check(n,1);\n\n for(int i=2;i<=n;i++){\n for(int j=i;j<=n;j+=i){\n check[j-1]=abs(check[j-1]-1);\n }\n }\n for(auto x:check){\n if(x) ans+... |
319 | <p>There are <code>n</code> bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.</p>
<p>On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the <code>i<sup>th</sup></code> round, you toggle every <code>i</co... | 0 | {
"code": "class Solution {\npublic:\n int bulbSwitch(int n) {\n return sqrt(n);\n \n }\n};",
"memory": "7400"
} |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n sort(tasks.begin(), tasks.end());\n int result = 0, i, j;\n for(i=0; i<tasks.size(); ++i){\n for(j=i+1; j<tasks.size(); ++j)\n if(tasks[i]!=tasks[j]) break;\n \n j =... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n sort(tasks.begin(),tasks.end()); \n int result = 0 , i = 0;\n while(i < tasks.size()){\n int j;\n for(j=i+1;j<tasks.size();j++){\n if(tasks[i] != tasks[j])\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\n \npublic:\n int minimumRounds(vector<int>& tasks) {\n int n = tasks.size();\n sort(tasks.begin(),tasks.end());\n int i = 0; \n\n int cnt = 0;\n \n while( i < n ){\n int j = upper_bound(tasks.begin(),tasks.end(),tasks[i]) - tasks.be... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\n \npublic:\n int minimumRounds(vector<int>& tasks) {\n int n = tasks.size();\n sort(tasks.begin(),tasks.end());\n int i = 0; \n\n int cnt = 0;\n \n while( i < n ){\n int j = upper_bound(tasks.begin(),tasks.end(),tasks[i]) - tasks.be... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n int n=tasks.size();\n sort(tasks.begin(),tasks.end());\nint count=0;\n for(int i=0;i<tasks.size();i++){\nint count1=0;\nwhile(i+1<n&&tasks[i]==tasks[i+1]){\n count1++;\n i++;\n}\ncount1++;\ncout<<count1;\nint x=co... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n int count=1;\n vector<int>cnt;\n\n sort(tasks.begin(),tasks.end());\n\n if(tasks.size()==1)\n return -1;\n\n for(int i=1;i<tasks.size();i++){\n if(tasks[i]==tasks[i-1])\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n sort(tasks.begin(),tasks.end());\n vector<pair<int,int>> v;\n int cnt = 1;\n int curr = tasks[0];\n for(int i=1;i<tasks.size();i++){\n if(tasks[i] != curr){\n if(cnt == 1){r... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n int findi(int i,vector<int> &tasks,vector<int> &dp)\n {\n if(i==0)\n return 1e9;\n if(i==1)\n {\n if(tasks[i]==tasks[i-1])\n return 1;\n return 1e9;\n }\n if(i==2)\n {\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n#if 1\n\n int minimumRounds(vector<int>& tasks) {\n std::unordered_map<int, int> mm;\n int total = 0;\n int negtive = 0;\n int remainTask = tasks.size();\n for(const auto& i:tasks){\n int& cur = mm[i];\n switch(cur){\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n#if 1\n\n int minimumRounds(vector<int>& tasks) {\n std::unordered_map<int, int> mm;\n int total = 0;\n int negtive = 0;\n int remainTask = tasks.size();\n for(const auto& i:tasks){\n int& cur = mm[i];\n switch(cur){\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n#if 1\n\n int minimumRounds(vector<int>& tasks) {\n std::unordered_map<int, int> mm;\n int total = 0;\n int negtive = 0;\n int remainTask = tasks.size();\n for(const auto& i:tasks){\n int& cur = mm[i];\n switch(cur){\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n#if 1\n\n int minimumRounds(vector<int>& tasks) {\n std::unordered_map<int, int> mm;\n int total = 0;\n int negtive = 0;\n int remainTask = tasks.size();\n for(const auto& i:tasks){\n int& cur = mm[i];\n switch(cur){\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution\n{\npublic:\n int minimumRounds(vector<int> &tasks)\n {\n unordered_map<int, int> counter;\n for (int task : tasks)\n {\n counter[task] += 1;\n }\n int rounds = 0;\n for (auto diff : counter)\n {\n if (diff.second == 1... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n unordered_map <int , int> mp;\n for(auto x : tasks)\n mp[x]++;\n\nint count = 0;\nfor(auto x : mp)\n{\nint freq = x.second;\n\ncout<<freq<<\" \";\nif(freq == 1) return -1;\n\nif(freq ==2)\ncount +=1;\n\nelse\n{\ncount += freq /... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n unordered_map<int,int>mp;\n int n=tasks.size();\n for(int i=0;i<n;i++){\n mp[tasks[i]]++;\n }\n int c=0;\n for(auto x:mp){\n if(x.second==1) return -1;\n if( x... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n int result = 0 , size = tasks.size();\n unordered_map<int, int> myMap;\n for(int i = 0; i< size ;i++){\n int task = tasks[i];\n if (myMap.find(task)!=myMap.end()) \n myMap[task... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 0 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n unordered_map<int, int> mp;\n for (auto task: tasks)\n mp[task]++;\n\n int cnt = 0;\n for (auto val: mp) {\n int n = val.second;\n if (n == 1)\n return -1;\n\... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 1 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n\n unordered_map<int,int>mp;\n int n=tasks.size();\n int mini=INT_MAX;\n for(int i=0;i<n;i++)\n {\n mp[tasks[i]]++;\n \n }\n int ans=0;\n for(auto it:mp)\n {\n if(it.seco... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 1 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n\n unordered_map<int,int>mp;\n int n=tasks.size();\n int mini=INT_MAX;\n for(int i=0;i<n;i++)\n {\n mp[tasks[i]]++;\n \n }\n int ans=0;\n for(auto it:mp)\n {\n if(it.seco... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 2 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n unordered_map<int, int> Map;\n for(int i: tasks) Map[i]++;\n int ans = 0;\n for(auto it : Map){\n if(it.second == 1) return -1;\n ans += ceil(it.second / 3.0);\n }\n retu... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 2 | {
"code": "class Solution {\npublic:\n int helper(vector<int>&nums, int i, int n,vector<int>&dp){\n if(i>=n)return 0;\n\n if(dp[i]!=-1)return dp[i];\n int take2 = 1e9, take3 = 1e9;\n if(n-i>=2 && nums[i+1]==nums[i]){\n take2 = 1 + helper(nums, i+2,n,dp);\n }\n i... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 2 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n unordered_map<int, int> taskCount;\n int maxValue = INT_MIN;\n for (auto it : tasks) {\n taskCount[it]++;\n maxValue = max(maxValue,taskCount[it]);\n }\n cout<<maxValue<<endl;\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 2 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n map<int, int> m;\n int cnt = 0;\n for(int i = 0; i < tasks.size(); i++) {\n m[tasks[i]]++;\n }\n for(auto value : m) {\n if(value.second == 1) {\n return -1;\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 2 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n map<int, int> diffFreq;\n for (auto& task : tasks)\n ++diffFreq[task];\n\n int rounds = 0;\n for (auto [diff, freq] : diffFreq) {\n if (freq < 2)\n return -1;\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 2 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n map<int,int>mpp;\n for(int i=0;i<tasks.size();i++)mpp[tasks[i]]++;\n int cnt=0;\n for(auto i:mpp){\n if(i.second==1)return -1;\n if(i.second%3==0)cnt+=i.second/3;\n else if(... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n map<int,int>mp;\n int ans;\n for (auto it:tasks)\n {\n mp[it]++;\n }\n for (auto it:mp)\n {\n if (it.second<2) return -1;\n ans+=it.second/3+(it.second%... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n map<int,int>mp;\n int ans=0;\n for (auto it:tasks)\n {\n mp[it]++;\n }\n for (auto it:mp)\n {\n if (it.second<2) return -1;\n }\n for (auto it:mp)\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n \n map<int,int> m; // frequency\n for(int i=0; i<tasks.size(); i++)\n {\n m[tasks[i]]++;\n }\n int ans = 0;\n for(auto it=m.begin(); it!=m.end(); it++)\n {\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(nullptr);\n int n = tasks.size();\n map <int,int> hashMap;\n // value and count\n for(int i = 0;i<n;i++) {\n hashMap[tasks[i]] ++;\n }\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n map<int,int>m;\n for(int i=0;i<tasks.size();i++){\n m[tasks[i]]++;\n }\n int ct=0;\n for(auto it:m){\n int tt=it.second;\n if(tt==1){\n \n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n unordered_map<int,int> mp;\n for(auto it:tasks) mp[it]++;\n vector<int> nums;\n for(auto it:mp)\n {\n nums.push_back(it.second);\n if(it.second==1) return -1;\n }\n\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n unordered_map<int,int> mp;\n for(auto i:tasks){\n mp[i]++;\n }\n priority_queue<int,vector<int>,greater<int>> pq;\n for(auto i:mp){\n pq.push(i.second);\n }\n int ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n \n int minimumRounds(vector<int>& tasks) {\n unordered_map<int,int>mp;\n int count=0;\n int m=0;\n for(auto x:tasks){\n mp[x]++;\n m=max(m,mp[x]);\n }\n\t\t\n\t\t//find the max value of repeating number and create th... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "#include <vector>\n#include <unordered_map>\n#include <iostream>\n#include <climits>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n unordered_map<int, int> freqMap;\n \n // Count the frequency of each difficulty level\n for (i... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "\nclass Solution \n{\npublic:\n int minimumRounds(vector<int>& tasks) \n {\n unordered_map<int, int> mp;\n \n \n for (auto & x: tasks) \n {\n mp[x]++;\n }\n \n int maxFreq = 0;\n for (auto& x : mp) \n {\n maxFr... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n unordered_map<int,int>mp;\n int count=0;\n int m=0;\n for(auto x:tasks){\n mp[x]++;\n m=max(m,mp[x]);\n }\n vector<int> dp(m + 1, INT_MAX / 2);\n dp[0]=0;\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "#define ll long long int\nclass Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n unordered_map<ll,ll> m;\n for(int i=0;i<tasks.size();i++) {\n m[tasks[i]]++;\n }\n ll sum=0;\n for(auto i:m) {\n if(i.second<=1) {return -1;}\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "using ll = long long int;\n\nclass Solution {\npublic:\n ll solve(ll counts) {\n if (counts <= 1) return -1;\n if (counts == 2 || counts == 3) return 1;\n ll y = counts / 3, x = ceil((double)(counts - 3*y)/2);\n\n while (true) {\n if (x == -1 || y == -1) break;\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n vector<int> v;\n map<int,int> mp;\n for(int i=0;i<tasks.size();i++){\n mp[tasks[i]]++;\n }\n for(auto it=mp.begin();it!=mp.end();it++){\n v.push_back(it->second);\n }\n ... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n bool IsPossible(int mid , vector <int> &nums) {\n int n = nums.size();\n\n int days = 0;\n\n for(int i = 0 ; i < n ; i++) {\n if(nums[i] == 1) return false;\n \n else {\n if(nums[i] % 3 == 0) days += nums[i]... |
2,362 | <p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to com... | 3 | {
"code": "class Solution {\npublic:\n int minimumRounds(vector<int>& tasks) {\n vector<int> v;\n map<int,int> mp;\n for(int i=0;i<tasks.size();i++){\n mp[tasks[i]]++;\n }\n for(auto it=mp.begin();it!=mp.end();it++){\n v.push_back(it->second);\n }\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int longestPath(vector<int>& parents, string s) {\n int n = parents.size();\n // Store the number of children for each node\n vector<int> graph(n, 0); \n\n // Count the number of children for each node\n for (int i = 1; i < n; i++) \n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "const int N = 100010;\nint res;\nint h[N], e[N], ne[N], idx;\nvoid add(int a, int b) {\n e[idx] = b, ne[idx] = h[a], h[a] = idx++;\n}\nclass Solution {\n int dfs(string &s, int u) {\n int first = -1, second = -1;\n for (int i = h[u]; ~i; i = ne[i]) {\n int j = e[i];\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "const int N = 1e5 + 100;\nvector<int> tree[N];\nstring s;\nint fin;\n\nclass Solution {\nprivate:\n int dfs(int v, int par) {\n int fi = 0; \n int se = 0; \n for (auto u : tree[v]) {\n if (u == par) {\n continue;\n }\n int path = dfs(u... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "const int N = 1e5 + 100;\nvector<int> tree[N];\nstring s;\nint fin;\n\nclass Solution {\nprivate:\n int dfs(int v, int par) {\n int fi = 0; // First longest path length\n int se = 0; // Second longest path length\n for (auto u : tree[v]) {\n if (u == par) {\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\n int res = 0;\npublic:\n int solve(int node, vector<int> adj[]){\n int maxVal = 0, sMax = 0;\n for(auto x:adj[node]){\n int it = solve(x, adj);\n if(it > maxVal){\n sMax = maxVal;\n maxVal = it;\n }\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\n\npublic:\n int longestPath(vector<int>& parent, string s) {\n int ret = 1;\n int n = parent.size();\n\n unordered_map<int,pair<int,int>> mp;\n vector<int> indeg(n);\n for(int num: parent){\n if(num==-1){ continue; }\n indeg[num]... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int ans = 1;\n vector<int> child[100001];\n int dfs(int cur,string& s){\n if(child[cur].empty())return 1;\n int big1 = 0;\n int big2 = 0;\n for(auto &childNode : child[cur]){\n int tmp = dfs(childNode,s);\n // ans = max(... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int ans;\n\n int dfs(vector<int> adj[], int node, string &s, char parentChar) {\n int m1 = 0, m2 = 0; \n \n\n for (auto &child : adj[node]) {\n int x = dfs(adj, child, s, s[node]); \n \n if (x > m1) {\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n vector<int> child[100001];\n int ans;\n int dfs(string &s, int curr_node)\n {\n if(child[curr_node].empty())return 1;\n int mx1 = 0, mx2 =0;\n // traversing over all the child nodes of the curr_node\n for(auto &child_node : child[curr_node])\n... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int ans=1;\n int f(vector<int>adj[],string &s,int node){\n\n int mx1=0,mx2=0;\n if(adj[node].empty())return 1;\n\n\n for(auto it:adj[node]){\n \n int res=f(adj,s,it); \n ans=max(ans,res);\n if(s[node]==s[it])continue;\n\n if(res>... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int ans=1;\n int solve(int node, vector<int>adj[], string &s){\n if(adj[node].empty()) return 1;\n int maxi1=0,maxi2=0;\n \n for(auto it:adj[node]){\n int res=solve(it,adj,s);\n if(s[node]!=s[it]){\n if(res>m... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n vector<int> child[100001];\n int ans;\n int dfs(string &s, int curr_node)\n {\n if(child[curr_node].empty())return 1;\n int mx1 = 0, mx2 =0;\n // traversing over all the child nodes of the curr_node\n for(auto &child_node : child[curr_node])\n... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\nint ans=0;\n int help(int node,vector<int> adj[],string& s){\n if(adj[node].empty()) return 1;\n int a=0,b=0;\n for(int child:adj[node]){\n int k=help(child,adj,s);\n ans=max(ans,k);\n if(s[node]==s[child])continue;\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int func(vector<int> adj[], int i, int &ans, string &s) {\n if(adj[i].empty()) return 1;\n int m1 = 0, m2 = 0;\n // int an = 0;\n for(auto it : adj[i]) {\n int an = func(adj, it, ans, s);\n if(s[i] == s[it]) continue;\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n vector <int> adj[100005];\n pair <int, int> res[100005];\n string s_glob = \"\";\n int seen = 0;\n\n void dfs(int curr){\n int c_1 = -1, c_2 = -1;\n int ma_two = 1;\n for (auto u : adj[curr]){\n dfs(u);\n if (s_glob[u] !=... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n vector <int> adj[100005];\n pair <int, int> res[100005];\n string s_glob = \"\";\n int seen = 0;\n\n void dfs(int curr){\n int c_1 = -1, c_2 = -1;\n int ma_two = 1;\n for (auto u : adj[curr]){\n dfs(u);\n if (s_glob[u] !=... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int ans = 0 ;\n void dfs(int ver, int par, vector<int> adj[], vector<int> &dp, string &s){\n int max1 = 0, max2 = 0 ;\n for(int ch : adj[ver]){\n if(ch==par)continue ;\n dfs(ch,ver,adj,dp,s) ;\n if(s[ch] != s[ver]){\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\nint up[100001], down[100001];\nint mx = 0;\nvoid dfs(int node, int par, string& s, vector<int> adj[]) {\n up[node] = 0;\n if(s[node] != s[par]) up[node] = 1 + up[par];\n int h1 = up[node], h2 = 0;\n for(auto& b: adj[node]) {\n // if(b != par) {\n dfs... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\nprivate:\n int dfs(int curr, vector<int> adjList[], vector<int>& dp, string& s, int& global) {\n if (dp[curr] != -1) return dp[curr];\n int total = 1;\n int maxPoss = 0;\n int secondMaxPoss = 0;\n\n char currChar = s[curr];\n\n for (auto nei : ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\nint ans=1;\n\nvoid dfs(int i,int parent,vector<int>adj[],vector<int>&p,string& s,vector<int>&dist){\n dist[i]=1;\n \n for(auto child:adj[i]){\n dfs(child,i,adj,p,s,dist);\n if(s[child]!=s[i]){\n ans=max(ans,dist[i]+dist[child]);\n dist[i]=max(dist... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "struct sol {\n int max1, max2, max;\n sol() : max1(0), max2(0), max(1) {}\n sol(int max1, int max2, int max) : max1(max1), max2(max2), max(max) {}\n};\n\nclass Solution {\npublic:\n void dfs(int v, vector<int> adj[], vector<sol> &dp, string &s) {\n \n for(auto child : adj[v]) {\n\... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "struct sol {\n int max1, max2, max;\n sol() : max1(0), max2(0), max(1) {}\n sol(int max1, int max2, int max) : max1(max1), max2(max2), max(max) {}\n};\n\nclass Solution {\npublic:\n void dfs(int v, vector<int> adj[], vector<sol> &dp, string &s) {\n \n for(auto child : adj[v]) {\n\... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n // declared here, no need to pass in recursive functions\n vector<int>* children; \n string s;\n int maxSoFar = 1;\n\n int recur(int node)\n {\n int n = children[node].size();\n\n int first = 0; // first highest path in my subtree\n in... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int longestPath(vector<int>& parent, string s) {\n int n = parent.size();\n vector<int> g[n];\n for (int i = 1; i < n; i++) {\n g[parent[i]].push_back(i);\n }\n int ans = 0;\n function<int(int)> dfs = [&](int i) -> int {\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n vector<int>tree[100005];\n int ans;\n int FindLongestLength( int curr_node, string&s ){\n\n int best=0;\n int second_best=0;\n\n for(auto &child_node: tree[curr_node])\n {\n int len=FindLongestLength(child_node,s);\n \n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int longestPath(vector<int>& parent, string s) {\n int n = parent.size();\n vector<int> g[n];\n for (int i = 1; i < n; i++) {\n g[parent[i]].push_back(i);\n }\n int ans = 0;\n function<int(int)> dfs = [&](int i) -> int {\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int ans = 0;\n vector<vector<int>> adj;\n string s;\n int longestPath(vector<int>& parent, string ss) {\n vector<int> v;\n s = ss;\n adj = vector<vector<int>> (parent.size(), v);\n for(int i = 1; i < parent.size(); i++) adj[parent[i]].push... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "string s1;\nvector<int> b;\nclass Solution {\npublic:\n int dfs( int node , vector<vector<int>> &adj , int &ans)\n {\n int ma=0,ma2=0;\n for( auto &it : adj[node])\n {\n int x=dfs(it,adj,ans);\n if(s1[it]!=s1[node]){\n if(ma<x){\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int longestPath(vector<int>& parent, string s) {\n int root = 0;\n int n = parent.size();\n vector<vector<int>> children(n);\n int i;\n for (i = 0; i < n; ++i) {\n if (parent[i] == -1) root = i;\n else {\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 0 | {
"code": "class Solution {\npublic:\n int dfs(int node, const vector<vector<int>>& adj, const string& s, int& res) {\n int max1 = 0;\n int max2 = 0;\n\n for (int neighbour : adj[node]) {\n int ans = dfs(neighbour, adj, s, res);\n if (s[node]==s[neighbour]) continue;\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\n int maxi = 0;\n\n int count(vector<vector<int>>& adj, int node, vector<int>& vis, string& s) {\n \n int longest = 0, second_longest = 0;\n \n for (auto& child : adj[node]) {\n \n\n int length = count(adj, child, vis, s);\n\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n int ans = 1;\n vector<vector<int>> adj;\n void solve(int node, vector<int> &count, string &s){\n\n for(int i: adj[node]){\n solve(i, count, s);\n\n if(s[node]!=s[i]){\n if(count[node]>1){\n ans = max(ans, co... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n int longestPath(vector<int>& parent, string s) {\n s_ = s;\n n = parent.size();\n children.resize(n, std::vector<int>{});\n dp.resize(n, 0);\n for (int i = 0; i < n; i++) {\n if (parent[i] != -1) {\n children[parent... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n vector<vector<int>>adj;\n vector<int>mp;\n int ans=0;\n int longestPath(vector<int>& parent, string s) {\n //we are going to make a directed graph adj with the help of parent vector\n //we are going to calculate longest path from every vertex and use it... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\n list<int>* adj;\n void addEdge(int u, int v) { adj[u].push_back(v); }\n int helper(int u, string& s, int& ans) {\n\n int l = 0, r = 0;\n for (auto it = adj[u].begin(); it != adj[u].end(); it++) {\n int v = *it;\n int dist = helper(v, s, ans);\... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n void dfs(int x,int pa,vector<vector<int>> &v,vector<int>&dp,string &s){\n int a=1;\n for(auto it:v[x]){\n if(it!=pa){\n dfs(it,x,v,dp,s);\n if(s[it]!=s[x])a=max(a,dp[it]+1);\n }\n }\n dp[x]=a;\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n#define ll long long\n\n ll find(int i, int par, string &s, vector<vector<int>> &adj, vector<ll>& ans) {\n ll in = 1;\n ll x = 0, y = 0;\n\n for (auto it : adj[i]) {\n if (it == par) continue;\n ll temp = find(it, i, s, adj, ans);\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\n int res = 0;\n int dp[100001][2];\n int solve(int node,vector<int> adj[], string &st, int flag){\n if(dp[node][flag] != -1)return dp[node][flag];\n int ans = 0;\n int f = 0, s = 0;\n for(auto it:adj[node]){\n if(flag){\n if(s... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n int result=0;\n int dfs(int node,int par,vector<int> adj[],string &s){\n\n int maxi1=0,maxi2=0;\n\n for(auto it:adj[node]){\n if(it!=par){\n int res=dfs(it,node,adj,s);\n\n if(s[it]==s[node]) continue;\n\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n int result;\n int dfs(vector<int> adj[], string &s, int curr , int parent){\n int longest = 0;\n int secondLongest = 0;\n\n for(int & child : adj[curr]){\n if(child == parent) continue;\n int child_path = dfs(adj , s, child , curr);\n if(s... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n vector<vector<int>> graph;\n vector<int> indp;\n void indfs(int node, int depth, vector<int>& parent, string& s){\n indp[node]=1;\n\n for(auto &child:graph[node]){\n if(child!=parent[node]){\n indfs(child, depth+1, parent, s);\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "#include <vector>\n#include <string>\n#include <functional> // Include for std::function\n\nclass Solution {\npublic:\n // Function to find the longest path where each character is different \n // from its parent in a tree defined by parent-child relationships and node values given by string s.\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\nprivate:\n int ans = 0;\n\n int dfs(vector<int> adj[], string &s, int &n, int node, int par){\n char ch = s[node];\n int firstMaxi = 0, secondMaxi = 0;\n\n for(int nbr : adj[node]){\n if(nbr != par){\n int len = dfs(adj, s, n, nbr, node... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n int dfs(int node, int pp, int &res, vector<int>graph[],string &s){\n\n int val = 1;\n int max1 = 0 ; int max2 = 0 ;\n for(auto ch : graph[node]){\n\n if(ch == pp)continue;\n // if(s[node] == s[ch])continue ;\n\n int b = df... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n int sp[100001];// single path\n int dp[100001];// double path\n vector<int>g[100001];\n string s;\n int ans;\n void dfs(int node,int p){\n sp[node]=1;// only one node yet\n dp[node]=1;\n int max1=0,max2=0;\n for(auto v:g[node]){\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n int longestPath(vector<int>& parent, string s) {\n _edges.resize(parent.size());\n for (int i = 1; i < parent.size(); i++)\n _edges[parent[i]].push_back(i);\n _s = &s;\n int max_inner, max_hanging;\n find(0, max_inner, max_hanging... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n\n void dfs(int node,int parent,vector<int> adj[],string& s,int& ans,vector<int>& dp){\n dp[node]=1;\n int a=-1,b=-1;\n for(auto &i:adj[node]){\n if(i==parent) continue;\n dfs(i,node,adj,s,ans,dp);\n if(dp[i]>=a && s[i]!=s[... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n int longestPath(vector<int>& parent, string s) {\n int N = s.size();\n vector<vector<int>> tree (N, vector<int>());\n for (int i = 1; i < N; i++) {\n tree[parent[i]].push_back(i);\n }\n\n int ans = 1;\n vector<int> dp(N, 1)... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n int give(int u,vector<vector<int>>& adj,vector<int>& vis,vector<int> &h){\n int n = vis.size();\n \n int maxval = 0, maxnode;\n auto dfs = [&] (auto&& dfs,int node,int par,int lev) -> void {\n h[node] = lev;\n if(h[node] > max... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "void comp_find(int i, vector<vector<int>> &g, vector<int> &vis) {\n vis[i]=1;\n for (auto &v:g[i]) if (!vis[v])\n comp_find(v,g,vis);\n}\n\nvoid dfs(int i, int par, int dep, int &max_dep_node, int &maxdep, vector<vector<int>>&g) {\n if (dep>maxdep) {\n maxdep=dep;\n max_dep_no... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n void dfs(int v, int len, vector<vector<int>> &adj,\n vector<int> &vis, int &maxlen, int &node) {\n len++;\n vis[v] = 1;\n if (maxlen < len) {\n maxlen = len;\n node = v;\n }\n for (auto u : adj[v]) {\n ... |
2,364 | <p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size... | 1 | {
"code": "class Solution {\npublic:\n\n //can think of this as connected component, edges between nodes of adjacent just can't be reached, delete edge\n //then within each go as far as you can???\n //node is connected only to parent and to children. If parent is same letter, child is now root. If child is s... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.