id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "class Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n stack<ListNode*>st;\n ListNode* temp=head;\n while(temp!=NULL){\n st.push(temp);\n temp=temp->next;\n }\n temp=head;\n ListNode* c = new ListNode(0);\n int carry=0;\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "class Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n ListNode* t = head;\n vector<int> ans;\n while(t){\n ans.push_back(t->val);\n t = t->next;\n }\n reverse(ans.begin(),ans.end());\n vector<int> actualans;\n int y = 0;...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "class Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n ListNode* t = head;\n vector<int> ans;\n while(t){\n ans.push_back(t->val);\n t = t->next;\n }\n reverse(ans.begin(),ans.end());\n vector<int> actualans;\n int y = 0;...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "class Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n ListNode* t = head;\n vector<int> ans;\n while(t){\n ans.push_back(t->val);\n t = t->next;\n }\n reverse(ans.begin(),ans.end());\n vector<int> actualans;\n int y = 0;...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
2
{ "code": "class Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n ListNode* t = head;\n vector<int> ans;\n while(t){\n ans.push_back(t->val);\n t = t->next;\n }\n reverse(ans.begin(),ans.end());\n vector<int> actualans;\n int y = 0;...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n pair<...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "static const int fastIO = [] {\n\tstd::ios_base::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);\n\treturn 0;\n}();\n\nclass Solution {\nprivate:\n\tListNode* copy(ListNode* node) {\n\t\tif (!node) return nullptr;\n\t\treturn new ListNode(node->val, copy(node->next));\n\t}\n\n\tint s...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "class Solution {\npublic:\n ListNode* doubleIt(ListNode* head){\n if (!head) return NULL;\n string s=\"\";\n ListNode* temp=head;\n while(temp!=NULL){\n s+=to_string(temp->val);\n temp=temp->next;\n }\n int sum=0;\n for(int i=s.size(...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "// /**\n// * Definition for singly-linked list.\n// * struct ListNode {\n// * int val;\n// * ListNode *next;\n// * ListNode() : val(0), next(nullptr) {}\n// * ListNode(int x) : val(x), next(nullptr) {}\n// * ListNode(int x, ListNode *next) : val(x), next(next) {}\n// * };\n// *...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for sing...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n...
2,871
<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p> <p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
3
{ "code": "class Solution {\n ListNode* solve(vector<int>&res){\n if(res.empty()){\n return NULL;\n }\n ListNode* head = new ListNode(res[0]);\n ListNode*temp = head;\n for(int i = 1; i< res.size(); i++){\n temp -> next = new ListNode(res[i]);\n ...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "using ll = long long;\n\nclass Solution {\npublic:\n long long minimumReplacement(vector<int>& nums) {\n ios_base::sync_with_stdio(0); \n cin.tie(0);\n int n = nums.size();\n int prev = INT_MAX;\n ll ops = 0;\n for (int i = n - 1; i >= 0; --i) {\n int...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "using ll = long long;\n\nclass Solution {\npublic:\n long long minimumReplacement(vector<int>& nums) {\n ios_base::sync_with_stdio(0); \n cin.tie(0);\n cout.tie(0);\n int n = nums.size();\n int prev = INT_MAX;\n ll ops = 0;\n for (int i = n - 1; i >= 0; -...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\n\nclass Solution {\npublic:\n long long minimumReplacement(vector<int>& nums) {\n long long count = 0, last = nums[nums.size() - 1];\n\n for (int i = nums.size() - 2; i >= 0; i--) ...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\n\nclass Solution {\npublic:\n long long minimumReplacement(vector<int>& nums) {\n long long count = 0, last = nums[nums.size() - 1];\n\n for (int i = nums.size() - 2; i >= 0; i--) ...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "class Solution {\npublic:\n //fact 1: if a number is broken into n parts -> no of operations = n-1\n //fact 2: traverse the array in reverse order because if we go in front then we might need to backtrack and other previous elements\n //fact 3: divide the ith number such that is is smaller than i+...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "class Solution {\npublic:\n long long minimumReplacement(vector<int>& nums) {\n ios_base::sync_with_stdio(false); cin.tie(NULL);\n int cur = nums.back();\n long long ans = 0;\n for(int i = nums.size() - 2; i >= 0; i--){\n if (nums[i] > cur && nums[i] % cur == 0) {\...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "class Solution {\npublic:\n long long minimumReplacement(vector<int>& nums) {\n int n=nums.size();\n long long ans=0;\n int last=nums[n-1];\n for(int i=n-2;i>=0;i--)\n {\n if(nums[i]>last)\n {\n int k=ceil(double(nums[i])/last);\n ...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "class Solution {\npublic:\n long long minimumReplacement(vector<int>& nums) {\n long long ans=0;\n int n=nums.size();\n int minhighest=nums[n-1];\n for(int i=n-2;i>=0;i--){\n int times=nums[i]/minhighest;\n if(nums[i]%minhighest!=0){\n tim...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "class Solution {\npublic:\n long long minimumReplacement(vector<int>& nums) {\n long long ans = 0;\n\n for(int i = nums.size()-2;i>=0;i--){\n if(nums[i]>nums[i+1]){\n int step = nums[i]/nums[i+1];\n int rem = nums[i]%nums[i+1];\n if(r...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "class Solution {\npublic:\n long long minimumReplacement(vector<int>& nums) {\n int n = nums.size();\n int last = nums[n-1]; \n long long ans = 0; \n\n for (int i = n - 2; i >= 0; --i) {\n if (nums[i] > last) { \n int t = nums[i] / last; \n ...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "class Solution {\npublic:\n long long minimumReplacement(vector<int>& nums) {\n int n=nums.size();\n long long count=0;\n for(int i=n-2;i>=0;i--){\n cout<<count<<\" \";\n int c=(nums[i]+nums[i+1]-1)/nums[i+1];\n nums[i]/=c;\n count+=c-1;\n...
2,450
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation you can replace any element of the array with <strong>any two</strong> elements that <strong>sum</strong> to it.</p> <ul> <li>For example, consider <code>nums = [5,6,7]</code>. In one operation, we can replace <code>nums[1...
0
{ "code": "class Solution {\npublic:\n long long minimumReplacement(vector<int>& nums) {\n long long ans = 0;\n reverse(nums.begin(), nums.end());\n int prev = nums.front();\n for (int i = 1; i < nums.size(); ++i) {\n int cl = (nums[i] + prev - 1) / prev;\n ans += ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class UnionFind {\n\npublic:\n\n\tvector<int> parent;\n\n\texplicit UnionFind(int numberOfNodes) {\n\t\tparent.resize(numberOfNodes);\n\t\tfor (int node = 0; node < numberOfNodes; ++node) {\n\t\t\tparent[node] = node;\n\t\t}\n\t}\n\n\tint findParent(int node) {\n\t\tif (parent[node] != node) {\n\t\t\tparen...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class UnionFind {\n\npublic:\n\n\tvector<int> parent;\n\n\texplicit UnionFind(int numberOfNodes) {\n\t\tparent.resize(numberOfNodes);\n\t\tfor (int node = 0; node < numberOfNodes; ++node) {\n\t\t\tparent[node] = node;\n\t\t}\n\t}\n\n\tint findParent(int node) {\n\t\tif (parent[node] != node) {\n\t\t\tparen...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\n vector<int> parent, size;\n int findParent(int p){\n return parent[p] == p ? p : findParent(parent[p]);\n }\n\n void connect(int& u, int& v){\n int p1 = findParent(u);\n int p2 = findParent(v);\n\n if(p1 == p2 || size[p1] == 0 || size[p2] == 0) ret...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\nprivate:\n vector<int> __size, __par;\n int find(int a) {\n if(__par[a] == -1) return a;\n return __par[a] = find(__par[a]);\n } \n\n void merge(int a, int b) {\n a = find(a);\n b = find(b);\n if(a==b) return;\n if(__size[a] < __size[b...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n vector<int> p,s;\n int fi(int i){\n if(i == p[i])return i;\n return p[i] = fi(p[i]);\n }\n void un(int x,int y){\n int xp = fi(x);\n int yp = fi(y);\n if((xp == yp) or s[xp]==0 or s[yp]==0){\n return;\n }\n ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class DSU {\npublic:\n std::vector<int> parent;\n DSU(int n) : parent(n, -1) {} // Initialize all elements to -1\n\n int find(int x) {\n if (parent[x] < 0) return x;\n return parent[x] = find(parent[x]); // Path compression\n }\n\n void unite(int u, int v) {\n int pare...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int find(vector<int>& ds,int a)\n {\n return ds[a]<0?a:ds[a]=find(ds,ds[a]);\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& rest) {\n unordered_set<int> ms(rest.begin(),rest.end());\n vector<int> ds(n,-1);\n for(a...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "/*\n- classic way: use edges to create {node: [connected nodes]} hash. Then do dfs.\n- union-find: \n1. start from node 0: for a connected node, we set roots to be the smallest node among roots.\n2. when reach restricted node: just don't join. \n3. at the end, find root and count those with root 0\n\nWrong...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int find(vector<int>& parent, int u) {\n if (parent[u] < 0) {\n return u;\n }\n return parent[u] = find(parent, parent[u]);\n }\n void unite(vector<int>& parent, int i, int j) {\n int u = find(parent, i);\n int v = find(pare...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n vector<int>parents;\n \n void makeUnion(int nodeA , int nodeB){\n int parentA = find(nodeA);\n int parentB = find(nodeB);\n \n if(parentA<parentB){\n parents[parentB] = parentA;\n return;\n }\n parents[pare...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "/*\nImportant to note that when doing Union, it is not necessary that it gets unioned to node 0 only. So when want size of node 0, get parent of 0 and then use it's size.\n\nDSU was my initial approach\n\nWhen using DFS, no need for extra restricted set/array. Just mark it as visited.\n*/\n/*\nclass Soluti...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n vector<int> root, rank;\n int FindRoot(int a) {\n if (a == root[a]) return a;\n root[a] = FindRoot(root[a]);\n return root[a];\n }\n void merge(int a, int b) {\n int rootA = FindRoot(root[a]);\n int rootB = FindRoot(root[b]);\n ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n vector<int> root, rank;\n int FindRoot(int a) {\n if (a == root[a]) return a;\n root[a] = FindRoot(root[a]);\n return root[a];\n }\n void merge(int a, int b) {\n int rootA = FindRoot(root[a]);\n int rootB = FindRoot(root[b]);\n ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "struct Disjoint{\n vector<int>parent;\n Disjoint(int n){\n parent.resize(n);\n for(int i{};i<n;++i)parent[i]=i;\n }\n int findParent(int x){\n if(parent[x]!=x)parent[x]=findParent(parent[x]);\n return parent[x];\n }\n void merge(int x, int y){\n int a = ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n int result = 0;\n vector<int> roots(n);\n unordered_set<int> obsticles(restricted.begin(), restricted.end());\n\n for (int i = 0; i < n; ++i) {\n roots[i]...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int findP(int n, vector<int>& parent) {\n if (n != parent[n])\n parent[n] = findP(parent[n], parent);\n return parent[n];\n }\n int reachableNodes(int n, vector<vector<int>>& edges,\n vector<int>& restricted) {\n map...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class UnionFind {\npublic:\n UnionFind(int sz) : root(sz), rank(sz) {\n for (int i = 0; i < sz; i++) {\n root[i] = i;\n rank[i] = 1;\n }\n }\n\n int find(int x) {\n if (x == root[x]) {\n return x;\n }\n return root[x] = find(root[...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class DisjointSet {\npublic:\n vector<int> rank, parent, size;\n DisjointSet(int n) {\n rank.resize(n + 1, 0);\n parent.resize(n + 1);\n size.resize(n + 1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class disjointst\n{\n public:\n vector<int>parent,size;\n\n disjointst(int n)\n {\n parent.resize(n,0);\n size.resize(n,1);\n for(int i=0;i<n;i++) parent[i]=i;\n }\n\n ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class disjointst\n{\n public: \n vector<int>parent,size;\n\n disjointst(int n)\n {\n parent.resize(n,0);\n size.resize(n,1);\n\n for(int i=0;i<n;i++) parent[i]=i;\n }\n\n int findupr(int x)\n ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> visitableVerticles(n);\n vector<bool> allowed(n, true);\n for (int i = 0; i < restricted.size(); i++){\n allowed[restricted[i]] = false;\n ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int find(int x,vector<int>& parent){\n if(x==parent[x]){\n return x;\n }\n return find(parent[x],parent);\n}\nvoid union1(int x,int y,vector<int>& parent){\n int px = find(x,parent);\n int py = find(y,parent);\n if(px<py){\n parent[py] = px;\n ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int cnt=0;\n void d(int node,int p,vector<int>adj[])\n {\n // cout<<node<<endl;\n cnt++;\n for(auto c:adj[node])\n {\n if(c==p)continue;\n d(c,node,adj);\n }\n // return 0;\n }\n int reachableNodes(in...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class UnionFind {\n vector<int> root, rank;\npublic:\n UnionFind(int n) : root(n), rank(n, 1) {\n iota(root.begin(), root.end(), 0);\n }\n int find(int x) {\n if (x != root[x]) {\n return find(root[x]);\n }\n return root[x];\n }\n void join(int x, in...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class UnionFind {\n vector<int> root, rank;\npublic:\n UnionFind(int n) : root(n), rank(n, 1) {\n iota(root.begin(), root.end(), 0);\n }\n int find(int x) {\n if (x != root[x]) {\n return find(root[x]);\n }\n return root[x];\n }\n void join(int x, in...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class UnionFind {\n vector<int> root, rank;\npublic:\n UnionFind(int n) : root(n), rank(n, 1) {\n iota(root.begin(), root.end(), 0);\n }\n int find(int x) {\n if (x != root[x]) {\n return find(root[x]);\n }\n return root[x];\n }\n void join(int x, in...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class UnionFind {\n vector<int> root, rank;\npublic:\n UnionFind(int n) : root(n), rank(n, 1) {\n iota(root.begin(), root.end(), 0);\n }\n int find(int x) {\n if (x != root[x]) {\n return find(root[x]);\n }\n return root[x];\n }\n void join(int x, in...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class UnionFind {\n vector<int> root, rank;\npublic:\n UnionFind(int n) : root(n), rank(n, 1) {\n iota(root.begin(), root.end(), 0);\n }\n int find(int x) {\n if (x != root[x]) {\n return find(root[x]);\n }\n return root[x];\n }\n void join(int x, in...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, const vector<vector<int>>& edges, const vector<int>& restricted) {\n vector<int> used(n);\n for (auto & x : restricted)\n used[x] = -1;\n\n vector adj(n, vector<int>());\n for (const auto & a : edges) {\n ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, const vector<vector<int>>& edges, const vector<int>& restricted) {\n vector<int> used(n);\n for (auto & x : restricted)\n used[x] = -1;\n\n vector adj(n, vector<int>());\n for (const auto & a : edges) {\n ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, const vector<vector<int>>& edges, const vector<int>& restricted) {\n vector<int> used(n);\n for (auto & x : restricted)\n used[x] = -1;\n\n vector adj(n, vector<int>());\n for (const auto & a : edges) {\n ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> graph(n);\n bool seen[n];\n memset(seen, false, sizeof(seen));\n for (int num : restricted)\n seen[num] = true;\n for (auto& e ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int dfs(int index, int parent, vector<vector<int>>& graph)\n {\n int answer = 0;\n std::cout << index << std::endl;\n for (int val : graph[index])\n {\n if (val != parent)\n {\n answer += dfs(val, index, grap...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n for(int i=0;i<n-1;i++) {\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n bool...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges,\n vector<int>& restricted) {\n\n vector<int> adj[n];\n int start = 0;\n set<int> rest(restricted.begin(), restricted.end());\n\n for (int i = 0; i < edges.size(); i++) {\n ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int>adj[n];\n for(int i=0;i<n-1;i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n vector...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<bool> notVisited(n, true);\n vector<vector<int>> g(n);\n unordered_set<int> r(restricted.begin(), restricted.end());\n stack<int> s;\n int count = 1;\n...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<bool> notVisited(n, true);\n vector<vector<int>> g(n);\n unordered_set<int> r(restricted.begin(), restricted.end());\n stack<int> s;\n int count = 1;\n...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int> adj[n];\n vector<int> res(n,0),vis(n,0);\n for(auto it:restricted){\n res[it]=1;\n }\n for(auto it:edges){\n if(!res[it[0]] ...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\n int visited[100001];\n vector<int> ke[100001];\nprivate:\n void dfs(int u)\n {\n visited[u]=1;\n for(int x:ke[u])\n {\n if(visited[x]==0)\n {\n dfs(x);\n }\n }\n }\npublic:\n int reachableNodes(...
2,445
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p> <p>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 there is an edge b...
0
{ "code": "class Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> adj(n);\n for (const auto& e : edges) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n\n vector<bool> visi...