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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n vector<int> st;\n int carry = 0;\n ListNode* tr = head;\n while(tr){\n st.push_back(tr->val);\n tr = tr->next;\n }\n for(int i=st.size()-1; i>=0; i--){\n st[i] = st[i] * 2 + carry;\n carry = st[i] / 10;\n st[i] = st[i] % 10; \n }\n\n tr = head;\n for(int i=0; i<st.size(); i++){\n tr->val = st[i];\n tr = tr->next;\n }\n\n if(carry){\n ListNode* ptr = new ListNode;\n ptr->val = carry;\n ptr->next = head;\n head = ptr;\n }\n return head;\n }\n};",
"memory": "49445"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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\n ListNode* reverse(ListNode* head){\n if(head->next == nullptr) return head;\n ListNode* nxt = head->next;\n ListNode* prev = head;\n prev->next = nullptr;\n while(nxt!=nullptr){\n ListNode* tt = nxt->next;\n nxt->next = prev;\n prev = nxt;\n nxt = tt;\n }\n return prev;\n }\n ListNode* doubleIt(ListNode* head) {\n ListNode* temp = head;\n vector<int> s;\n while(temp!=nullptr){\n s.push_back(temp->val);\n temp = temp->next;\n }\n temp = head;\n int n = s.size();\n int c = 0;\n for(int i = n-1 ; i>=0 ; i--){\n s[i]*=2;\n if(c==1){\n s[i]++;\n c=0;\n }\n if(s[i]>=10){\n s[i]%=10;\n c=1;\n }\n }\n for(int i = 0 ; i<n; i++){\n temp->val = s[i];\n temp = temp->next;\n }\n ListNode* t = new ListNode(1);\n if(c==1){\n t->next = head;\n }\n \n return c==1?t:head;\n \n }\n};",
"memory": "49445"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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\n ListNode* reverse(ListNode* head){\n if(head->next == nullptr) return head;\n ListNode* nxt = head->next;\n ListNode* prev = head;\n prev->next = nullptr;\n while(nxt!=nullptr){\n ListNode* tt = nxt->next;\n nxt->next = prev;\n prev = nxt;\n nxt = tt;\n }\n return prev;\n }\n ListNode* doubleIt(ListNode* head) {\n ListNode* temp = head;\n vector<int> s;\n unsigned long long x = 0;\n while(temp!=nullptr){\n s.push_back(temp->val);\n temp = temp->next;\n }\n temp = head;\n ListNode* prev = head;\n int n = s.size();\n int c = 0;\n for(int i = n-1 ; i>=0 ; i--){\n s[i]*=2;\n if(c==1){\n s[i]++;\n c=0;\n }\n if(s[i]>=10){\n s[i]%=10;\n c=1;\n }\n temp->val = s[i];\n prev = temp;\n temp = temp->next;\n }\n if(c==1){\n ListNode* t = new ListNode(1);\n prev->next = t;\n }\n \n return reverse(head);\n \n }\n};",
"memory": "51115"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n stack<ListNode *>st;\n ListNode *temp=head;\n while(temp!=NULL)\n {\n st.push(temp);\n temp=temp->next;\n }\n int carry=0;\n while(!st.empty())\n {\n st.top()->val=st.top()->val*2+carry;\n if(st.top()->val >9)\n {\n (st.top()->val)%=10;\n carry=1;\n }\n else\n {\n carry=0;\n }\n st.pop();\n }\n if(carry==1)\n {\n ListNode *temp= new ListNode(1,head);\n head=temp;\n }\n return head;\n }\n};",
"memory": "52785"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n if(head ==NULL) return head;\n if(head->val == 0) return head;\n stack<int>s;\n int carry = 0;\n ListNode* curr = head;\n while(curr!=NULL){\n s.push(curr->val);\n curr = curr->next;\n } \n stack<int>s2; \n while(!s.empty()){\n int dig = s.top();\n int doubleDig = dig*2 + carry;\n s.pop();\n carry = (doubleDig/10)?doubleDig/10:0;\n int temp = (doubleDig/10)?(doubleDig%10):doubleDig;\n s2.push(temp);\n // std::cout << temp<<endl;\n }\n if(carry) s2.push(carry);\n curr = head;\n ListNode* currprev = head;\n while(curr != NULL){\n currprev = curr;\n curr->val = s2.top();\n s2.pop();\n curr = curr->next;\n }\n if(!s2.empty()){\n ListNode* newTemp = new ListNode(s2.top());\n s2.pop();\n currprev->next = newTemp;\n }\n return head;\n\n \n }\n};",
"memory": "59465"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n\n stack<int> stk;\n ListNode* curr=head;\n while(curr!=NULL)\n {\n stk.push(curr->val);\n curr=curr->next;\n }\n int carry=0;\n stack<int> res;\n while(!stk.empty())\n {\n int sum=carry;\n int top=stk.top();\n sum+=(top*2);\n stk.pop();\n res.push(sum%10);\n carry=sum/10;\n\n }\n \n curr=head;\n while(curr!=NULL)\n {\n curr->val=res.top();\n curr=curr->next;\n res.pop();\n }\n if(carry!=0)\n {\n ListNode* Node=new ListNode(1);\n Node->next=head;\n return Node;\n }\n return head;\n }\n};",
"memory": "61135"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 int num;\n while(st.size()>1){\n st.top()->val=carry+(st.top()->val*2);\n carry=(st.top()->val)/10;\n st.top()->val=(st.top()->val)%10;\n st.pop();\n }\n if((st.top()->val*2)+carry <= 9){\n st.top()->val=(st.top()->val*2)+carry;\n }\n else{\n st.top()->val=carry+(st.top()->val*2);\n carry=(st.top()->val)/10;\n st.top()->val=(st.top()->val)%10;\n st.pop();\n c->val=carry;\n head=c;\n c->next=temp;\n }\n return head;\n }\n};",
"memory": "62805"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ListNode* p=head;\n stack<int> s1;\n stack<int> s2;\n while(p){\n s1.push(p->val);\n p=p->next;\n }\n int c=0;\n while(!s1.empty()){\n s2.push((((s1.top())*2)%10)+c);\n c=((s1.top())*2)/10;\n s1.pop();\n }\n if(c){\n s2.push(c);\n }\n p=head;\n while(p->next!=NULL){\n p->val=s2.top();\n s2.pop();\n p=p->next;\n }\n p->val=s2.top();\n s2.pop();\n if(!s2.empty()){\n ListNode* t=new ListNode;\n t->val=s2.top();\n p->next=t;\n p=p->next;\n }\n return head;\n }\n};",
"memory": "64475"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ListNode* p=head;\n stack<int> s1;\n stack<int> s2;\n while(p){\n s1.push(p->val);\n p=p->next;\n }\n int c=0;\n while(!s1.empty()){\n s2.push((((s1.top())*2)%10)+c);\n c=((s1.top())*2)/10;\n s1.pop();\n }\n if(c){\n s2.push(c);\n }\n p=head;\n while(p->next!=NULL){\n p->val=s2.top();\n s2.pop();\n p=p->next;\n }\n p->val=s2.top();\n s2.pop();\n if(!s2.empty()){\n ListNode* t=new ListNode;\n t->val=s2.top();\n p->next=t;\n p=p->next;\n }\n return head;\n }\n};",
"memory": "66145"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ListNode* p=head;\n stack<int> s1;\n stack<int> s2;\n while(p){\n s1.push(p->val);\n p=p->next;\n }\n int c=0;\n while(!s1.empty()){\n s2.push((((s1.top())*2)%10)+c);\n c=((s1.top())*2)/10;\n s1.pop();\n }\n if(c){\n s2.push(c);\n }\n p=head;\n while(p->next!=NULL){\n p->val=s2.top();\n s2.pop();\n p=p->next;\n }\n p->val=s2.top();\n s2.pop();\n if(!s2.empty()){\n ListNode* t=new ListNode;\n t->val=s2.top();\n p->next=t;\n \n }\n return head;\n }\n};",
"memory": "67815"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ListNode* temp = head;\n vector<int> vec;\n while(temp!=NULL){\n vec.push_back(temp->val);\n temp=temp->next;\n }\n int carr=0;\n int val=0;\n stack<int> qu;\n while(!vec.empty()){\n val = vec.back();\n vec.pop_back();\n val = (val*2)+carr;\n qu.push(val%10);\n carr = val/10;\n }\n if(carr!=0) qu.push(carr);\n temp = head;\n ListNode* tempprev = head;\n while(temp->next!=NULL){\n tempprev = temp;\n temp->val = qu.top();\n qu.pop();\n temp=temp->next;\n }\n if(!qu.empty()){\n temp->val=qu.top();\n qu.pop();\n if(!qu.empty()){\n ListNode* curr = new ListNode(qu.top());\n temp->next=curr;\n }\n }\n return head;\n\n\n }\n};",
"memory": "69485"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n vector<ListNode*> v;\n\n ListNode* curr = head;\n while (curr){\n v.push_back(curr);\n curr = curr -> next;\n }\n\n int curry = 0;\n for (int i = v.size() - 1; i >= 0; i--){\n int x = v[i] -> val;\n x <<= 1;\n x += curry;\n curry = (x / 10);\n v[i] -> val = (x % 10);\n }\n\n if (curry == 1){\n ListNode* newHead = new ListNode(1, head);\n head = newHead;\n }\n\n return head;\n }\n};",
"memory": "71155"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n vector<ListNode*> stack{};\n ListNode* temp = head;\n while (temp != nullptr) {\n stack.push_back(temp);\n temp = temp->next;\n }\n int i = stack.size() - 1;\n int carry = 0;\n while (stack.size() > 0) {\n int result = stack[i]->val * 2;\n stack[i]->val = result % 10 + carry;\n carry = result / 10;\n stack.pop_back();\n i--;\n }\n if (carry != 0) {\n ListNode* new_node = new ListNode(carry, head);\n return new_node;\n }\n return head;\n }\n};",
"memory": "72825"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n vector<ListNode *> s;\n for (ListNode *cursor = head; cursor != nullptr; cursor = cursor->next) {\n s.push_back(cursor);\n }\n int carry = 0;\n while (!s.empty()) {\n ListNode *cursor = s.back();\n s.pop_back();\n int value = cursor->val * 2 + carry;\n cursor->val = value % 10;\n carry = value / 10;\n }\n return carry > 0 ? new ListNode(carry, head) : head;\n }\n};",
"memory": "74495"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n vector<ListNode*> x;\n ListNode *p;\n p=head;\n while(p){\n x.push_back(p);\n p=p->next;\n }\n int c=0;\n for (int i=x.size()-1;i>=0;i--){\n p=x[i];\n int f=2*p->val+c;\n if (f>=10){\n f-=10;\n c=1;\n }else c=0;\n x[i]->val=f;\n }\n if (c){\n ListNode* q=new ListNode();\n q->val=1;\n q->next=head;\n head=q;\n }\n return head;\n }\n};",
"memory": "76165"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n vector<ListNode*> x;\n ListNode *p;\n p=head;\n while(p){\n x.push_back(p);\n p=p->next;\n }\n int c=0;\n for (int i=x.size()-1;i>=0;i--){\n p=x[i];\n int f=2*p->val+c;\n if (f>=10){\n f-=10;\n c=1;\n }else c=0;\n x[i]->val=f;\n }\n if (c){\n ListNode* q=new ListNode();\n q->val=1;\n q->next=head;\n head=q;\n }\n return head;\n }\n};",
"memory": "76165"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 int carry=0;\n ListNode* doubleIt(ListNode* head) {\n if (head==nullptr) return head;\n else{\n doubleIt(head->next);\n int doubled=(head->val)<<1;\n head->val=doubled%10+carry;\n carry=(doubled-doubled%10)/10;\n }\n if (carry){\n ListNode* newnode=new ListNode(carry, head);\n head=newnode;\n }\n return head;\n }\n};",
"memory": "77835"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 int carry=0;\n ListNode* doubleIt(ListNode* head) {\n if (head==nullptr) return head;\n else{\n doubleIt(head->next);\n int doubled=(head->val)<<1;\n head->val=doubled%10+carry;\n carry=(doubled-doubled%10)/10;\n }\n if (carry){\n ListNode* newnode=new ListNode(carry, head);\n head=newnode;\n }\n return head;\n }\n};",
"memory": "79505"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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;\n bool out = true;\n for(int i=0;i<ans.size();i++){\n int s = (ans[i] * 2);\n bool flag = true;\n\n if(s + y <= 9){\n actualans.push_back(s + y);\n y = 0;\n }\n else{\n actualans.push_back((s+y)%10);\n y = (s+y)/10;\n flag = false;\n }\n if(flag == true) out = true;\n else out = false;\n }\n if(out == false && y > 0) actualans.push_back(y);\n reverse(actualans.begin(),actualans.end());\n t = head;\n int aqua = -1;\n for(int i=0;i<actualans.size() && t;i++){\n t->val = actualans[i];\n if(t->next != NULL) t = t->next;\n else{\n aqua = i;\n break;\n }\n }\n \n for(int i=aqua+1;i<actualans.size();i++){\n ListNode* naya = new ListNode(actualans[i]);\n t->next = naya;\n t = t->next;\n }\n return head;\n\n\n \n }\n};",
"memory": "81175"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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;\n bool out = true;\n for(int i=0;i<ans.size();i++){\n int s = (ans[i] * 2);\n bool flag = true;\n\n if(s + y <= 9){\n actualans.push_back(s + y);\n y = 0;\n }\n else{\n actualans.push_back((s+y)%10);\n y = (s+y)/10;\n flag = false;\n }\n if(flag == true) out = true;\n else out = false;\n }\n if(out == false && y > 0) actualans.push_back(y);\n reverse(actualans.begin(),actualans.end());\n t = head;\n int aqua = -1;\n for(int i=0;i<actualans.size() && t;i++){\n t->val = actualans[i];\n if(t->next != NULL) t = t->next;\n else{\n aqua = i;\n break;\n }\n }\n \n for(int i=aqua+1;i<actualans.size();i++){\n ListNode* naya = new ListNode(actualans[i]);\n t->next = naya;\n t = t->next;\n }\n return head;\n\n\n \n }\n};",
"memory": "82845"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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;\n bool out = true;\n for(int i=0;i<ans.size();i++){\n int s = (ans[i] * 2);\n bool flag = true;\n\n if(s + y <= 9){\n actualans.push_back(s + y);\n y = 0;\n }\n else{\n actualans.push_back((s+y)%10);\n y = (s+y)/10;\n flag = false;\n }\n if(flag == true) out = true;\n else out = false;\n }\n if(out == false && y > 0) actualans.push_back(y);\n reverse(actualans.begin(),actualans.end());\n t = head;\n int aqua = -1;\n for(int i=0;i<actualans.size() && t;i++){\n t->val = actualans[i];\n if(t->next != NULL) t = t->next;\n else{\n aqua = i;\n break;\n }\n }\n \n for(int i=aqua+1;i<actualans.size();i++){\n ListNode* naya = new ListNode(actualans[i]);\n t->next = naya;\n t = t->next;\n }\n return head;\n\n\n \n }\n};",
"memory": "82845"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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;\n bool out = true;\n for(int i=0;i<ans.size();i++){\n int s = (ans[i] * 2);\n bool flag = true;\n\n if(s + y <= 9){\n actualans.push_back(s + y);\n y = 0;\n }\n else{\n actualans.push_back((s+y)%10);\n y = (s+y)/10;\n flag = false;\n }\n if(flag == true) out = true;\n else out = false;\n }\n if(out == false && y > 0) actualans.push_back(y);\n reverse(actualans.begin(),actualans.end());\n t = head;\n int aqua = -1;\n for(int i=0;i<actualans.size() && t;i++){\n t->val = actualans[i];\n if(t->next != NULL) t = t->next;\n else{\n aqua = i;\n break;\n }\n }\n \n for(int i=aqua+1;i<actualans.size();i++){\n ListNode* naya = new ListNode(actualans[i]);\n t->next = naya;\n t = t->next;\n }\n return head;\n\n\n \n }\n};",
"memory": "84515"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n\n ListNode *ans = new ListNode, *temp = ans;\n int result;\n\n ans -> next = head;\n\n while(head != NULL){\n ans -> next = new ListNode;\n\n result = (head -> val)*2;\n if(result > 9){\n ans -> val += result/10;\n (ans -> next) -> val = result%10;\n }\n else (ans -> next) -> val = result;\n\n head = head -> next;\n ans = ans -> next;\n }\n\n if(temp -> val) return temp;\n else return temp -> next;\n }\n};",
"memory": "96205"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* reverseLL(ListNode* head) {\n if (head == NULL || head->next == NULL) {\n return head;\n }\n ListNode* newhead = reverseLL(head->next);\n ListNode* front = head->next;\n front->next = head;\n head->next = NULL;\n return newhead;\n }\n ListNode* doubleIt(ListNode* head) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n ListNode *l1 = head, *l2 = head;\n ListNode* newhead1 = reverseLL(l1);\n ListNode* dummy = new ListNode();\n ListNode* temp = dummy;\n int carry = 0;\n while (newhead1 != nullptr || carry) {\n int sum = 0;\n if (newhead1 != nullptr) {\n sum += 2 * (newhead1->val);\n newhead1 = newhead1->next;\n }\n sum = sum + carry;\n carry = sum / 10;\n ListNode* newnode = new ListNode(sum % 10);\n temp->next = newnode;\n temp = temp->next;\n }\n return reverseLL(dummy->next);\n }\n};",
"memory": "97875"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* Reverse(ListNode* head){\n if(head==NULL || head->next==NULL){\n return head;\n }\n ListNode*last=Reverse(head->next);\n head->next->next=head;\n head->next=NULL;\n return last;\n }\n ListNode* doubleIt(ListNode* head) {\n if(!head){\n return head;\n }\n\n ListNode*dummy=new ListNode(0);\n head=Reverse(head);\n ListNode* curr=head;\n ListNode* prev=dummy;\n int carry=0;\n\n while(curr!=NULL){\n int sum=curr->val*2+carry;\n carry=sum/10;\n prev->next=new ListNode(sum%10);\n prev=prev->next;\n curr=curr->next;\n }\n\n if(carry){\n prev->next=new ListNode(carry);\n }\n return Reverse(dummy->next);\n }\n};",
"memory": "99545"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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<int, ListNode*> addLists(ListNode* l1, ListNode* l2) {\n if (l1 == nullptr && l2 == nullptr) {\n return {0, nullptr};\n }\n \n auto nextResult = addLists(l1->next, l2->next);\n int sum = l1->val + l2->val + nextResult.first;\n ListNode* currentNode = new ListNode(sum % 10);\n currentNode->next = nextResult.second;\n \n return {sum / 10, currentNode};\n }\npublic:\n ListNode* doubleIt(ListNode* head) {\n ListNode* l1=head,*l2=head;\n auto result = addLists(l1, l2);\n ListNode* resultList = result.second;\n\n if (result.first > 0) {\n ListNode* carryNode = new ListNode(result.first);\n carryNode->next = resultList;\n resultList = carryNode;\n }\n return resultList;\n }\n};",
"memory": "101215"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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\n void apply(ListNode *head){\n if(!head) return;\n\n apply(head->next);\n\n mul += head->val*2 %10;\n carry = head->val*2 /10;\n\n ans->val = mul;\n ListNode *newNode = new ListNode(carry);\n newNode->next = ans;\n ans = newNode;\n\n mul = carry;\n }\n\n\n ListNode *ans;\n int mul =0, carry=0;\n ListNode* doubleIt(ListNode* head) {\n\n ans = new ListNode(-1);\n apply(head);\n return carry == 0 ? ans->next: ans;\n }\n};",
"memory": "102885"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 solve(ListNode* node) {\n\t\tif (!node) return 0;\n\t\tnode->val = node->val * 2 + solve(node->next);\n\t\tint carry = node->val / 10;\n\t\tnode->val %= 10;\n\t\treturn carry;\n\t}\n\npublic:\n\tListNode* doubleIt(ListNode* head) {\n\t\tListNode* newHead = copy(head);\n\t\tint carry = solve(newHead);\n\t\tif (carry > 0)\n\t\t\tnewHead = new ListNode(carry, newHead);\n\t\treturn newHead;\n\t}\n};",
"memory": "104555"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* reverse(ListNode* head) {\n ListNode* prevNode = NULL;\n ListNode* currNode = head;\n\n while (currNode) {\n ListNode* nextNode = currNode -> next;\n currNode -> next = prevNode;\n prevNode = currNode;\n currNode = nextNode;\n }\n\n return prevNode;\n }\n\n ListNode* add(ListNode* &num1, ListNode* &num2, int &carry) {\n // Base Case\n if (!num1 && !num2) {\n if (carry) {\n ListNode* newDigit = new ListNode(carry);\n return newDigit;\n }\n else return NULL;\n }\n\n // Ek Case\n int digit1 = 0;\n int digit2 = 0;\n // Update digit1 if num1 node exists\n if (num1) {\n digit1 = num1 -> val;\n num1 = num1 -> next;\n }\n // Update digit2 if num2 node exists\n if (num2) {\n digit2 = num2 -> val;\n num2 = num2 -> next;\n }\n\n cout << digit1 << \", \" << digit2 << endl;\n int sum = digit1 + digit2 + carry;\n int ansDigit = sum % 10;\n carry = sum / 10;\n\n ListNode* ansNode = new ListNode(ansDigit);\n ansNode -> next = add(num1, num2, carry);\n\n return ansNode;\n }\n\n ListNode* doubleIt(ListNode* head) {\n head = reverse(head);\n ListNode* num1 = head;\n ListNode* num2 = num1;\n int carry = 0;\n return reverse(add(num1, num2, carry));\n }\n};",
"memory": "106225"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* reverse(ListNode* &back, ListNode* &curr){\n if(!curr)\n return back;\n\n ListNode* forw=curr->next;\n curr->next=back;\n return reverse(curr,forw);\n }\n\n ListNode* doubleIt(ListNode* head) {\n ListNode* back=NULL;\n ListNode* curr=head;\n ListNode* newHead=reverse(back,curr);\n\n ListNode* dummy=new ListNode(-1);\n ListNode* temp=dummy;\n int carry=0;\n while(newHead){\n int value=((newHead->val*2)+carry);\n int digit=value%10;\n carry=value/10;\n\n ListNode* newNode=new ListNode(digit);\n temp->next=newNode;\n temp=temp->next;\n newHead=newHead->next;\n }\n\n if(carry){\n ListNode* newNode=new ListNode(carry);\n temp->next=newNode;\n temp=temp->next;\n }\n\n back=NULL;\n curr=dummy->next;\n return reverse(back,curr);\n }\n};",
"memory": "107895"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ListNode* temp = head;\n string s=\"\";\n while(temp!=nullptr){\n s.push_back(temp->val + '0');\n temp= temp->next;\n }\n\n // cout<<s<<endl;\n reverse(s.begin(), s.end());\n int carry =0;\n for(int i=0; i<s.size(); i++){\n int a = s[i]-'0';\n carry += 2*a;\n s[i]=carry%10+'0';\n carry = carry/10;\n }\n if(carry!=0){\n s.push_back(carry+'0');\n }\n reverse(s.begin(), s.end());\n // cout<<s<<endl;\n int n = s.size();\n ListNode* res = head;\n head->val = s[0] - '0';\n for(int i=1; i<n; i++){\n ListNode* temp = new ListNode(s[i]-'0'); \n head->next = temp;\n head = head->next; \n }\n return res;\n\n }\n};",
"memory": "109565"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ListNode* temp=head;\n ListNode* ans=new ListNode(0);\n ListNode* t=ans;\n string s;\n while(temp){\n s+=temp->val;\n temp=temp->next; \n }\n reverse(s.begin(), s.end());\n int carry=0;\n for(int i=0;i<s.size();i++){\n int x=2*s[i]+carry;\n s[i]=x%10;\n carry=x/10;\n }\n if(carry!=0){\n s+=carry;\n }\n reverse(s.begin(), s.end());\n for(int i=0;i<s.size();i++){\n t->next=new ListNode(s[i]);\n t=t->next;\n }\n return ans->next;\n }\n};",
"memory": "111235"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n string s=\"\";\n ListNode* start=head;\n while(start!=NULL){\n s+=to_string(start->val);\n start=start->next;\n }\n // cout<<s;\n int bal=0;\n for(int i=s.size()-1;i>=0;i--){\n int e=int(s[i])-int('0');\n e=e*2+bal;\n if(e>=10){\n bal=1;\n e=e-10;\n }\n else{\n bal=0;\n }\n s[i]=char(e+int('0'));\n }\n if(bal==1){\n s='1'+s;\n }\n // cout<<\" \"<<s;\n head->val=int(s[0])-int('0');\n ListNode* er=head;\n for(int i=1;i<s.size();i++){\n ListNode* temp=new ListNode(int(s[i])-int('0'));\n er->next=temp;\n er=er->next;\n }\n return head;\n }\n};",
"memory": "112905"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ListNode* current =head;\n string num_str;\n while(current){\n num_str+=to_string(current->val);\n current=current->next;\n }\n cout<<num_str<<endl;\n int carry=0;\n for(int i=num_str.length()-1;i>=0;i--){\n int digit=(num_str[i]-'0')*2+carry;\n carry=digit/10;\n num_str[i]=(digit%10)+'0';\n }\n cout<<num_str<<endl;\n if(carry==1){\n num_str=to_string(carry)+num_str;\n }\n cout<<num_str;\n ListNode* dummy=new ListNode(0);\n ListNode* prev=dummy;\n for(char c: num_str){\n prev->next=new ListNode(c-'0');\n prev=prev->next;\n }\n return dummy->next;\n }\n};",
"memory": "114575"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ListNode* current =head;\n string num_str;\n while(current){\n num_str+=to_string(current->val);\n current=current->next;\n }\n cout<<num_str<<endl;\n int carry=0;\n for(int i=num_str.length()-1;i>=0;i--){\n int digit=(num_str[i]-'0')*2+carry;\n carry=digit/10;\n num_str[i]=(digit%10)+'0';\n }\n cout<<num_str<<endl;\n if(carry==1){\n num_str=to_string(carry)+num_str;\n }\n cout<<num_str;\n ListNode* dummy=new ListNode(0);\n ListNode* prev=dummy;\n for(char c: num_str){\n prev->next=new ListNode(c-'0');\n prev=prev->next;\n }\n return dummy->next;\n }\n};",
"memory": "114575"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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()-1;i>=0;i--){\n int k=s[i]-'0';\n k=k*2+sum;\n s[i]=(k%10)+'0';\n sum=k/10;\n }\n if(sum>0){\n s=to_string(sum)+s;\n }\n ListNode* dummy=new ListNode(0);\n ListNode* current=dummy;\n for(int i=0;i<s.size();i++){\n current->next=new ListNode(s[i]-'0');\n current=current->next;\n }\n return dummy->next;\n }\n};",
"memory": "116245"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n string st;\n ListNode* temp = head;\n while(temp){\n st+=(temp->val+'0');\n temp=temp->next;\n }\n int carry = 0;\n for(int i=st.size()-1;i>=0;i--){\n int digit = st[i]-'0';\n int sum = digit+digit+carry;\n carry=sum/10;\n digit=sum%10;\n st[i]=digit+'0';\n }\n ListNode* ans = new ListNode();\n temp=head;\n ListNode* t = ans;\n int i=0;\n if(carry){\n st= '1'+st;\n t->next = new ListNode(1);\n i++;\n t=t->next;\n }\n for(;i<st.size();i++){\n t->next = new ListNode(st[i]-'0');\n t=t->next;\n }\n return ans->next;\n }\n};",
"memory": "117915"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n string st;\n ListNode* temp = head;\n while(temp){\n st+=(temp->val+'0');\n temp=temp->next;\n }\n int carry = 0;\n for(int i=st.size()-1;i>=0;i--){\n int digit = st[i]-'0';\n int sum = digit+digit+carry;\n carry=sum/10;\n digit=sum%10;\n st[i]=digit+'0';\n }\n ListNode* ans = new ListNode();\n temp=head;\n ListNode* t = ans;\n int i=0;\n if(carry){\n st= '1'+st;\n t->next = new ListNode(1);\n i++;\n t=t->next;\n }\n for(;i<st.size();i++){\n t->next = new ListNode(st[i]-'0');\n t=t->next;\n }\n return ans->next;\n }\n};",
"memory": "117915"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 void makeDouble(string&s){\n string temp=\"\";\n int carry=0;\n for(int i=s.size()-1;i>=0;i--){\n int digit=s[i]-'0';\n digit=digit+digit+carry;\n // cout<<digit<<\" \";\n carry=digit/10;\n temp+=(digit%10)+'0';\n // cout<<temp<<endl;\n\n }\n if(carry){\n temp+=carry+'0';\n }\n reverse(temp.begin(),temp.end());\n s=temp;\n }\n ListNode* doubleIt(ListNode* head) {\n string s=\"\";\n ListNode*temp=head;\n\n while(temp!=NULL){\n s+=temp->val+'0';\n temp=temp->next;\n // num/=10;\n }\n // cout<<s<<endl;\n makeDouble(s);\n // s=to_string(stol(s)*2);\n // cout<<num;\n // num=num*2;\n // cout<<\" \"<<num;\n // string s=to_string(num);\n\n // reverse(s.begin(),s.end());\n // cout<<s;\n ListNode*newLL=new ListNode(s[0]-'0');\n head=newLL;\n for(int i=1;i<s.size();i++){\n newLL->next=new ListNode(s[i]-'0');\n newLL=newLL->next;\n }\n\n return head;\n \n }\n};",
"memory": "119585"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n if (!head)\n return nullptr;\n\n // Step 1: Convert the linked list to a string representation of the\n // number\n string num = \"\";\n while (head) {\n num += to_string(head->val);\n head = head->next;\n }\n\n // Step 2: Double the number represented by the string\n string result = \"\";\n int carry = 0;\n for (int i = num.length() - 1; i >= 0; i--) {\n int digit = (num[i] - '0') * 2 + carry;\n carry = digit / 10;\n result += to_string(digit % 10);\n }\n if (carry > 0) {\n result += to_string(carry);\n }\n\n // Step 3: Reverse the result string as we've built it backwards\n reverse(result.begin(), result.end());\n\n // Step 4: Convert the result string back to a linked list\n ListNode* newHead = new ListNode(result[0] - '0');\n ListNode* current = newHead;\n for (int i = 1; i < result.length(); ++i) {\n current->next = new ListNode(result[i] - '0');\n current = current->next;\n }\n\n return newHead;\n }\n};",
"memory": "121255"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n ListNode* temp = head;\n string s = \"\";\n\n while(temp != NULL){\n s += to_string(temp->val);\n temp = temp->next;\n }\n \n int carry = 0;\n string result = \"\";\n int i = s.size() - 1;\n\n while(i>=0 || carry){\n int val = (i>=0) ? s[i--]-'0':0;\n int mul = val*2 + carry;\n\n carry = mul/10;\n result += to_string(mul%10);\n }\n\n reverse(result.begin(),result.end());\n\n ListNode* newHead = new ListNode(result[0] - '0');\n ListNode* curr = newHead;\n\n for(int k=1; k<result.size(); k++){\n curr->next = new ListNode(result[k]-'0');\n curr = curr->next;\n }\n return newHead;\n }\n};",
"memory": "122925"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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// class Solution {\n// public:\n// ListNode* doubleIt(ListNode* head) {\n// vector<int>v1;\n// ListNode*curr = head;\n// string str1 = \" \";\n// while(curr != nullptr)\n// {\n// str1+= to_string(curr->val);\n// curr = curr->next;\n// }\n// long long numb = stoll(str1);\n\n// // v1.pop();\n// numb = numb * 2;\n// string str = to_string(numb);\n// for (int i = 0; i < str.length(); i++) {\n// v1.push_back(str[i] - '0');\n// }\n// // v1.push_back(numb);\n// ListNode* l1 = new ListNode(0);\n// ListNode*newhead = l1;\n// for(int i = 0 ; i < v1.size() ; i++)\n// {\n// newhead->next = new ListNode(v1[i]);\n// newhead = newhead->next;\n// }\n// return l1->next;\n\n// }\n// };\n\nclass Solution {\npublic:\n ListNode* doubleIt(ListNode* head) {\n if (!head) return nullptr;\n\n // Convert linked list to a string representing the number\n string str1;\n ListNode* curr = head;\n while (curr != nullptr) {\n str1 += to_string(curr->val);\n curr = curr->next;\n }\n\n // Double the number by treating it as a string to handle large numbers\n string doubled = multiplyByTwo(str1);\n\n // Create a new linked list from the doubled string\n ListNode* dummy = new ListNode(0);\n ListNode* newHead = dummy;\n for (char c : doubled) {\n newHead->next = new ListNode(c - '0');\n newHead = newHead->next;\n }\n\n return dummy->next;\n }\n\nprivate:\n string multiplyByTwo(const string& num) {\n string result;\n int carry = 0;\n for (int i = num.size() - 1; i >= 0; --i) {\n int digit = (num[i] - '0') * 2 + carry;\n carry = digit / 10;\n result.push_back((digit % 10) + '0');\n }\n if (carry) {\n result.push_back(carry + '0');\n }\n reverse(result.begin(), result.end());\n return result;\n }\n};",
"memory": "124595"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n stack<int> st;\n ListNode *temp = head;\n while(temp != nullptr){\n st.push(temp->val);\n temp = temp->next;\n }\n ListNode *dummy = nullptr;\n temp = dummy;\n int carry = 0;\n while(!st.empty()){\n int num = 2 * st.top() + carry;\n carry = num / 10;\n ListNode *newNode = new ListNode(num%10);\n newNode->next = dummy;\n dummy = newNode;\n st.pop();\n }\n if(carry != 0){\n ListNode *newNode = new ListNode(carry);\n newNode->next = dummy;\n dummy = newNode;\n }\n return dummy;\n }\n};",
"memory": "126265"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n stack<int> st;\n int val=0;\n\n while (head!=NULL) {\n st.push(head->val);\n head=head->next;\n }\n\n ListNode* newHead=NULL;\n\n while (!st.empty()) {\n int x=st.top()*2+val;\n ListNode* node=new ListNode(x%10);\n node->next=newHead;\n newHead=node;\n val=x/10;\n st.pop();\n }\n\n if (val>0) {\n ListNode* node=new ListNode(val);\n node->next=newHead;\n newHead=node;\n }\n\n return newHead;\n }\n};\n",
"memory": "127935"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 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 ListNode* doubleIt(ListNode* head) {\n // Step 1: Use a stack to reverse the linked list and store values\n stack<int> st;\n ListNode* temp = head;\n while (temp) {\n st.push(temp->val);\n temp = temp->next;\n }\n\n // Step 2: Initialize carry and process the most significant digit\n int carry = 0;\n int t = (st.top() * 2 + carry) % 10;\n carry = (st.top() * 2 + carry) / 10;\n st.pop();\n\n // Step 3: Create the new linked list starting with the processed digit\n ListNode* ptr = new ListNode(t);\n\n while (!st.empty()) {\n int newVal = st.top() * 2 + carry; // Double the value and add carry\n t = newVal % 10; // Extract last digit\n carry = newVal / 10; // Update carry\n st.pop();\n\n ListNode* newnode = new ListNode(t);\n newnode->next = ptr;\n ptr = newnode;\n }\n\n // Step 4: If there's any remaining carry, add it as a new node\n if (carry > 0) {\n ListNode* newnode = new ListNode(carry);\n newnode->next = ptr;\n ptr = newnode;\n }\n\n return ptr;\n }\n};\n",
"memory": "129605"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 ListNode* doubleIt(ListNode* head) {\n \n vector<int> numbers;\n \n auto p=head;\n while(p) numbers.push_back(p->val),p=p->next;\n\n ListNode* p2=nullptr;\n int n=int(numbers.size());\n int carry=0;\n for(int i=n-1,s2,d;i>=0;--i) {\n s2 = numbers[i]*2+carry;\n carry = s2/10;\n d=s2%10;\n concat(p2,d);\n }\n\n if(carry>0) concat(p2,carry);\n\n return p2;\n }\n\n void concat(ListNode* & p2, int d)\n {\n if(p2==nullptr)\n p2= new ListNode(d);\n else {\n auto pNew = new ListNode(d);\n pNew->next = p2;\n p2=pNew;\n }\n }\n\n};",
"memory": "141295"
} |
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> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
<li><font face="monospace"><code>0 <= Node.val <= 9</code></font></li>
<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>
| 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 temp = temp->next;\n }\n return head;\n }\npublic:\n ListNode* doubleIt(ListNode* head) {\n vector<int>res;\n ListNode* temp = head;\n while(temp!= NULL){\n res.push_back(temp->val);\n temp = temp->next;\n }\n int carry = 0;\n for(int i = res.size()-1; i>=0 ; i--){\n int doublet = 2* res[i] + carry;\n res[i]=doublet % 10;\n carry = doublet/10;\n }\n if(carry > 0) {\n res.insert(res.begin(), carry);\n }\n return solve(res);\n }\n};",
"memory": "142965"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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 cur = nums[i];\n if (cur > prev) {\n int slots = (cur + prev - 1) / prev; // ceil(cur / prev); // we need at least this slot\n ops += slots - 1;\n cur = cur / slots;\n }\n prev = cur;\n }\n return ops;\n }\n};",
"memory": "56500"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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; --i) {\n int cur = nums[i];\n if (cur > prev) {\n int slots = (cur + prev - 1) / prev; // ceil(cur / prev); // we need at least this slot\n ops += slots - 1;\n cur = cur / slots;\n }\n prev = cur;\n }\n return ops;\n }\n};",
"memory": "56500"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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--) {\n if (nums[i] > last) {\n int numDivisions =\n (nums[i] / last) + (nums[i] % last == 0 ? 0 : 1);\n count += numDivisions - 1;\n last = nums[i] / numDivisions;\n } else\n last = nums[i];\n }\n\n return count;\n }\n};",
"memory": "56600"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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--) {\n if (nums[i] > last) {\n int numDivisions =\n (nums[i] / last) + (nums[i] % last == 0 ? 0 : 1);\n count += numDivisions - 1;\n last = nums[i] / numDivisions;\n } else\n last = nums[i];\n }\n\n return count;\n }\n};",
"memory": "56600"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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+1 number and each part is as big as possible\n long long minimumReplacement(vector<int>& nums) {\n std::ios::sync_with_stdio(0);\n int n = nums.size();\n long long operations = 0;\n for(int i=n-1; i>=1; i--) {\n if(nums[i-1] > nums[i]) {\n //calculate no of parts\n long long parts = nums[i-1]/nums[i];\n //increment parts if not divisible\n if(nums[i-1]%nums[i] != 0) parts++;\n operations += (parts - 1);\n //calculate the smallest biggest number of the part\n nums[i-1] = nums[i-1]/parts;\n }\n }\n return operations;\n\n \n }\n};",
"memory": "56700"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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) {\n ans += (long long) nums[i] / cur - 1;\n } else if (nums[i] > cur && nums[i] % cur != 0){\n ans += (long long) nums[i]/ cur;\n cur = (long long) nums[i]/ ((long long) nums[i]/cur + 1);\n } else if (nums[i] <= cur){\n cur = nums[i];\n }\n }\n return ans;\n }\n};",
"memory": "56800"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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 last=nums[i]/k;\n ans+=k-1;\n }\n else{\n last=nums[i];\n }\n }\n return ans;\n\n \n }\n};",
"memory": "57000"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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 times++;\n minhighest=nums[i]/times;\n }\n ans+=times-1;\n }\n return ans;\n }\n};",
"memory": "57100"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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(rem!=0) step++;\n ans += (step-1);\n nums[i]=nums[i]/step;\n }\n }\n return ans;\n }\n};",
"memory": "57200"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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 if (nums[i] % last) t++; \n last = nums[i] / t; \n ans += t - 1; \n } else {\n last = nums[i]; \n }\n }\n return ans; \n }\n\n};",
"memory": "57200"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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 }\n return count;\n }\n};",
"memory": "57300"
} |
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]</code> with <code>2</code> and <code>4</code> and convert <code>nums</code> to <code>[5,2,4,7]</code>.</li>
</ul>
<p>Return <em>the minimum number of operations to make an array that is sorted in <strong>non-decreasing</strong> order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,9,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are the steps to sort the array in non-decreasing order:
- From [3,9,3], replace the 9 with 3 and 6 so the array becomes [3,3,6,3]
- From [3,3,6,3], replace the 6 with 3 and 3 so the array becomes [3,3,3,3,3]
There are 2 steps to sort the array in non-decreasing order. Therefore, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array is already in non-decreasing order. Therefore, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 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 += (nums[i] - 1) / prev;\n if (nums[i] % prev == 0) {\n prev = prev;\n } else {\n prev = (nums[i] / cl);\n }\n }\n return ans;\n }\n};",
"memory": "57300"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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\tparent[node] = findParent(parent[node]);\n\t\t}\n\t\treturn parent[node];\n\t}\n\n\tvoid join(int first, int second) {\n\t\tfirst = findParent(first);\n\t\tsecond = findParent(second);\n\t\tif (first == second) {\n\t\t\treturn;\n\t\t}\n\t\tif (first < second) {\n\t\t\tparent[second] = first;\n\t\t}\n\t\telse {\n\t\t\tparent[first] = second;\n\t\t}\n\t}\n};\n\nclass Solution {\n\n\tstatic const int START_NODE = 0;\n\tint numberOfNodes = 0;\n\t\n\npublic:\n\tint reachableNodes(int numberOfNodes, const vector<vector<int>>& edges, const vector<int>& restricted) {\n\t\tthis->numberOfNodes = numberOfNodes;\n\t\tvector<bool> quickAccessRestricted = createQuickAccessRestricted(restricted);\n\t\tUnionFind unionFind(numberOfNodes);\n\n\t\tjoinReachableNodesFromStart(unionFind, edges, quickAccessRestricted);\n\t\treturn countReachableNodesFromStart(unionFind, quickAccessRestricted);\n\t}\n\n\tvector<bool> createQuickAccessRestricted(span<const int> restricted) const {\n\t\tvector<bool> quickAccessRestricted(numberOfNodes);\n\t\tfor (const auto& value : restricted) {\n\t\t\tquickAccessRestricted[value] = true;\n\t\t}\n\t\treturn quickAccessRestricted;\n\t}\n\n\tvoid joinReachableNodesFromStart(UnionFind& unionFind, span<const vector<int>> edges, const vector<bool>& quickAccessRestricted) const {\n\t\tfor (const auto& edge : edges) {\n\t\t\tint first = edge[0];\n\t\t\tint second = edge[1];\n\t\t\tif (quickAccessRestricted[first] || quickAccessRestricted[second]) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tunionFind.join(first, second);\n\t\t}\n\t}\n\n\tint countReachableNodesFromStart(UnionFind& unionFind, const vector<bool>& quickAccessRestricted) const {\n\t\tint numberOfReachableNodesFromStart = 0;\n\t\tfor (int node = 0; node < numberOfNodes; ++node) {\n\t\t\tif (quickAccessRestricted[node]) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (unionFind.findParent(node) == START_NODE) {\n\t\t\t\t++numberOfReachableNodesFromStart;\n\t\t\t}\n\t\t}\n\t\treturn numberOfReachableNodesFromStart;\n\t}\n};\n",
"memory": "117660"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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\tparent[node] = findParent(parent[node]);\n\t\t}\n\t\treturn parent[node];\n\t}\n\n\tvoid join(int first, int second) {\n\t\tfirst = findParent(first);\n\t\tsecond = findParent(second);\n\t\tif (first == second) {\n\t\t\treturn;\n\t\t}\n\t\tif (first < second) {\n\t\t\tparent[second] = first;\n\t\t}\n\t\telse {\n\t\t\tparent[first] = second;\n\t\t}\n\t}\n};\n\nclass Solution {\n\n\tstatic const int START_NODE = 0;\n\tint numberOfNodes = 0;\n\t\n\npublic:\n\tint reachableNodes(int numberOfNodes, const vector<vector<int>>& edges, const vector<int>& restricted) {\n\t\tthis->numberOfNodes = numberOfNodes;\n\t\tvector<bool> quickAccessRestricted = createQuickAccessRestricted(restricted);\n\t\tUnionFind unionFind(numberOfNodes);\n\n\t\tjoinReachableNodesFromStart(unionFind, edges, quickAccessRestricted);\n\t\treturn countReachableNodesFromStart(unionFind, quickAccessRestricted);\n\t}\n\n\tvector<bool> createQuickAccessRestricted(span<const int> restricted) const {\n\t\tvector<bool> quickAccessRestricted(numberOfNodes);\n\t\tfor (const auto& value : restricted) {\n\t\t\tquickAccessRestricted[value] = true;\n\t\t}\n\t\treturn quickAccessRestricted;\n\t}\n\n\tvoid joinReachableNodesFromStart(UnionFind& unionFind, span<const vector<int>> edges, const vector<bool>& quickAccessRestricted) const {\n\t\tfor (const auto& edge : edges) {\n\t\t\tint first = edge[0];\n\t\t\tint second = edge[1];\n\t\t\tif (quickAccessRestricted[first] || quickAccessRestricted[second]) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tunionFind.join(first, second);\n\t\t}\n\t}\n\n\tint countReachableNodesFromStart(UnionFind& unionFind, const vector<bool>& quickAccessRestricted) const {\n\t\tint numberOfReachableNodesFromStart = 0;\n\t\tfor (int node = 0; node < numberOfNodes; ++node) {\n\t\t\tif (quickAccessRestricted[node]) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (unionFind.findParent(node) == START_NODE) {\n\t\t\t\t++numberOfReachableNodesFromStart;\n\t\t\t}\n\t\t}\n\t\treturn numberOfReachableNodesFromStart;\n\t}\n};\n",
"memory": "117660"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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) return;\n \n if(size[p1] >= size[p2]) {\n parent[p2] = p1;\n size[p1] += size[p2];\n } else { \n parent[p1] = p2;\n size[p2] += size[p1];\n }\n }\n\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n size.resize(n,1);\n parent.resize(n,0);\n for(int i = 1; i < n; i++) parent[i] = i;\n for(int& restrictedNode : restricted) size[restrictedNode] = 0;\n\n for(vector<int>& e:edges) connect(e[0], e[1]);\n\n return size[findParent(0)];\n }\n};",
"memory": "118980"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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]) swap(a,b);\n __size[a] += __size[b];\n __par[b] = a; \n }\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n __size.resize(n, 1);\n __par.resize(n, -1);\n\n vector<bool> isRestricted(n);\n for(int i=0 ; i<restricted.size() ; i++) \n isRestricted[restricted[i]] = true;\n \n for(auto &v : edges) {\n if(isRestricted[v[0]] || isRestricted[v[1]]) continue;\n merge(v[0], v[1]);\n }\n\n return __size[find(0)];\n }\n};",
"memory": "118980"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 if(s[xp]>=s[yp]){\n p[yp] = xp;\n s[xp] += s[yp];\n }else{\n p[xp] = yp;\n s[yp] += s[xp];\n }\n }\n int reachableNodes(int n,vector<vector<int>>& e,vector<int>& r){\n s.resize(n,1);\n for(int i=0;i<n;i++){\n p.push_back(i);\n }\n for(int i=0;i<r.size();i++){\n s[r[i]]=0;\n }\n for(auto &ed:e){\n un(ed[0],ed[1]);\n } \n return s[fi(0)];\n }\n};",
"memory": "120300"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 parentU = find(u);\n int parentV = find(v);\n\n if (parentU != parentV) {\n if (parent[parentU] < parent[parentV]) { // parentU has larger size (more negative)\n parent[parentU] += parent[parentV]; // Increase size of the root of parentU\n parent[parentV] = parentU; // Set parentV under parentU\n } else {\n parent[parentV] += parent[parentU]; // Increase size of the root of parentV\n parent[parentU] = parentV; // Set parentU under parentV\n }\n }\n }\n};\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n DSU obj(n);\n unordered_set<int>st(restricted.begin(),restricted.end());\n for(auto &it:edges)\n {\n int u=it[0];\n int v=it[1];\n if(st.find(u)==st.end()&&st.find(v)==st.end())\n obj.unite(u,v);\n }\n\n for(auto it:obj.parent)\n {\n cout<<it<<\" \";\n }\n \n\n if(obj.parent[0]<0) return abs(obj.parent[0]);\n else \n return abs(obj.parent[obj.parent[0]]);\n\n\n }\n};",
"memory": "120300"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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(auto& e : edges)\n {\n if(ms.count(e[0]) || ms.count(e[1]))\n continue;\n int a = find(ds,e[0]);\n int b = find(ds,e[1]);\n if(b<a)\n swap(a,b);\n ds[a]+=ds[b];\n ds[b]=a;\n }\n return -ds[find(ds,0)];\n }\n};",
"memory": "121620"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 1 (21 mins, 48/62 passed): n =10\nedges = [[4,1],[1,3],[1,5],[0,5],[3,6],[8,4],[5,7],[6,9],[3,2]]\nrestricted = [2,7]\nOutput 2\nExpected 8\nReason: classic mistake: set root of root to new root, instead of setting root to new root!\n*/\n\nclass Solution {\npublic:\n template<typename T>\n void printVec(vector<T> cnt) {\n cout << \"[ \";\n for(auto& i : cnt) {\n cout << i << \" \";\n }\n cout << \"]\" << endl;\n }\n\n int find(int node, vector<int>& root) {\n // base case: root is itself\n if(root[node] == node) {\n return node;\n }\n int parent = root[node];\n int root_parent = this->find(parent, root); // find ultimate root of parent\n root[node] = root_parent; // compression of path\n return root_parent;\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n // init root\n vector<int> root(n, 0);\n for(size_t i = 1; i < n; ++i){\n root[i] = i; // init root to itself\n }\n \n unordered_set<int> res_set(restricted.begin(), restricted.end()); // convert to set for fast search\n // iter over edges\n for(auto& edge: edges) {\n int n1 = edge[0];\n int n2 = edge[1];\n if(res_set.count(n1) > 0 || res_set.count(n2) > 0) {\n continue; // do nothing, no union\n }\n // do union\n int root1 = this->find(n1, root);\n int root2 = this->find(n2, root);\n if(root1 == root2) { // already unioned to same root\n continue;\n }\n else if(root1 < root2) {\n root[root2] = root1; // note this is the bug!\n }\n else { // root1 > root2\n root[root1] = root2; // note this is the bug!\n }\n\n //cout << \"after edge (\" << n1 << \", \" << n2 << \"), root vector: \";\n //printVec(root);\n }\n\n // count those with root 0\n int count = 0;\n for(size_t i = 0; i < n; ++i) {\n int rooti = this->find(i, root);\n if(rooti == 0) {\n count++;\n }\n }\n return count;\n }\n};",
"memory": "122940"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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(parent, j);\n if (u != v) {\n if (abs(parent[u]) > abs(parent[v])) {\n parent[u] += parent[v];\n parent[v] = u;\n } else {\n parent[v] += parent[u];\n parent[u] = v;\n }\n }\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n unordered_set<int> restrictedSet(restricted.begin(), restricted.end());\n vector<int> parent(n, -1);\n\n for (auto& edge : edges) {\n int u = edge[0];\n int v = edge[1];\n if (restrictedSet.count(u) || restrictedSet.count(v)) {\n continue;}\n unite(parent, u, v);\n }\n\n int zero = find(parent, 0);\n int count = 0;\n for (int i = 0; i < n; i++) {\n if (!restrictedSet.count(i) && find(parent, i) == zero) {\n count++;\n }\n }\n\n return count;\n }\n};\n",
"memory": "124260"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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[parentA]=parentB;\n }\n \n int find(int node){\n if(node==parents[node]){\n return node;\n }\n return parents[node]=find(parents[node]);\n }\n \n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n parents.resize(n,0);\n unordered_set<int> track;\n for(int i=0;i<n;i++){\n parents[i]=i;\n }\n for(auto & rest : restricted){\n track.insert(rest);\n }\n \n for(auto &edge : edges){\n if(track.find(edge[0])==track.end() && track.find(edge[1])==track.end()){\n makeUnion(edge[0],edge[1]);\n }\n }\n int nodeReachable = 0;\n for(int i=0;i<n;i++){\n if(find(i)==0){\n nodeReachable++;\n }\n }\n return nodeReachable;\n \n \n }\n};",
"memory": "125580"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 Solution {\n int dfs(vector<vector<int>>& adj, vector<bool>& visitedOrRestricted, int node){\n visitedOrRestricted[node]=true;\n int curr=1;\n for(int child:adj[node]){\n if(!visitedOrRestricted[child])\n curr+=dfs(adj,visitedOrRestricted,child);\n }\n return curr;\n }\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<bool> visitedOrRestricted(n,false);\n for(int &node:restricted)\n visitedOrRestricted[node]=true;\n vector<vector<int>> adj(n);\n for(auto &edge:edges){\n int u=edge[0], v=edge[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n return dfs(adj,visitedOrRestricted,0);\n }\n};\n*/\n// DSU\nclass DSU {\n public:\n vector<int> parent,size;\n DSU(int n){\n parent.resize(n);\n iota(parent.begin(),parent.end(),0);\n size.resize(n,1);\n }\n int Find(int v){\n if(parent[v]==v) return v;\n return parent[v] = Find(parent[v]);\n }\n void Union(int a, int b){\n a=Find(a);\n b=Find(b);\n if(a==b) return;\n if(size[a]<size[b]) swap(a,b);\n parent[b]=a;\n size[a]+=size[b];\n }\n};\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n DSU dsu(n);\n // unordered_set<int> hset;\n // for(int &node:restricted)\n // hset.insert(node);\n unordered_set<int> hset(restricted.begin(), restricted.end());\n for(auto &edge:edges){\n int u=edge[0], v=edge[1];\n // if(hset.count(u) || hset.count(v)) continue;\n if(hset.count(u) + hset.count(v) == 0)\n dsu.Union(u,v);\n }\n // int parentOfZero = dsu.parent[0];\n // return dsu.size[parentOfZero==-1 ? 0 : parentOfZero];\n return dsu.size[ dsu.parent[0] ];\n }\n};\n",
"memory": "126900"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 if (rootA == rootB) return;\n if (rank[rootA] > rank[rootB])\n root[rootB] = rootA;\n else if (rank[rootA] < rank[rootB])\n root[rootA] = rootB;\n else {\n root[rootA] = rootB;\n rank[rootB]++;\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n root = vector<int>(n, 0);\n rank = vector<int>(n, 0);\n for (int i=0; i<n; i++)\n root[i] = i;\n int ans = 0;\n unordered_set<int> s;\n for (int i=0; i<restricted.size(); i++)\n s.insert(restricted[i]);\n for (int i=0; i<edges.size(); i++) {\n if (s.find(edges[i][0])!=s.end() || s.find(edges[i][1])!=s.end())\n continue;\n merge(edges[i][0], edges[i][1]);\n }\n for (int i=0; i<n; i++) {\n if (FindRoot(0) == FindRoot(i))\n ans++;\n }\n\n return ans;\n }\n};",
"memory": "128220"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 if (rootA == rootB) return;\n if (rank[rootA] > rank[rootB])\n root[rootB] = rootA;\n else if (rank[rootA] < rank[rootB])\n root[rootA] = rootB;\n else {\n root[rootA] = rootB;\n rank[rootB]++;\n }\n }\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n root = vector<int>(n, 0);\n rank = vector<int>(n, 0);\n for (int i=0; i<n; i++)\n root[i] = i;\n int ans = 0;\n unordered_set<int> s;\n for (int i=0; i<restricted.size(); i++)\n s.insert(restricted[i]);\n for (int i=0; i<edges.size(); i++) {\n if (s.find(edges[i][0])!=s.end() || s.find(edges[i][1])!=s.end())\n continue;\n merge(edges[i][0], edges[i][1]);\n }\n for (int i=0; i<n; i++) {\n if (FindRoot(0) == FindRoot(i))\n ans++;\n }\n\n return ans;\n }\n};",
"memory": "129540"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 = findParent(x);\n int b = findParent(y);\n parent[b]=a;\n }\n};\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n Disjoint* disjoint = new Disjoint(n);\n unordered_set<int>s;\n for(int i{};i<restricted.size();++i)s.insert(restricted[i]);\n for(int i{};i<edges.size();++i){\n auto it1 = s.find(edges[i][0]);\n auto it2 = s.find(edges[i][1]);\n if(it1!=s.end()||it2!=s.end())continue;\n disjoint->merge(edges[i][0],edges[i][1]);\n }\n int root = disjoint->findParent(0);\n int ans{};\n for(int i{};i<n;++i){\n int temp = disjoint->findParent(i);\n if(temp == root)++ans;\n }\n return ans;\n }\n};",
"memory": "129540"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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] = i;\n }\n\n for (const auto& edge : edges) {\n if (!obsticles.contains(edge[0]) && !obsticles.contains(edge[1])) {\n auto rootP = find_root(edge[0], roots);\n auto rootQ = find_root(edge[1], roots);\n\n if (rootP != rootQ) {\n roots[rootQ] = rootP;\n }\n }\n }\n\n for (int i = 0; i < n; ++i) {\n result += find_root(0, roots) == find_root(i, roots) ? 1 : 0;\n }\n\n return result;\n }\n\nprivate:\n int find_root(int root, vector<int>& roots)\n {\n return roots[root] == root ? root : roots[root] = find_root(roots[root], roots);\n }\n};",
"memory": "130860"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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<int, bool> m;\n for (int i = 0; i < restricted.size(); i++)\n m[restricted[i]] = true;\n vector<int> parent(n, 1);\n vector<int> size(n, 1);\n for (int i = 0; i < n; i++)\n parent[i] = i;\n for (auto& edge : edges) {\n int u = findP(edge[0], parent);\n int v = findP(edge[1], parent);\n if(m.find(u)!=m.end() or m.find(v)!=m.end())\n continue;\n if (u != v) {\n if (size[u] > size[v]) {\n parent[v] = u;\n size[u] += size[v];\n } else {\n parent[u] = v;\n size[v] += size[u];\n }\n }\n }\n return size[findP(0,parent)];\n }\n};",
"memory": "130860"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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[x]);\n }\n\n void unionSet(int x, int y) {\n int rootX = find(x);\n int rootY = find(y);\n if (rootX != rootY) {\n if (rank[rootX] >= rank[rootY]) {\n root[rootY] = rootX;\n rank[rootX] += rank[rootY];\n } else {\n root[rootX] = rootY;\n rank[rootY] += rank[rootX];\n }\n }\n }\n\nprivate:\n vector<int> root;\n vector<int> rank;\n};\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n UnionFind dsu(n);\n int m = edges.size();\n sort(restricted.begin(), restricted.end());\n for(auto edge : edges){\n if(binary_search(restricted.begin(), restricted.end(), edge[0]) || binary_search(restricted.begin(), restricted.end(), edge[1])) continue;\n dsu.unionSet(edge[0], edge[1]);\n }\n int ans = 0;\n int tar = dsu.find(0);\n for(int i = 0; i < n; i++){\n ans += (dsu.find(i) == tar);\n }\n return ans;\n }\n};",
"memory": "132180"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 findUPar(int node) {\n if (node == parent[node])\n return node;\n return parent[node] = findUPar(parent[node]);\n }\n\n void unionByRank(int u, int v) {\n int ulp_u = findUPar(u);\n int ulp_v = findUPar(v);\n if (ulp_u == ulp_v) return;\n if (rank[ulp_u] < rank[ulp_v]) {\n parent[ulp_u] = ulp_v;\n }\n else if (rank[ulp_v] < rank[ulp_u]) {\n parent[ulp_v] = ulp_u;\n }\n else {\n parent[ulp_v] = ulp_u;\n rank[ulp_u]++;\n }\n }\n\n void unionBySize(int u, int v) {\n int ulp_u = findUPar(u);\n int ulp_v = findUPar(v);\n if (ulp_u == ulp_v) return;\n if (size[ulp_u] < size[ulp_v]) {\n parent[ulp_u] = ulp_v;\n size[ulp_v] += size[ulp_u];\n }\n else {\n parent[ulp_v] = ulp_u;\n size[ulp_u] += size[ulp_v];\n }\n }\n};\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted){\n DisjointSet ds(n) ;\n set<int> s(restricted.begin(), restricted.end()) ;\n for(int i=0;i<edges.size();i++){\n int node = edges[i][0] ;\n int adjnode = edges[i][1] ;\n if(s.count(node) || s.count(adjnode)){\n continue ;\n }\n else{\n ds.unionByRank(node,adjnode) ;\n }\n }\n\n int cnt = 1 ;\n for(int i=1;i<n;i++){\n if(ds.findUPar(0) == ds.findUPar(i)){\n cnt++ ;\n }\n }\n return cnt ;\n }\n};",
"memory": "132180"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 int findupr(int x)\n {\n if(x==parent[x])\n return x;\n return parent[x]=findupr(parent[x]);\n }\n\n void unionbysize(int x,int y)\n {\n int ux=findupr(x),uy=findupr(y);\n if(ux==uy) return;\n if(size[ux]>size[uy])\n {\n size[ux]+=size[uy];\n parent[uy]=ux;\n }\n else\n {\n size[uy]+=size[ux];\n parent[ux]=uy;\n }\n }\n};\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<int>r(n,0);\n for(auto it:restricted) r[it]=1;\n \n disjointst ds(n);\n for(auto it:edges)\n {\n int u=it[0],v=it[1];\n int ulu=ds.findupr(u),ulv=ds.findupr(v);\n if(!r[u] and !r[v] and ulu!=ulv)\n ds.unionbysize(ulu,ulv);\n }\n int root=ds.findupr(0);\n\n return ds.size[root];\n }\n};",
"memory": "133500"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 {\n if(parent[x]==x)\n return x;\n return parent[x]=findupr(parent[x]);\n }\n\n void unionbysize(int u,int v)\n {\n int ulv=findupr(v),ulu=findupr(u);\n\n if(ulv==ulu) return;\n else if(size[ulv]<size[ulu])\n {\n parent[ulv]=ulu;\n size[ulu]+=size[ulv];\n }\n else\n {\n parent[ulu]=ulv;\n size[ulv]+=size[ulu];\n }\n }\n};\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n int cnt=0;\n vector<int>r(n,0);\n \n for(auto it:restricted) r[it]=1;\n disjointst ds(n);\n\n for(auto it:edges)\n {\n int u=it[0],v=it[1];\n if(!r[u] and !r[v])\n ds.unionbysize(u,v);\n }\n int k=ds.findupr(0);\n return ds.size[k];\n }\n};",
"memory": "133500"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 }\n for (int i = 0; i < edges.size(); i++){\n if (allowed[edges[i][0]] && allowed[edges[i][1]]){\n visitableVerticles[edges[i][0]].push_back(edges[i][1]);\n visitableVerticles[edges[i][1]].push_back(edges[i][0]);\n }\n }\n \n if (!allowed[0]) return 0; \n int res = 0;\n vector<bool> visited(n, false);\n\n stack<int> leftVerticles;\n leftVerticles.push(0);\n int i;\n while(!leftVerticles.empty()){\n i = leftVerticles.top();\n leftVerticles.pop();\n visited[i] = true;\n res++;\n for (int j = 0; j < visitableVerticles[i].size(); j++){\n if (!visited[visitableVerticles[i][j]]) leftVerticles.push(visitableVerticles[i][j]);\n }\n }\n return res;\n }\n};",
"memory": "134820"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 }\n else if(py<px){\n parent[px] = py;\n }\n else{\n parent[px] = py;\n }\n}\nint compressor_find(int x,vector<int>& parent){\n if(x==parent[x]){\n return x;\n }\n return parent[x] = compressor_find(parent[x],parent);\n}\nvoid show(vector<int> parent,int l=-5,int i=0){\n if(l==-5){\n l= parent.size();\n }\n if(parent[i]!=-55){\n cout<<i<<\": \"<<parent[i]<<endl;\n }\n if(i+1<=l-1){\n show(parent,l,i+1);\n }\n}\nvoid show1(vector<vector<int>> a,int l=-5,int i=0){\n if(l==-5){\n l=a.size();\n }\n cout<<a[i][0]<<\" & \"<<a[i][1]<<endl;\n if(i+1<=l-1){\n show1(a,l,i+1);\n }\n}\nint barking_pork(vector<vector<int>> edges,vector<int> restricted,int n){\n int edges_size = edges.size();\n int res_size = restricted.size();\n vector<int> parent(n,0);\n for(int i=0;i<n;i++){\n parent[i]=i;\n }\n for(int i=0;i<res_size;i++){\n parent[restricted[i]] = -55;\n }\n for(int i=0;i<edges_size;i++){\n int x = edges[i][0];int y = edges[i][1];\n if(parent[x]==-55 || parent[y]==-55){\n continue;\n }\n union1(x,y,parent);\n }\n for(int i=0;i<n;i++){\n if(parent[i]!=-55){\n compressor_find(i,parent);\n }\n }\n int count=0;\n for(int i=0;i<n;i++){\n if(parent[i]==0){\n count++;\n }\n }\n return count;\n}\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n \n return barking_pork(edges,restricted,n);\n }\n};",
"memory": "136140"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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(int n, vector<vector<int>>& e, vector<int>& r) {\n vector<int>v(n,0);\n for(int i=0;i<r.size();i++)\n {\n v[r[i]]++;\n }\n vector<int>adj[n];\n for(int i=0;i<e.size();i++)\n {\n if(v[e[i][0]]!=0 || v[e[i][1]]!=0)\n {\n continue;\n }\n adj[e[i][0]].push_back(e[i][1]);\n adj[e[i][1]].push_back(e[i][0]);\n }\n d(0,-1,adj);\n return cnt;\n }\n};",
"memory": "137460"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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, int y) {\n int rootX = find(x), rootY = find(y);\n if (rootX != rootY) {\n if (rank[rootX] > rootY) {\n swap(rootX, rootY);\n }\n root[rootX] = rootY;\n rank[rootY] += rank[rootX];\n }\n }\n int getSize(int x) {\n return rank[find(x)];\n }\n};\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n UnionFind uf(n);\n unordered_set<int> restSet(restricted.begin(), restricted.end());\n\n for (auto edge : edges) {\n int a = edge[0], b = edge[1];\n if (restSet.find(a) == restSet.end() && restSet.find(b) == restSet.end()) {\n uf.join(a, b);\n }\n }\n \n return uf.getSize(0);\n }\n};",
"memory": "138780"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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, int y) {\n int rootX = find(x), rootY = find(y);\n if (rootX != rootY) {\n if (rank[rootX] > root[rootY]) {\n swap(rootX, rootY);\n }\n root[rootX] = rootY;\n rank[rootY] += rank[rootX];\n }\n }\n int getSize(int x) {\n return rank[find(x)];\n }\n};\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n UnionFind uf(n);\n unordered_set<int> restSet(restricted.begin(), restricted.end());\n\n for (auto edge : edges) {\n int a = edge[0], b = edge[1];\n if (restSet.find(a) == restSet.end() && restSet.find(b) == restSet.end()) {\n uf.join(a, b);\n }\n }\n \n return uf.getSize(0);\n }\n};",
"memory": "140100"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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, int y) {\n int rootX = find(x), rootY = find(y);\n if (rootX != rootY) {\n root[rootX] = rootY;\n rank[rootY] += rank[rootX];\n }\n }\n int getSize(int x) {\n return rank[find(x)];\n }\n};\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n UnionFind uf(n);\n unordered_set<int> restSet(restricted.begin(), restricted.end());\n\n for (auto edge : edges) {\n int a = edge[0], b = edge[1];\n if (restSet.find(a) == restSet.end() && restSet.find(b) == restSet.end()) {\n uf.join(a, b);\n }\n }\n \n return uf.getSize(0);\n }\n};",
"memory": "140100"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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, int y) {\n int rootX = find(x), rootY = find(y);\n if (rootX != rootY) {\n if (rank[rootX] > root[rootY]) {\n swap(rootX, rootY);\n }\n root[rootX] = rootY;\n rank[rootY] += rank[rootX];\n }\n }\n int getSize(int x) {\n return rank[find(x)];\n }\n};\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n UnionFind uf(n);\n unordered_set<int> restSet(restricted.begin(), restricted.end());\n\n for (auto edge : edges) {\n int a = edge[0], b = edge[1];\n if (restSet.find(a) == restSet.end() && restSet.find(b) == restSet.end()) {\n uf.join(a, b);\n }\n }\n \n return uf.getSize(0);\n }\n};",
"memory": "141420"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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, int y) {\n int rootX = find(x), rootY = find(y);\n if (rootX != rootY) {\n if (rank[rootX] > root[rootY]) {\n swap(rootX, rootY);\n }\n root[rootX] = rootY;\n rank[rootY] += rank[rootX];\n }\n }\n int getSize(int x) {\n return rank[find(x)];\n }\n};\n\nclass Solution {\npublic:\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n UnionFind uf(n);\n unordered_set<int> restSet(restricted.begin(), restricted.end());\n\n for (auto edge : edges) {\n int a = edge[0], b = edge[1];\n if (restSet.find(a) == restSet.end() && restSet.find(b) == restSet.end()) {\n uf.join(a, b);\n }\n }\n \n return uf.getSize(0);\n }\n};",
"memory": "141420"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 int u = a[0], v = a[1];\n if (!used[u] && !used[v]) {\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n }\n\n int sum = 0;\n auto dfs = [&](auto& self, int v) -> void {\n if (used[v] == 0) {\n used[v] = 1, sum++;\n for (auto & u : adj[v]) {\n self(self, u);\n }\n }\n };\n \n dfs(dfs, 0);\n return sum;\n }\n};",
"memory": "142740"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 int u = a[0], v = a[1];\n if (!used[u] && !used[v]) {\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n }\n\n auto dfs = [&](auto& self, int v) -> void {\n if (used[v] == 0) {\n used[v] = 1;\n for (auto & u : adj[v]) {\n self(self, u);\n }\n }\n };\n\n dfs(dfs, 0);\n\n int sum = 0;\n for (auto & x : used)\n if (x > 0)\n sum += x;\n return sum;\n }\n};",
"memory": "144060"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 int u = a[0], v = a[1];\n if (!used[u] && !used[v]) {\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n }\n\n int sum = 0;\n auto dfs = [&](auto& self, int v) -> void {\n if (used[v] == 0) {\n used[v] = 1, sum++;\n for (auto & u : adj[v]) {\n self(self, u);\n }\n }\n };\n dfs(dfs, 0);\n \n return sum;\n }\n};",
"memory": "144060"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 : edges){\n if (!seen[e[1]] && !seen[e[0]]){\n graph[e[0]].push_back(e[1]);\n graph[e[1]].push_back(e[0]);\n }\n }\n return dfs(0, graph, seen);\n }\nprivate:\n int dfs(int node, vector<vector<int>>& graph, bool seen[]){\n int res = 1;\n seen[node] = true;\n for (auto& neighbor : graph[node])\n if (!seen[neighbor])\n res += dfs(neighbor, graph, seen);\n return res;\n }\n};",
"memory": "145380"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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, graph);\n }\n }\n\n return answer + 1;\n }\n\n int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n vector<vector<int>> graph(n);\n vector<bool> check(n, true);\n\n for (int val : restricted)\n {\n check[val] = false;\n }\n\n\n for (auto& elem : edges)\n {\n if (check[elem[0]] && check[elem[1]])\n {\n graph[elem[0]].push_back(elem[1]);\n graph[elem[1]].push_back(elem[0]);\n }\n }\n\n return dfs(0, -1, graph);\n }\n};",
"memory": "145380"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 rest[n];\n memset(rest, 0, sizeof(rest));\n for(int i=0;i<restricted.size();i++) {\n //adj[restricted[i]].clear();\n rest[restricted[i]] = 1;}\n queue<int> q;\n q.push(0);\n int ans = 0;\n bool vis[n];\n memset(vis, 0, sizeof(vis));\n vis[0] = 1;\n while(!q.empty()) {\n int temp = q.front();\n q.pop();\n ans++;\n for(auto &a : adj[temp]) {\n if(!vis[a] && !rest[a]) {\n vis[a] = 1;\n q.push(a);\n }\n }\n }\n return ans;\n }\n};",
"memory": "146700"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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 if ((rest.find(edges[i][0]) == rest.end()) &&\n (rest.find(edges[i][1]) == rest.end())) {\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n }\n int c = 1;\n vector<int> vis(n, 0);\n queue<int> q;\n q.push(0);\n while (!q.empty()) {\n int node = q.front();\n vis[node]=1;\n q.pop();\n for (auto it : adj[node]) {\n if(!vis[it]){\n q.push(it);\n c++;\n\n }\n }\n }\n return c;\n }\n};",
"memory": "146700"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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<int>vis(n,0);\n for(int i=0;i<restricted.size();i++){\n vis[restricted[i]]=1;\n }\n queue<int>q;\n q.push(0);\n int cnt=1;\n while(!q.empty()){\n int node = q.front();\n vis[node]=1;\n q.pop();\n for(auto it:adj[node]){\n if(!vis[it]){\n cnt++;\n q.push(it);\n }\n }\n }\n return cnt;\n }\n};",
"memory": "148020"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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\n for (vector<int> &e: edges) {\n if (r.find(e[0]) != r.end() || r.find(e[1]) != r.end()) continue;\n g[e[0]].push_back(e[1]);\n g[e[1]].push_back(e[0]);\n }\n\n s.push(0);\n notVisited[0] = false;\n\n while (!s.empty()) {\n int u = s.top();\n s.pop();\n for (int v: g[u]) {\n if (notVisited[v]) {\n notVisited[v] = false;\n count++;\n s.push(v);\n }\n }\n }\n\n return count;\n }\n};",
"memory": "148020"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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\n for (vector<int> &e: edges) {\n if (r.find(e[0]) != r.end() || r.find(e[1]) != r.end()) continue;\n g[e[0]].push_back(e[1]);\n g[e[1]].push_back(e[0]);\n }\n\n s.push(0);\n notVisited[0] = false;\n\n while (!s.empty()) {\n int u = s.top();\n s.pop();\n for (int v: g[u]) {\n if (notVisited[v]) {\n notVisited[v] = false;\n count++;\n s.push(v);\n }\n }\n }\n\n return count;\n }\n};",
"memory": "149340"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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]] && !res[it[1]]){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n }\n int cnt=0;\n queue<int>q;\n q.push(0);\n while(!q.empty()){\n int node = q.front();\n q.pop();\n if(vis[node]) continue;\n vis[node]=1;\n cnt++;\n for(auto it:adj[node]){\n q.push(it);\n }\n }\n return cnt;\n }\n};",
"memory": "149340"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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(int n, vector<vector<int>>& edges, vector<int>& restricted) {\n int m=n-1;\n for(int i=0;i<m;i++)\n {\n ke[edges[i][0]].push_back(edges[i][1]);\n ke[edges[i][1]].push_back(edges[i][0]);\n }\n for(int i=0;i<restricted.size();i++)\n {\n visited[restricted[i]]=1;\n }\n dfs(0);\n int d=0;\n for(int i=0;i<n;i++)\n {\n if(visited[i]==1)\n ++d;\n }\n return d-restricted.size();\n }\n};",
"memory": "150660"
} |
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 between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array <code>restricted</code> which represents <strong>restricted</strong> nodes.</p>
<p>Return <em>the <strong>maximum</strong> number of nodes you can reach from node </em><code>0</code><em> without visiting a restricted node.</em></p>
<p>Note that node <code>0</code> will <strong>not</strong> be a restricted node.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png" style="width: 402px; height: 322px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png" style="width: 412px; height: 312px;" />
<pre>
<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The diagram above shows the tree.
We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
<li><code>1 <= restricted.length < n</code></li>
<li><code>1 <= restricted[i] < n</code></li>
<li>All the values of <code>restricted</code> are <strong>unique</strong>.</li>
</ul>
| 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> visited(n, false);\n for (int r : restricted) visited[r] = true;\n\n if (visited[0]) return 0;\n\n stack<int> s;\n s.push(0);\n int count = 0;\n while (!s.empty()) {\n int node = s.top(); s.pop();\n ++count;\n visited[node] = true;\n for (int nbr : adj[node]) {\n if (!visited[nbr]) s.push(nbr);\n }\n }\n\n return count;\n }\n};",
"memory": "150660"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.