id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
154
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,4,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,4]</code> if it was rotated <code>4</code> times.</li> <li><code...
0
{ "code": "class Solution {\npublic:\n int findMin(vector<int>& nums) {\n int n = nums.size();\n if(n==1) {\n return nums[0];\n }\n if(n==2) {\n return min(nums[0], nums[1]);\n }\n \n if(nums[0] < nums[n-1]) {\n return nums[0];\n ...
154
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,4,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,4]</code> if it was rotated <code>4</code> times.</li> <li><code...
0
{ "code": "class Solution {\npublic:\n\n\n int findMin(vector<int>& nums) {\n int low = 0, high = nums.size() - 1;\n int res = nums[0];\n while (low <= high) {\n int pivot = low + (high - low) / 2;\n res = min(res, nums[pivot]);\n if (nums[pivot] < nums[high])\...
154
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,4,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,4]</code> if it was rotated <code>4</code> times.</li> <li><code...
0
{ "code": "class Solution {\npublic:\n int findMin(vector<int>& nums) {\n int start = 0;\n int end = nums.size()-1;\n int ans = 0;\n while(start<end){\n int mid = start + (end-start)/2;\n if(nums[start]==nums[mid] && nums[mid]==nums[end]){\n start++;...
154
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,4,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,4]</code> if it was rotated <code>4</code> times.</li> <li><code...
3
{ "code": "class Solution {\npublic:\n int findMin(vector<int>& a) {\n sort(a.begin(),a.end());\n return a[0];\n }\n};", "memory": "15500" }
154
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,4,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,4]</code> if it was rotated <code>4</code> times.</li> <li><code...
3
{ "code": "class Solution {\npublic:\n int findMin(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n return nums[0];\n }\n};", "memory": "15500" }
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\ni...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n\n\n /* This is one of the solution possible::\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *he...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n int init = []\n{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for(st...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nint init = [] {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for (stri...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": " int init = []\n{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for(string s; getline(std::cin, s);)\n {\n if(s[0] != '0') out << \"Intersected at '\" << s << \"'\\n\";\n else out << \"No intersection\\n\";\n for(i...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n int init = []\n{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for(s...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": " int init = []\n{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for(string s; getline(std::cin, s);)\n {\n if(s[0] != '0') out << \"Intersected at '\" << s << \"'\\n\";\n else out << \"No intersection\\n\";\n for(i...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": " int init = []\n{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n for(string s; getline(std::cin, s);)\n {\n if(s[0] != '0') out << \"Intersected at '\" << s << \"'\\n\";\n else out << \"No intersection\\n\";\n for(i...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n if(!headA || !headB)\n retu...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n if(!headA || !headB) return NULL;\...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n int len1=length(headA);\n i...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode *temp1=headA;\n Li...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n if(headA == headB) return headA;\n...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* ans;\n void traverse(ListNode* headA,ListNode* headB){\n if(!headA){while(headB){if(headB...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode*t1=headA;\n ListNo...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode* h1 = headA;\n Lis...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n ListNode* temp = headA;\n v...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n \n stack< ListNode*> s1 ;\n...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "// write a cpp program to fins the intersection os the linked_list\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n vector<ListNode*>adress;\n while(headA!=NULL){\n adress.push_back(headA);\n headA=headA->next;\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "class Solution {\npublic:\n ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n vector<ListNode*> v;\n ListNode* p = headB;\n while (p) {\n v.push_back(p);\n p = p->next;\n }\n\n p = headA;\n\n int size = v.size();\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n ListNode* curr = new ListNode(0);\...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n std::vector<ListNode*> pAvec;\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n\nclass Solution {\npublic:\nListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n stack<ListNode*> a;\n stack<ListNode*...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode* h1 = new ListNode(0,head...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nint len(ListNode *head){\n int c=0;\n while(head!=NULL){\n c++;\n head=head->next;\n }\n return c;\n}\nclass Solut...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n vector<ListNode*> firstPath;\n vector<ListNode*> secondPath;\n \n ListNode *getIntersectionNode(List...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n vector<ListNode *> a;\n vec...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\ntypedef ListNode Node;\n#include<vector>\nint notin(long long l,const std::vector<Node*>& arr,Node* n){\n int t=1;\n for(long long i=0...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) \n {\n if(headA==headB)\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n set<ListNode*>m;\n\n ListNo...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n if(headA==NULL||headB==NULL)return...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode *curr1=headA;\n Li...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "class Solution {\n public:\n ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n std::unordered_set<ListNode*> nodes;\n ListNode* a = headA;\n while (a != nullptr) {\n nodes.insert(a);\n a = a->next;\n }\n ListNode* b = headB;\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n std::unordered_set<ListNode*> h;\n...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> st;\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "#include <unordered_set>\n\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n std::unordered_set<ListNode*> nodesInA;\n \n // Traverse the first list and add all nodes to the set\n while (headA) {\n nodesInA.insert(head...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> pointer_s...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {\n unordered_set<ListNode*> st;\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> hashset;\...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n #pragma GCC target(\"tune=native\")\n#pragma GCC optimize(\"Ofast\")\n//#pragma GCC optimize(\"Os\")\n// Code here is optimized for size\n\...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> st;\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode* , int> mpp...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode*,bool>m;\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode*, int>m;\n\n L...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode* tempA = headA;\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n ListNode* tempA = headA;\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode*,int> m;\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode *,int> mp;\n L...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode*,int> mpp;\n L...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode*,int> mp;\n Li...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*>seen;\n\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> mp;\n\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n if(headA==headB)return headA;\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_set<ListNode*> s;\n\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n\n if(headA == headB) return headA;...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void fillSet(ListNode *headA, std::set<ListNode*> &s)\n {\n s.insert(headA);\n if (headA)\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n set<ListNode*> list;\n whil...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n set<ListNode *>st;\n ListNode *...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n std::set<ListNode *> a;\n s...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n set<ListNode*> mpp1;\n set<...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n // 透過記憶體位址來確認是否指向同一個 node\n ...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n int counta,countb;\n ListNode...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode*,int> mpp;\n L...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n map<ListNode*,bool> m;\n Li...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode* ,bool>visit...
160
<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n unordered_map<ListNode*,int> check...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "/*\n[0, 2] [2, 4] [0, 5]\nzybvxf\nbfz\n\nif we can group indices which are kind of connected, and then can sort the characters on those indices and if \nwe do this for every group we will get the lexicographically smallest string.\n*/\n\nstruct DSU {\n vector<int> par, rank;\n \n DSU(int n) {\n ...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class UnionFind {\npublic:\n UnionFind(int sz) : root(sz), rank(sz), count(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\t// Some ranks may beco...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class Solution {\npublic:\n int findUPar(int node,vector<int> &parent)\n {\n if(parent[node]==node)return node;\n return parent[node]=findUPar(parent[node],parent);\n }\n void unionBySize(vector<int> &size,int node1,int node2,vector<int> &parent)\n {\n int ulPar1=findUPa...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class Solution {\npublic:\n int findP(int node, vector<int>& parent)\n\t{\n\t if(parent[node]==node)\n\t {\n\t return node;\n\t }\n\t return parent[node]=findP(parent[node], parent);\n\t}\n\t\n\tvoid unionSize (int u, int v, vector<int>& parent, vector<int>& size)\n\t{\n\t int paru...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "\nclass UnionFind {\n public:\n UnionFind(int n) : id(n), rank(n) {\n iota(id.begin(), id.end(), 0);\n }\n\n void unionByRank(int u, int v) {\n const int i = find(u);\n const int j = find(v);\n if (i == j)\n return;\n if (rank[i] < rank[j]) {\n id[i] = j;\n } else if (rank[i]...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class Solution {\npublic:\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n int n = s.size();\n vector<int> parent(n);\n iota(parent.begin(), parent.end(), 0);\n function<int(int)> find = [&](int x) {\n return parent[x] == x ? x : parent[x]...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class UniFond{\n int* root;\n int* rank;\n public:\n UniFond(int size){\n root=new int[size];\n rank=new int[size];\n for(int i=0;i<size;i++){\n root[i]=i;\n }\n }\n int find(int node){\n if (node==root[node...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "#include <vector>\n#include <string>\n#include <algorithm>\n#include <unordered_map>\nusing namespace std;\n\nclass Solution {\npublic:\n // Union-Find (Disjoint Set Union) Helper functions\n int find(int x, vector<int>& parent) {\n if (parent[x] != x) {\n parent[x] = find(parent[x]...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class DSU {\npublic:\n vector<int> parent;\n vector<int> rank;\n\n DSU(int n) {\n parent.resize(n);\n rank.resize(n, 1);\n for (int i = 0; i < parent.size(); i++) {\n parent[i] = i;\n }\n }\n\n int find(int x) {\n if (x != parent[x]) {\n ...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class Solution {\npublic:\n vector<int> rank, par;\n Solution() {\n // Default constructor implementation\n }\n Solution(int n){\n rank = vector<int> (n,1);\n for(int i=0; i<n; i++){\n par.push_back(i);\n }\n }\n int find(int x){\n if(par[x] =...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class Solution {\npublic:\n \n int find(int i,vector<int> &parent){\n if(parent[i]==-1) return i;\n return parent[i]=find(parent[i],parent);\n }\n \n void unio(int a,int b,vector<int> &parent){\n int xs=find(a,parent);\n int ys=find(b,parent);\n if(xs!=ys){...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class UnionFind {\npublic:\n UnionFind(const int size) : ranks_(size), parents_(size) {\n std::iota(parents_.begin(), parents_.end(), 0);\n }\n\n void union_set(const int x, const int y) {\n const auto x_parent = find(x);\n const auto y_parent = find(y);\n if (x_parent ...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class DSU{\npublic:\n vector<int>parent;\n vector<int>size;\n DSU(int n){\n parent.resize(n);\n size.resize(n,1);\n for(int i=0;i<n;i++)\n parent[i]=i;\n }\n int find(int u){\n if(u==parent[u])\n return u;\n return parent[u]=find(paren...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "\n\nclass Solution {\npublic:\nclass DSU {\nprivate:\n vector<int> parent, rank;\n\npublic:\n // Initialize the DSU with n elements\n DSU(int n) {\n parent.resize(n);\n rank.resize(n, 0);\n for (int i = 0; i < n; ++i) {\n parent[i] = i;\n }\n }\n\n // F...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class Solution {\npublic:\n vector<int> root;\n vector<int> rank;\n int find(int x) {\n if(x == root[x]) return x;\n root[x] = find(root[x]);\n return root[x];\n }\n void Union(int x, int y) {\n int rx = find(x);\n int ry = find(y);\n if(rx!=ry) {\n ...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class Solution {\n class DSU{\n public:\n vector<int> parents;\n vector<int> nodeSize;\n DSU(int n){\n for(int i=0;i<n;i++) parents.push_back(i);\n nodeSize.resize(n,1);\n }\n int findParent(int node){\n if(parents[node]==node) retur...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class Solution {\npublic:\nclass dsu{\n private:\n vector<int> parent;\n vector<int> rank;\n public:\n dsu(int x){\n parent.resize(x);\n rank.resize(x);\n for(int i =0; i < x;i++){\n parent[i] = i;\n rank[i] = 1;\n }\n }\n bool connect(...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class DisJoint{\n public:\n vector<int>parent;\n vector<int>size;\n DisJoint(int n)\n {\n parent.resize(n,0);\n size.resize(n,0);\n for(int i=0;i<n;i++)\n {\n parent[i]=i;\n }\n }\n int findParent(int node)\n {\n if(node==parent[n...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class Solution {\npublic:\n \n int find_uparent(int x, vector<int>& parent)\n {\n if(parent[x]==x) return x;\n return parent[x]=find_uparent(parent[x], parent);\n }\n string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {\n // find all elements that...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
0
{ "code": "class Disjoint\n{\npublic:\n vector<int> parent,size;\n Disjoint(int n)\n {\n parent.resize(n,0);\n for(int i=0;i<n;i++)\n parent[i]=i;\n size.resize(n,1);\n\n }\n\n int findpar(int u)\n {\n if(parent[u]==u)\n return u;\n else\n ...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
1
{ "code": "class DSU {\nprivate:\n vector<int> parents;\n vector<int> sizes;\npublic:\n DSU(int n) {\n parents.resize(n);\n iota(parents.begin(), parents.end(), 0);\n sizes.assign(n, 1);\n }\n\n int find(int a) {\n if (parents[a] != a) {\n return parents[a] = find...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
1
{ "code": "class Solution {\n class unionFind {\n vector<int> root;\n vector<int> rank;\n \n public:\n unionFind(int sz) : root(sz), rank(sz, 1) {\n for (int i = 0; i < sz; i++) {\n root[i] = i;\n }\n }\n\n int find(int x) {\n ...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
1
{ "code": "class Solution {\npublic:\n class UnionFind {\n vector<int> ids, sz;\n int num_components;\n\n public:\n UnionFind(int n) : ids(n), sz(n, 1), num_components(n) {\n for (int i = 0; i < n; ++i) {\n ids[i] = i;\n }\n }\n\n int find(...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
1
{ "code": "class Solution {\npublic:\n int find(int x,vector<int> &parent){\n if(parent[x]==-1){\n return x;\n }\n return parent[x]=find(parent[x],parent);\n }\n\n void uni(int u,int v,vector<int> &rank,vector<int> &parent){\n int p1=find(u,parent);\n int p2=find...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
1
{ "code": "using ll = long long;\nclass DSU{\n public:\n vector<int> rank;\n vector<int> parent;\n int n;\n DSU(int n){\n this->n=n;\n rank=vector<int>(n,1);\n parent=vector<int>(n,0);\n iota(parent.begin(),parent.end(),0);\n \n }\n ll find(ll a){\n if(pa...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
1
{ "code": "using ll = long long;\nclass DSU{\n public:\n vector<int> rank;\n vector<int> parent;\n int n;\n DSU(int n){\n this->n=n;\n rank=vector<int>(n,1);\n parent=vector<int>(n,0);\n iota(parent.begin(),parent.end(),0);\n \n }\n ll find(ll a){\n if(pa...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
1
{ "code": "class Solution {\n private:\n vector<int> parent;\n vector<int> rank;\n\n public:\n int find(int x) {\n if (parent[x] == x) return x;\n return parent[x] = find(parent[x]);\n }\n void join(int x, int y) {\n int p1 = find(x);\n int p2 = find(y);\n if (p1 != p2) {\n if (rank[p1] > ran...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
1
{ "code": "class Solution {\npublic:\n vector<int> parents;\n vector<int> ranks;\n int uffind(int a){\n if(parents[a] == a)return a;\n parents[a] = uffind(parents[a]);\n return parents[a];\n }\n void ufjoin(int a, int b){\n int aroot{uffind(a)}, broot{uffind(b)};\n if...
1,308
<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p> <p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbs...
1
{ "code": "#include<bits/stdc++.h>\n#define ll long long\nusing namespace std;\n\nclass disjoint {\n ll* parent;\n ll* size;\npublic:\n disjoint(ll n) {\n parent = new ll[n];\n size = new ll[n];\n for (ll i = 0; i < n; i++) {\n parent[i] = i;\n size[i] = 1;\n ...