id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if(head == NULL) return nullptr;\n\n ListNode* slow = head;\n ListNode* fast = head;\n // int counter=0;\n\n while(fast&& fast->next){\n slow = slow->next;\n fast = fast->next->next;\n // counter++;\n if(slow==fast){\n slow=head;\n while(slow!=fast){\n slow = slow->next;\n fast = fast->next;\n }\n return slow;\n }\n }\n return nullptr;\n }\n};",
"memory": "10000"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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//首先设立两个指针, 快指针每次走 2步, 慢指针每次走 1步\n//如果有环, 快慢指针最终一定会相遇, 且一定不为空\n//这时快指针刚好比慢指针多绕了一圈\n//这时如果快指针退回起点, 然后两个指针都每次走 1步\n//再次相遇的时候刚好就是环的起点\n\nclass Solution {\npublic:\n ListNode *detectCycle(ListNode *head) {\n ListNode* slow=head;\n ListNode* fast=head;\n while(slow!=NULL&&fast!=NULL&&fast->next!=NULL){\n slow=slow->next;\n fast=fast->next->next;\n if(fast==slow) break;\n }\n if(fast==NULL || fast->next==NULL) return NULL; //表示没环,空的\n fast=head;\n while(fast!=slow){\n fast=fast->next;\n slow=slow->next;\n }\n return fast;\n }\n};",
"memory": "10100"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if(!head || !head -> next)\n return NULL;\n ListNode* slow = head;\n ListNode* fast = head;\n while(slow && fast && fast -> next){\n slow = slow -> next;\n fast = fast -> next -> next ;\n if(slow == fast)\n break;\n }\n if(slow != fast)\n return NULL;\n slow = head;\n while(true && slow != fast){\n slow = slow -> next ;\n fast = fast -> next ;\n if(slow == fast)\n break;\n }\n return slow;\n }\n};",
"memory": "10200"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 0 | {
"code": "class Solution {\n public:\n ListNode* detectCycle(ListNode* head) {\n ListNode* slow = head;\n ListNode* fast = head;\n while (fast && fast->next) {\n slow = slow->next;\n fast = fast->next->next;\n if (slow == fast) {\n slow = head;\n while (slow != fast) {\n slow = slow->next;\n fast = fast->next;\n }\n return slow;\n }\n }\n return nullptr;\n }\n};\n",
"memory": "10200"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n ListNode* slow=head;\n ListNode* fast=head;\n while(fast!=NULL and fast->next!=NULL){\n slow=slow->next;\n fast=fast->next->next;\n if(slow==fast){\n slow=head;\n while(slow!=fast){\n slow=slow->next;\n fast=fast->next;\n }\n return slow; //starting point\n }\n }\n return NULL;\n }\n};",
"memory": "10300"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 0 | {
"code": "class Solution {\npublic:\n ListNode *detectCycle(ListNode *head) {\n\n ListNode* slow = head;\n ListNode* fast = head;\n\n while(fast && fast->next) {\n slow = slow->next;\n fast = fast->next->next;\n\n if(slow == fast) {\n slow = head;\n\n while(slow != fast) {\n slow = slow->next;\n fast = fast->next;\n }\n\n return slow;\n }\n } \n \n return NULL;\n }\n};",
"memory": "10300"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n ListNode* slow = head;\n ListNode* fast = head;\n\n // Detect the cycle using Floyd's Cycle Detection Algorithm\n while (fast != NULL && fast->next != NULL) {\n slow = slow->next;\n fast = fast->next->next;\n if (slow == fast) {\n break; // Cycle detected\n }\n }\n\n // If no cycle is detected, return NULL\n if (fast == NULL || fast->next == NULL) {\n return NULL;\n }\n\n // Find the start of the cycle\n slow = head;\n while (slow != fast) {\n slow = slow->next;\n fast = fast->next;\n }\n return slow;\n \n }\n \n};",
"memory": "10400"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n ListNode* fast=meetNode(head);\n if(fast==NULL) return NULL;\n ListNode* slow=head;\n while(slow!=fast){\n slow=slow->next;\n fast=fast->next;\n }\n return slow;\n }\n ListNode *meetNode(ListNode* head){\n if(head==NULL || head->next==NULL) return NULL;\n ListNode* slow=head,*fast=head;\n while(fast!=NULL && fast->next!=NULL){\n slow=slow->next;\n fast=fast->next->next;\n if(slow==fast) return fast;\n }\n return NULL;\n \n }\n};",
"memory": "10400"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 2 | {
"code": "class Solution {\npublic:\n ListNode *detectCycle(ListNode *head) {\n\n ListNode* slow = head;\n ListNode* fast = head;\n\n while(fast && fast->next) {\n slow = slow->next;\n fast = fast->next->next;\n\n if(slow == fast) {\n slow = head;\n\n while(slow != fast) {\n slow = slow->next;\n fast = fast->next;\n }\n\n return slow;\n }\n } \n \n return NULL;\n }\n};",
"memory": "10500"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 2 | {
"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 *detectCycle(ListNode *head) {\n ListNode* slow = head;\n ListNode* fast = head;\n while(fast!=NULL && fast->next!=NULL){\n slow = slow->next;\n fast = fast->next->next;\n if(slow==fast){\n slow = head;\n while(slow!=fast){\n slow = slow->next;\n fast = fast->next;\n }\n return slow;\n }\n }\n return NULL;\n }\n};",
"memory": "10500"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 bool findTarget(ListNode* head,int pos)\n {\n ListNode* targetNode=head;\n int i=0;\n for(;i<pos;i++)\n {\n targetNode=targetNode->next;\n }\n head=targetNode->next;\n for(i=pos;i<10000+pos;i++)\n {\n if(head==targetNode)\n {\n return true;\n }\n head=head->next;\n }\n return false;\n }\n ListNode *detectCycle(ListNode *head) {\n if(!head)\n {\n return nullptr;\n }\n if(head->next==head)\n {\n return head;\n }\n //int i=0;\n //int len=10000;\n ListNode *curNode=head;\n unordered_set<ListNode*> nodeset{head};\n for(int i=0;i<0;i++)\n {\n if(!head->next)\n {\n return nullptr;\n }\n if(nodeset.find(head->next)!=nodeset.end())\n {\n return head->next;\n }\n head=head->next;\n nodeset.insert(head);\n\n }\n //ListNode *targetNode=head;\n //ListNode *target=head;\n \n for(int i=0;i<10000;i++)\n {\n curNode=curNode->next;\n if(!curNode)\n {\n return nullptr;\n }\n if(curNode==head)\n {\n return head;\n }\n }\n int left=0,right=9999, mid;\n bool found=false;\n while(left<=right)\n {\n mid=(left+right)/2;\n //cout<<\"mid=\"<<mid<<endl;\n if(findTarget(head,mid))\n {\n right=mid-1;\n found=true;\n }\n else\n {\n left=mid+1;\n found=false;\n }\n }\n for(int i=0;i<mid;i++)\n {\n head=head->next;\n }\n if(found)\n { \n return head;\n }\n else\n {\n return head->next;\n }\n \n }\n};",
"memory": "10600"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 bool findTarget(ListNode* head,int pos)\n {\n ListNode* targetNode=head;\n int i=0;\n for(;i<pos;i++)\n {\n targetNode=targetNode->next;\n }\n head=targetNode->next;\n for(i=pos;i<10000+pos;i++)\n {\n if(head==targetNode)\n {\n return true;\n }\n head=head->next;\n }\n return false;\n }\n ListNode *detectCycle(ListNode *head) {\n if(!head)\n {\n return nullptr;\n }\n if(head->next==head)\n {\n return head;\n }\n //int i=0;\n //int len=10000;\n ListNode *curNode=head;\n unordered_set<ListNode*> nodeset{curNode};\n for(int i=0;i<20;i++)\n {\n if(!curNode->next)\n {\n return nullptr;\n }\n if(nodeset.find(curNode->next)!=nodeset.end())\n {\n return curNode->next;\n }\n curNode=curNode->next;\n nodeset.insert(curNode);\n\n }\n curNode=head;\n //ListNode *targetNode=head;\n //ListNode *target=head;\n \n for(int i=0;i<10000;i++)\n {\n curNode=curNode->next;\n if(!curNode)\n {\n return nullptr;\n }\n if(curNode==head)\n {\n return head;\n }\n }\n int left=0,right=9999, mid;\n bool found=false;\n while(left<=right)\n {\n mid=(left+right)/2;\n //cout<<\"mid=\"<<mid<<endl;\n if(findTarget(head,mid))\n {\n right=mid-1;\n found=true;\n }\n else\n {\n left=mid+1;\n found=false;\n }\n }\n for(int i=0;i<mid;i++)\n {\n head=head->next;\n }\n if(found)\n { \n return head;\n }\n else\n {\n return head->next;\n }\n \n }\n};",
"memory": "10600"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if (head == nullptr) {\n return head;\n }\n\n ListNode *slow = head;\n ListNode *fast = head;\n vector<ListNode*> nodeSet;\n \n while (fast->next != nullptr && fast->next->next != nullptr) {\n nodeSet.push_back(slow);\n if (find(nodeSet.begin(), nodeSet.end(), fast->next) != nodeSet.end()) {\n return fast->next;\n }\n else if (find(nodeSet.begin(), nodeSet.end(), fast->next->next) != nodeSet.end()) {\n return fast->next->next;\n }\n slow = slow->next;\n fast = fast->next->next;\n }\n return nullptr;\n }\n};",
"memory": "10700"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 {\nprivate:\n // bool goesToTargetNode(ListNode *f, ListNode *target) {\n // ListNode *currNode = f;\n // while (currNode != nullptr) {\n // currNode = currNode->next;\n // if (currNode == f) {\n // break;\n // }\n // if (currNode == target) {\n // return true;\n // }\n // }\n // return false;\n // }\n\npublic:\n ListNode *detectCycle(ListNode *head) {\n if (head == nullptr) {\n return head;\n }\n\n // Time: O(N^2), Memory: O(1)\n // ==================\n // ListNode *slow = head;\n // ListNode *fast = head;\n // vector<ListNode*> nodeSet;\n // while (fast->next != nullptr && fast->next->next != nullptr) {\n // if (slow != fast && goesToTargetNode(fast, slow)) {\n // return slow;\n // }\n // slow = slow->next;\n // fast = fast->next->next;\n // }\n // return nullptr;\n\n // Memory O(N)\n // ===========\n ListNode *slow = head;\n ListNode *fast = head;\n vector<ListNode*> nodeSet;\n \n while (fast->next != nullptr && fast->next->next != nullptr) {\n nodeSet.push_back(slow);\n if (find(nodeSet.begin(), nodeSet.end(), fast->next) != nodeSet.end()) {\n return fast->next;\n }\n else if (find(nodeSet.begin(), nodeSet.end(), fast->next->next) != nodeSet.end()) {\n return fast->next->next;\n }\n slow = slow->next;\n fast = fast->next->next;\n }\n return nullptr;\n }\n};",
"memory": "10700"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 3 | {
"code": "class Solution {\npublic:\n ListNode* detectCycle(ListNode* head) {\n if (!head or !head->next) return nullptr;\n\n ListNode* s = head, * f = head;\n\n while (s and f and f->next) {\n f = f->next->next;\n s = s->next;\n if (s == f) break;\n }\n if (!s or !f or !f->next) return nullptr;\n\n vector<ListNode*> v;\n ListNode* ptr = head;\n while (1) {\n if (v.size() == 0) v.push_back(ptr);\n else {\n for (int i = 0; i < v.size(); i++) {\n if (ptr == v[i]) return v[i];\n }\n }\n v.push_back(ptr);\n ptr = ptr->next;\n }\n\n return nullptr;\n }\n};",
"memory": "10800"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n vector <ListNode*> arr;\n ListNode*temp=head;\n while(temp!=NULL){\n for(ListNode*node:arr ){\n if(node==temp){\n return node ;\n }\n \n }\n arr.push_back(temp);\n temp=temp->next;\n\n }\n return NULL;\n }\n};",
"memory": "10900"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n \n vector<ListNode*> nodes;\n\n while(head!=NULL){\n if(count(nodes.begin(), nodes.end(), head)>0){\n return head;\n }\n else{\n nodes.push_back(head);\n head = head->next;\n }\n }\n\n ListNode* noCycle;\n\n return noCycle;\n }\n};",
"memory": "10900"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n vector<ListNode* > storage;\n int x = 0;\n\n while (head != nullptr) {\n {\n if(find(storage.begin(), storage.end(), head) != storage.end())\n {\n return head;\n }\n\n storage.push_back(head);\n\n head = head->next;\n } \n }\n return nullptr;\n};\n};",
"memory": "11000"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if(head==NULL || head->next==NULL)\n return NULL;\n vector<ListNode*> visited;\n ListNode *slow=head,*fast=head;\n while(fast->next!=NULL){\n if(visited.empty())\n visited.push_back(fast);\n else{\n if(find(visited.begin(),visited.end(),fast)!=visited.end())\n return fast;\n else\n visited.push_back(fast);\n\n }\n // slow=slow->next;\n fast=fast->next;\n }\n return NULL;\n }\n};",
"memory": "11100"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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:\nListNode *detectCycle(ListNode *head)\n{\n ListNode *slow = head;\n ListNode *fast = head;\n while (fast && fast->next) {\n fast = fast->next->next;\n slow = slow->next;\n if (slow == fast)\n break;\n }\n if (!fast || !fast->next)\n return nullptr;\n if (!head->next)\n return nullptr;\n std::unordered_set<ListNode*> set = get_cycle(fast);\n while (set.find(head) == set.end())\n head = head->next;\n return head;\n}\n\nstd::unordered_set<ListNode*> get_cycle(ListNode *head) {\n std::unordered_set<ListNode*> cycle{head};\n ListNode *trav = head->next;\n while (trav != head) {\n cycle.insert(trav);\n trav = trav->next;\n }\n return cycle;\n}\n};",
"memory": "11200"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if(!head || !head->next) return nullptr;\n vector<pair<ListNode*,int>> v;\n ListNode* tmp = head;\n while(tmp != nullptr){\n for(pair<ListNode*,int> p : v){\n if(p.first == tmp && p.second == tmp->val) return tmp;\n }\n pair<ListNode*,int> pairs;\n pairs.first = tmp;\n pairs.second = tmp->val;\n v.push_back(pairs);\n tmp = tmp->next;\n }\n return nullptr;\n }\n};",
"memory": "11600"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 bool hasCycle(ListNode *head) {\n if(head==NULL ){\n return 0;\n }\n \n ListNode* slow=head;\n ListNode* fast=head->next;\n \n while(slow!=fast){\n if(fast==NULL || fast->next==NULL){\n return 0;\n }\n slow=slow->next;\n fast=fast->next->next;\n \n\n }\n return 1;\n \n }\n ListNode *detectCycle(ListNode *head) {\n if(head==NULL || head->next ==NULL){\n return NULL;\n }\n set<ListNode*>mp;\n if(hasCycle(head) == 1){\n ListNode* temp = head;\n while(true){\n if(mp.find(temp)!=mp.end()){\n return temp;\n }\n else{\n mp.insert(temp);\n temp = temp->next;\n }\n }\n }\n return NULL;\n }\n};",
"memory": "11900"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n ListNode *slow=head,*fast=head;\n set <ListNode*> s;\n while (slow && fast && fast->next){\n slow=slow->next;\n fast=fast->next->next;\n if(slow==fast){\n slow=head;\n while(1){\n if(s.find(slow)!=s.end()){\n return slow;\n }\n s.insert(slow);\n slow=slow->next;\n }\n }\n }\n return NULL;\n }\n};",
"memory": "12000"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 bool isCycleExists(ListNode *head){\n ListNode* fast =head;\n ListNode* slow=head;\n while(fast!=NULL&&fast->next!=NULL){\n fast=fast->next->next;\n slow = slow->next;\n if(fast==slow){\n return true;\n }\n }\n return false;\n }\n ListNode *detectCycle(ListNode *head) {\n bool check = isCycleExists(head);\n if(check==false){\n return NULL;\n }\n set<ListNode*>s;\n ListNode* ptr=head;\n s.insert(ptr);\n while(1){\n ptr=ptr->next;\n if(s.find(ptr)!=s.end()){\n return ptr;\n }\n s.insert(ptr);\n }\n return NULL;\n }\n};",
"memory": "12100"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n\n // TODO: Write your code here\n ListNode* slow = head;\n ListNode* fast = head;\n \n set<ListNode*> hm;\n\n while(fast != nullptr && fast->next != nullptr){\n hm.insert(slow);\n slow = slow->next;\n fast = fast->next;\n if(hm.find(fast) != hm.end()) return fast;\n fast = fast->next;\n if(hm.find(fast) != hm.end()) return fast;\n\n }\n\n return NULL;\n }\n};\n\n\n ",
"memory": "12200"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n\n // TODO: Write your code here\n ListNode* slow = head;\n ListNode* fast = head;\n \n set<ListNode*> hm;\n\n while(fast != nullptr && fast->next != nullptr){\n hm.insert(slow);\n slow = slow->next;\n fast = fast->next;\n if(hm.find(fast) != hm.end()) return fast;\n fast = fast->next;\n if(hm.find(fast) != hm.end()) return fast;\n\n }\n\n return NULL;\n }\n};\n\n\n ",
"memory": "12200"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n set<ListNode*> nodes;\n ListNode *ptr = head;\n while (ptr != NULL && ptr->next != NULL)\n {\n nodes.insert(ptr);\n if (nodes.count(ptr->next))\n return ptr->next;\n ptr = ptr->next;\n }\n return NULL;\n }\n};",
"memory": "12400"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n set<ListNode*> s;\n ListNode* temp = head;\n\n while (temp != NULL) {\n if (s.find(temp) != s.end())\n return temp;\n s.insert(temp);\n temp = temp->next;\n }\n return NULL;\n }\n};",
"memory": "12500"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if(head==nullptr){\n return head;\n }\n ListNode* i=head;\n set<ListNode*> store;\n while(i!=nullptr){\n if(store.find(i)==store.end()){\n store.insert(i);\n }\n else{\n return i;\n }\n i=i->next;\n }\n return nullptr;\n }\n};",
"memory": "12700"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n set<ListNode*>mpp;\n while(head!=NULL){\n if(mpp.find(head)!=mpp.end()){\n return head;\n\n }\n \n mpp.insert(head);\n \n \n \n\n head=head->next;}\n\n \n return nullptr;\n }\n};",
"memory": "12700"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if(head==nullptr){\n return head;\n }\n ListNode* i=head;\n set<ListNode*> store;\n while(i!=nullptr){\n if(store.find(i)==store.end()){\n store.insert(i);\n }\n else{\n return i;\n }\n i=i->next;\n }\n return nullptr;\n }\n};",
"memory": "12800"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n ListNode *temp=head;\n set<ListNode *>s;\n while(temp){\n if(s.find(temp)!=s.end())\n return temp;\n s.insert(temp);\n temp=temp->next;\n }\n return NULL;\n }\n};",
"memory": "12800"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if(head==NULL || head->next==NULL)\n {\n return nullptr;\n }\n unordered_map<int,int>mpp;\n ListNode* slow=head;\n ListNode* fast=head;\n while(fast!=nullptr && fast->next!=nullptr)\n {\n slow=slow->next;\n mpp[slow->val]++;\n fast=fast->next->next;\n if(fast==slow)\n {\n slow=head;\n while(slow!=fast)\n {\n slow=slow->next;\n fast=fast->next;\n }\n return slow;\n }\n }\n return nullptr;\n }\n};",
"memory": "12900"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n unordered_set<ListNode*> s;\n ListNode *p,*q;\n if(head==NULL)\n {\n return NULL;\n }\n p=head;\n q=head;\n do{\n p=p->next;\n q=q->next;\n if(q!=NULL)\n {\n q=q->next;\n }\n }while(p!=NULL && q!=NULL && p!=q);\n if(p==NULL)\n {\n return NULL;\n }\n // int count=0;\n if(p==q)\n {\n ListNode *temp=head;\n while(1)\n {\n if(s.find(temp)!=s.end())\n {\n return temp;\n\n }\n else\n {\n s.insert(temp);\n }\n temp=temp->next;\n }\n }\n return NULL;\n }\n};",
"memory": "13000"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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\n bool checkCycle(ListNode* head){\n ListNode* slow = head;\n ListNode* fast = head;\n\n do{\n slow = slow->next;\n fast = fast->next->next;\n }while(fast != nullptr && fast->next != nullptr && slow != fast);\n\n if(slow == fast) return true;\n\n return false;\n }\n\n ListNode *detectCycle(ListNode *head) {\n \n if(!head || head->next == nullptr) return nullptr;\n\n bool cycle = checkCycle(head);\n\n if(cycle){\n unordered_map<ListNode* , int> mp;\n ListNode* dummy = head;\n\n while(dummy != nullptr){\n if(mp.find(dummy) != mp.end()){\n return dummy;\n }\n mp[dummy] = dummy->val;\n dummy = dummy->next;\n }\n }\n\n return nullptr;\n }\n};",
"memory": "13100"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n unordered_set<ListNode*> st;\n while(head){\n if(st.count(head)) return head;\n st.insert(head);\n head=head->next;\n }\n return NULL;\n }\n};",
"memory": "13200"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if (head == nullptr || head->next == nullptr) return nullptr;\n unordered_set<ListNode*> seen;\n ListNode* n = head;\n while (n->next != nullptr) {\n if (seen.count(n->next)) return n->next;\n seen.insert(n);\n n = n->next;\n }\n return nullptr;\n }\n};",
"memory": "13300"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if (head == nullptr || head->next == nullptr) return nullptr;\n unordered_set<ListNode*> seen;\n ListNode* n = head;\n while (n->next != nullptr) {\n if (seen.count(n->next)) return n->next;\n seen.insert(n);\n n = n->next;\n }\n return nullptr;\n }\n};",
"memory": "13300"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if(!(head)) return 0;\n\n unordered_set<ListNode*> m;\n while(head){\n if(m.count(head)) return head;\n m.insert(head);\n head = head->next;\n }\n return 0;\n }\n};",
"memory": "13400"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n unordered_set<ListNode*> prev_nodes;\n\n ListNode* curr = head;\n while (curr and not prev_nodes.contains(curr)) {\n prev_nodes.insert(curr);\n curr = curr->next;\n }\n\n return (prev_nodes.empty()) ? nullptr : curr;\n }\n};",
"memory": "13400"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n unordered_set<ListNode*> s;\n \n while(head!=NULL){\n if(s.find(head)!=s.end()) return head;\n \n s.insert(head);\n head = head->next;\n }\n return NULL;\n }\n};",
"memory": "13500"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n if (head == nullptr || head->next == nullptr) return nullptr;\n unordered_set<ListNode*> seen;\n ListNode* n = head;\n while (n->next != nullptr) {\n if (seen.count(n->next)) return n->next;\n seen.insert(n);\n n = n->next;\n }\n return nullptr;\n }\n};",
"memory": "13500"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n map<ListNode*,int>mp;\n ListNode* temp=head;\n int i=0;\n while(temp!=NULL){\n if(mp.find(temp)!=mp.end()){\n return temp;\n \n }\n mp[temp]=1;\n temp=temp->next;\n }\n return nullptr;\n }\n};",
"memory": "13600"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n unordered_map<ListNode*,int> mp;\n ListNode* tmp = head;\n while(tmp!=NULL){\n if(mp.find(tmp) != mp.end()){\n mp[tmp]++;\n }\n if(mp[tmp]==2){\n return tmp;\n }\n tmp = tmp->next;\n }\n return NULL;\n\n // if(head==NULL || head->next == NULL){\n // return NULL;\n // }\n \n // ListNode* slow = head;\n // ListNode* fast = head;\n // bool flag = false;\n // while(fast->next != NULL && fast->next->next != NULL){\n // slow = slow->next;\n // fast = fast->next->next;\n \n // if(slow == fast){\n // flag = true;\n // break;\n // }\n // }\n // if(flag==true){\n // slow=head;\n // while(slow!=fast){\n // slow=slow->next;\n // fast=fast->next;\n // }\n // return slow; \n // } else{\n // return NULL; \n // }\n }\n};",
"memory": "13700"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n map<ListNode*,int> list;\n ListNode* temp = head;\n int i =0;\n while(temp != nullptr){\n if(!list.count(temp)){\n list[temp] = i;\n i++;\n }else{\n return temp;\n }\n temp = temp->next;\n }\n return nullptr;\n }\n};",
"memory": "13700"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n // ListNode *fast = head , *second = head, *temp = head;\n // while(pos--){\n // temp=temp->next;\n // }\n // while(fast!=nullptr && fast->next !=nullptr){\n // fast=fast->next->next;\n // second = second->next;\n // if(fast==second){\n // return initialCycle()\n // }\n // }\n\n ListNode *temp = head;\n map<ListNode*,int>mp;\n while(temp){\n if(mp.find(temp)!=mp.end()){\n return temp;\n }\n mp[temp]=1;\n temp=temp->next;\n }\n return nullptr;\n }\n};",
"memory": "13800"
} |
142 | <p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>
<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p>
<p><strong>Do not modify</strong> the linked list.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;" />
<pre>
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> tail connects to node index 1
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;" />
<pre>
<strong>Input:</strong> head = [1,2], pos = 0
<strong>Output:</strong> tail connects to node index 0
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;" />
<pre>
<strong>Input:</strong> head = [1], pos = -1
<strong>Output:</strong> no cycle
<strong>Explanation:</strong> There is no cycle in the linked list.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>
| 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 *detectCycle(ListNode *head) {\n ListNode* temp = head;\n std:unordered_map<ListNode*,int>mapNode;\n while(temp!=NULL){\n if(mapNode.find(temp)!=mapNode.end()){\n return temp;\n }\n mapNode[temp] = 1;\n temp = temp->next;\n } \n return NULL;\n\n }\n};",
"memory": "13800"
} |
1,603 | <p>Given an array <code>nums</code>. We define a running sum of an array as <code>runningSum[i] = sum(nums[0]…nums[i])</code>.</p>
<p>Return the running sum of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [1,3,6,10]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,1,1]
<strong>Output:</strong> [1,2,3,4,5]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,2,10,1]
<strong>Output:</strong> [3,4,6,16,17]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-10^6 <= nums[i] <= 10^6</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> runningSum(vector<int>& nums) {\n vector<int>arr(nums.size());\n arr[0]=nums[0];\n for(int i=1;i<nums.size();i++){\n arr[i]=arr[i-1]+nums[i];\n }\n return arr;\n\n }\n};",
"memory": "10700"
} |
1,603 | <p>Given an array <code>nums</code>. We define a running sum of an array as <code>runningSum[i] = sum(nums[0]…nums[i])</code>.</p>
<p>Return the running sum of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [1,3,6,10]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,1,1]
<strong>Output:</strong> [1,2,3,4,5]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,2,10,1]
<strong>Output:</strong> [3,4,6,16,17]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-10^6 <= nums[i] <= 10^6</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> runningSum(vector<int>& nums) {\n vector<int> runningSum;\n int sum = 0;\n for (int num : nums){\n sum += num;\n runningSum.push_back(sum);\n }\n return runningSum;\n }\n};",
"memory": "10700"
} |
1,603 | <p>Given an array <code>nums</code>. We define a running sum of an array as <code>runningSum[i] = sum(nums[0]…nums[i])</code>.</p>
<p>Return the running sum of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [1,3,6,10]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,1,1]
<strong>Output:</strong> [1,2,3,4,5]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,2,10,1]
<strong>Output:</strong> [3,4,6,16,17]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-10^6 <= nums[i] <= 10^6</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> runningSum(vector<int>& nums) {\n int n=nums.size();\n vector<int> pre(n);\n pre[0]=nums[0];\n for(int i=1;i<n;i++){\n pre[i]=pre[i-1]+nums[i];\n }\n\n return pre;\n } \n};",
"memory": "10800"
} |
1,603 | <p>Given an array <code>nums</code>. We define a running sum of an array as <code>runningSum[i] = sum(nums[0]…nums[i])</code>.</p>
<p>Return the running sum of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [1,3,6,10]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,1,1]
<strong>Output:</strong> [1,2,3,4,5]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,2,10,1]
<strong>Output:</strong> [3,4,6,16,17]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-10^6 <= nums[i] <= 10^6</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> runningSum(vector<int>& nums) {\n vector<int> result;\n result.reserve(nums.size());\n\n result.push_back(nums[0]);\n for (int i = 1; i < nums.size(); ++i) {\n result.push_back(result[i - 1] + nums[i]);\n }\n\n return result;\n }\n};",
"memory": "10800"
} |
1,603 | <p>Given an array <code>nums</code>. We define a running sum of an array as <code>runningSum[i] = sum(nums[0]…nums[i])</code>.</p>
<p>Return the running sum of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [1,3,6,10]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,1,1]
<strong>Output:</strong> [1,2,3,4,5]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,2,10,1]
<strong>Output:</strong> [3,4,6,16,17]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-10^6 <= nums[i] <= 10^6</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> runningSum(vector<int>& nums) {\n int n = nums.size();\n for(int i=1;i<n;i++){\n nums[i] = nums[i]+nums[i-1];\n }\n return nums;\n }\n};",
"memory": "10900"
} |
1,603 | <p>Given an array <code>nums</code>. We define a running sum of an array as <code>runningSum[i] = sum(nums[0]…nums[i])</code>.</p>
<p>Return the running sum of <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [1,3,6,10]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,1,1]
<strong>Output:</strong> [1,2,3,4,5]
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,2,10,1]
<strong>Output:</strong> [3,4,6,16,17]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-10^6 <= nums[i] <= 10^6</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> runningSum(vector<int>& nums) {\n // vector<int>arr(nums.size());\n // arr[0]=nums[0];\n // for(int i=1;i<nums.size();i++){\n // arr[i]=arr[i-1]+nums[i];\n // }\n // return arr;\n for(int i=1;i<nums.size();i++){\n nums[i]=nums[i]+nums[i-1];\n }\n return nums;\n\n }\n};",
"memory": "10900"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n sort(arr.begin(), arr.end());\r\n map<int,int> mp;\r\n int prev=arr[0], cnt=0, n=arr.size(), distinct=0;\r\n for(int i=0;i<n;i++)\r\n {\r\n if(arr[i] == prev)\r\n cnt++;\r\n else\r\n {\r\n distinct++;\r\n mp[cnt]++;\r\n cnt=1;\r\n prev=arr[i];\r\n }\r\n }\r\n mp[cnt]++;\r\n distinct++;\r\n int removed=0;\r\n for(auto it:mp)\r\n {\r\n int freq = it.first;\r\n int ele = it.second;\r\n int totalEle = freq*ele;\r\n if(k >= totalEle)\r\n {\r\n k-=totalEle;\r\n removed+=ele;\r\n }\r\n else\r\n {\r\n removed+=(k/freq);\r\n k=0;\r\n }\r\n if(k == 0)\r\n break;\r\n }\r\n return distinct-removed;\r\n }\r\n};",
"memory": "47000"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "// class Solution {\r\n// public:\r\n// int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n// // sort(arr.begin(), arr.end());\r\n// vector<int> countArray(5000, 0);\r\n// unordered_map<int,int> countMap;\r\n// for(int i=0; i<arr.size(); i++) countMap[arr[i]]++;\r\n// for (auto it=countMap.begin(); it!=countMap.end(); ++it) {\r\n// cout<<it->first<<\" \"<<it->second<<endl;\r\n// countArray[it->second]++;\r\n// }\r\n// int count = 1,ans=0;\r\n// // for(int i=1; i<arr.size(); i++) {\r\n// // if(arr[i] != arr[i-1]) {\r\n// // countArray[count]++;\r\n// // count = 1;\r\n// // }\r\n// // else\r\n// // count++;\r\n// // }\r\n// // countArray[count]++;\r\n// for(int i=1; i<5000; i++) {\r\n// if(countArray[i]*i <= k) {\r\n// k-=countArray[i]*i;\r\n// }else{\r\n// int val=(i*countArray[i]-k)/i;\r\n// if((i*countArray[i]-k)%i != 0) val++;\r\n// ans+=val;\r\n// k=0;\r\n// }\r\n// }\r\n// return ans;\r\n// }\r\n// };\r\n\r\nclass Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\r\n sort(arr.begin(), arr.end());\r\n vector<int> countArray(5000, 0);\r\n int count = 1,ans=0;\r\n for(int i=1; i<arr.size(); i++) {\r\n if(arr[i] != arr[i-1]) {\r\n countArray[count]++;\r\n count = 1;\r\n }\r\n else\r\n count++;\r\n }\r\n countArray[count]++;\r\n for(int i=1; i<5000; i++) {\r\n if(countArray[i]*i <= k) {\r\n k-=countArray[i]*i;\r\n }else{\r\n int val=(i*countArray[i]-k)/i;\r\n if((i*countArray[i]-k)%i != 0) val++;\r\n ans+=val;\r\n k=0;\r\n }\r\n }\r\n return ans;\r\n }\r\n};",
"memory": "47300"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n \r\n int fr[200005] = {0};\r\n sort(arr.begin(), arr.end());\r\n int nr = -1, prev = -1;\r\n for (auto x : arr) {\r\n if (prev != x) {\r\n nr++;\r\n prev = x;\r\n }\r\n fr[nr] ++;\r\n }\r\n\r\n sort(fr, fr + nr + 1);\r\n int res = 0, sum = 0, idx = 0;\r\n while (sum + fr[idx] <= k && idx <= nr) {\r\n sum += fr[idx];\r\n idx++;\r\n res++;\r\n }\r\n return nr - res + 1;\r\n }\r\n};",
"memory": "47700"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n \r\n int fr[200005] = {0};\r\n sort(arr.begin(), arr.end());\r\n int nr = -1, prev = -1;\r\n for (auto x : arr) {\r\n if (prev != x) {\r\n nr++;\r\n prev = x;\r\n }\r\n fr[nr] ++;\r\n }\r\n\r\n sort(fr, fr + nr + 1);\r\n int res = 0, sum = 0, idx = 0;\r\n while (sum + fr[idx] <= k && idx <= nr) {\r\n sum += fr[idx];\r\n idx++;\r\n res++;\r\n }\r\n return nr - res + 1;\r\n }\r\n};",
"memory": "47900"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n \r\n int fr[200005] = {0};\r\n sort(arr.begin(), arr.end());\r\n int nr = -1, prev = -1;\r\n for (auto x : arr) {\r\n if (prev != x) {\r\n nr++;\r\n prev = x;\r\n }\r\n fr[nr] ++;\r\n }\r\n\r\n sort(fr, fr + nr + 1);\r\n int res = 0, sum = 0, idx = 0;\r\n while (sum + fr[idx] <= k && idx <= nr) {\r\n sum += fr[idx];\r\n idx++;\r\n res++;\r\n }\r\n return nr - res + 1;\r\n }\r\n};",
"memory": "47900"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n vector<int> freq;\r\n sort(arr.begin(),arr.end());\r\n freq.push_back(1);\r\n int x = 0;\r\n int n = arr.size();\r\n for(int i=0;i<n-1;i++){\r\n if(arr[i+1] == arr[i]){\r\n freq[x]++;\r\n }\r\n else{\r\n freq.push_back(1);\r\n x++;\r\n }\r\n }\r\n sort(freq.begin(),freq.end());\r\n int i = 0;\r\n while(i<freq.size() && k >= freq[i]){\r\n k = k - freq[i];\r\n i++;\r\n }\r\n int ans = freq.size() - i;\r\n return ans;\r\n }\r\n};",
"memory": "49400"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n sort(arr.begin(), arr.end());\r\n priority_queue<int, vector<int>, greater<int>> pq;\r\n int count = 1;\r\n for(int i = 1 ; i < arr.size() ; i++){\r\n if(arr[i] == arr[i - 1]) count++;\r\n else{\r\n pq.push(count);\r\n count = 1;\r\n }\r\n }\r\n pq.push(count);\r\n while(k >= pq.top()){\r\n k -= pq.top();\r\n pq.pop();\r\n }\r\n return pq.size();\r\n }\r\n};",
"memory": "49500"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n vector<int> freq;\r\n sort(arr.begin(),arr.end());\r\n freq.push_back(1);\r\n int x = 0;\r\n int n = arr.size();\r\n for(int i=0;i<n-1;i++){\r\n if(arr[i+1] == arr[i]){\r\n freq[x]++;\r\n }\r\n else{\r\n freq.push_back(1);\r\n x++;\r\n }\r\n }\r\n sort(freq.begin(),freq.end());\r\n int i = 0;\r\n while(i<freq.size() && k >= freq[i]){\r\n k = k - freq[i];\r\n i++;\r\n }\r\n int ans = freq.size() - i;\r\n return ans;\r\n }\r\n};",
"memory": "49600"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n priority_queue<int, vector<int>, greater<int>> pq;\r\n sort(arr.begin(),arr.end());\r\n int count=1;\r\n for(int i=1;i<arr.size();i++)\r\n {\r\n if(arr[i-1]==arr[i])\r\n {\r\n count++;\r\n }\r\n else\r\n {\r\n // cout<<count<<endl;\r\n pq.push(count);\r\n count=1;\r\n }\r\n\r\n }\r\n // cout<<count<<endl;\r\n pq.push(count);\r\n int ans=pq.size();\r\n // cout<<ans<<endl;\r\n while(k>0)\r\n {\r\n // cout<<k<<\" \"<<pq.top()<<endl;\r\n if(k<pq.top())\r\n break;\r\n k=k-pq.top();\r\n pq.pop();\r\n ans--;\r\n }\r\n return ans;\r\n }\r\n};",
"memory": "49700"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n // Sort the array to group similar elements together\r\n sort(arr.begin(), arr.end());\r\n\r\n // Vector to store pairs of (element, frequency)\r\n vector<pair<int, int>> v;\r\n\r\n // Calculate frequencies\r\n for (int i = 0; i < arr.size();) {\r\n int count = 1;\r\n int currentElement = arr[i];\r\n i++;\r\n while (i < arr.size() && arr[i] == currentElement) {\r\n count++;\r\n i++;\r\n }\r\n v.push_back(make_pair(currentElement, count));\r\n }\r\n\r\n // Sort by frequency (second element of the pair)\r\n sort(v.begin(), v.end(), [](const auto& a, const auto& b) {\r\n return a.second < b.second;\r\n });\r\n\r\n // Remove elements by frequency until `k` deletions are made\r\n for (int i = 0; i < v.size() && k > 0; ++i) {\r\n if (k >= v[i].second) {\r\n k -= v[i].second;\r\n v[i].second = 0; // Remove this element entirely\r\n } else {\r\n v[i].second -= k;\r\n k = 0; // No more deletions possible\r\n }\r\n }\r\n\r\n // Count unique elements left\r\n int uniqueCount = 0;\r\n for (const auto& p : v) {\r\n if (p.second > 0) {\r\n uniqueCount++;\r\n }\r\n }\r\n\r\n return uniqueCount;\r\n }\r\n};\r\n",
"memory": "52000"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n sort(arr.begin(), arr.end());\r\n vector<pair<int, int>> v;\r\n for (int i = 0; i < arr.size();) {\r\n int count = 1;\r\n int currentElement = arr[i];\r\n i++;\r\n while (i < arr.size() && arr[i] == currentElement) {\r\n count++;\r\n i++;\r\n }\r\n v.push_back(make_pair(currentElement, count));\r\n }\r\n sort(v.begin(), v.end(), [](const auto& a, const auto& b) {\r\n return a.second < b.second;\r\n });\r\n for (int i = 0; i < v.size() && k > 0; ++i) {\r\n if (k >= v[i].second) {\r\n k -= v[i].second;\r\n v[i].second = 0; \r\n } else {\r\n v[i].second -= k;\r\n k = 0; \r\n }\r\n }\r\n int uniqueCount = 0;\r\n for (const auto& p : v) {\r\n if (p.second > 0) {\r\n uniqueCount++;\r\n }\r\n }\r\n\r\n return uniqueCount;\r\n }\r\n};\r\n",
"memory": "52100"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n sort(arr.begin(),arr.end());\r\n vector <pair <int,int>> a;\r\n for (int i=0;i<arr.size();i++) {\r\n a.push_back(make_pair(1,arr[i]));\r\n if (i>0 && arr[i]==arr[i-1]) a[i].first=1+a[i-1].first;\r\n }\r\n\r\n for (int i=a.size()-2;i>=0;i--)\r\n if (a[i].second==a[i+1].second) a[i].first=a[i+1].first;\r\n\r\n sort(a.begin(),a.end());\r\n\r\n for (int i=0;i<a.size();i++)\r\n cout<<a[i].first<<\" \"<<a[i].second<<endl;\r\n\r\n int cnt=0;\r\n for (int j=k;j<a.size();j++)\r\n if (j==k || a[j].second!=a[j-1].second)\r\n cnt++;\r\n return cnt;\r\n }\r\n};",
"memory": "54300"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\r\nclass Solution {\r\npublic:\r\n using int2=pair<int, int>;\r\n struct cmp{\r\n bool operator()(int2& x, int2& y){\r\n return x.second>y.second;\r\n }\r\n };\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n sort(arr.begin(), arr.end());\r\n int prev=0;\r\n vector<int2> freq;\r\n for(int x: arr){\r\n if (x!=prev) {\r\n freq.emplace_back(x, 1);\r\n prev=x;\r\n }\r\n else freq.back().second++;\r\n }\r\n priority_queue<int2, vector<int2>, cmp> pq(freq.begin(), freq.end());\r\n while(k>0 && !pq.empty()){\r\n auto [x, f]=pq.top();//the min freq[x] on the top\r\n pq.pop();\r\n k-=f;\r\n }\r\n int sz=pq.size();\r\n // cout<<sz<<endl;\r\n return k==0?sz:sz+1;\r\n }\r\n};",
"memory": "54400"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n // lame sauce\r\n ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);\r\n\r\n // count unique int frequencies\r\n unordered_map<int, int> F;\r\n int maxF = 0;\r\n for (int &x: arr) {\r\n F[x]++;\r\n maxF = max(maxF, F[x]);\r\n }\r\n // intialize num of unique elements\r\n int n = F.size();\r\n // count frequencies\r\n vector<int> fF(++maxF, 0);\r\n for (auto &[k, f]: F) {\r\n fF[f]++;\r\n }\r\n // look at frequencies from smallest to largest\r\n for(int i = 1; i < maxF; i++) {\r\n if (!fF[i]) continue;\r\n // subtract from k\r\n if(fF[i] * i <= k) {\r\n k -= fF[i] * i;\r\n n -= fF[i];\r\n }\r\n // if k >= 0, decrease total unique elements\r\n else {\r\n while (k >= i && fF[i]) {\r\n k -= i;\r\n fF[i]--;\r\n n -= 1;\r\n }\r\n break;\r\n } \r\n }\r\n\r\n return n;\r\n }\r\n};",
"memory": "63000"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> numToOcc;\r\n map<int, int> occToUct;\r\n for (int num : arr) {\r\n numToOcc[num]++;\r\n }\r\n for (const auto [num, occ] : numToOcc) {\r\n occToUct[occ]++;\r\n }\r\n auto it = occToUct.begin();\r\n while (it != occToUct.end()) {\r\n int occ = it->first;\r\n if (occ <= k) {\r\n k -= occ;\r\n it->second--;\r\n if (it->second == 0) {\r\n it = occToUct.erase(it);\r\n }\r\n } else {\r\n break;\r\n }\r\n }\r\n int uctTotal = 0;\r\n for (const auto [occ, uct] : occToUct) {\r\n uctTotal += uct;\r\n }\r\n return uctTotal;\r\n }\r\n};",
"memory": "63600"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n void print_vi(const vector<int> &vi) {\r\n cout << \"{ \";\r\n for (int i : vi) {\r\n cout << i << \", \";\r\n }\r\n cout << '}' << endl;\r\n }\r\n void print_umii(const unordered_map<int, int> &umii) {\r\n cout << \"{ \";\r\n for (const auto [a, b] : umii) {\r\n cout << '(' << a << ':' << b << \") \";\r\n }\r\n cout << '}' << endl;\r\n }\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> numToOcc;\r\n int maxOcc = 0;\r\n for (int num : arr) {\r\n numToOcc[num]++;\r\n maxOcc = max(maxOcc, numToOcc[num]);\r\n }\r\n print_umii(numToOcc);\r\n vector<int> occToUct(maxOcc + 1);\r\n for (const auto [num, occ] : numToOcc) {\r\n occToUct[occ]++;\r\n }\r\n print_vi(occToUct);\r\n int occ = 1;\r\n while (occ < occToUct.size()) {\r\n if (occToUct[occ] == 0) {\r\n occ++;\r\n continue;\r\n }\r\n if (occ > k) break;\r\n k -= occ;\r\n occToUct[occ]--;\r\n }\r\n print_vi(occToUct);\r\n int uctTotal = 0;\r\n for (int uct : occToUct) {\r\n uctTotal += uct;\r\n }\r\n return uctTotal;\r\n }\r\n};",
"memory": "63600"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n std::unordered_map<int,int> num_to_count_map;\r\n for (const int num: arr) num_to_count_map[num] += 1;\r\n\r\n std::map<int, int> frequency_to_numbers_count_map;\r\n for (const auto &[num,count]: num_to_count_map) frequency_to_numbers_count_map[count] += 1;\r\n\r\n int num_unique_ints = 0;\r\n int last_count_key = 0;\r\n int rem_elements = 0;\r\n for (const auto& [count, total_nums] : frequency_to_numbers_count_map) {\r\n ++last_count_key;\r\n if (k - num_unique_ints <= total_nums*count) {\r\n rem_elements = std::ceil((total_nums*count - (k - num_unique_ints))/static_cast<double>(count));\r\n break;\r\n }\r\n num_unique_ints += total_nums*count;\r\n }\r\n\r\n int curr_count_key = 0;\r\n for (const auto& [count, total_nums] : frequency_to_numbers_count_map) {\r\n if (++curr_count_key <= last_count_key) continue;\r\n rem_elements += total_nums;\r\n }\r\n \r\n return rem_elements;\r\n }\r\n};",
"memory": "63700"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n void print_vi(const vector<int> &vi) {\r\n cout << \"{ \";\r\n for (int i : vi) {\r\n cout << i << \", \";\r\n }\r\n cout << '}' << endl;\r\n }\r\n void print_umii(const unordered_map<int, int> &umii) {\r\n cout << \"{ \";\r\n for (const auto [a, b] : umii) {\r\n cout << '(' << a << ':' << b << \") \";\r\n }\r\n cout << '}' << endl;\r\n }\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> numToOcc;\r\n int maxOcc = 0;\r\n for (int num : arr) {\r\n numToOcc[num]++;\r\n maxOcc = max(maxOcc, numToOcc[num]);\r\n }\r\n print_umii(numToOcc);\r\n vector<int> occToUct(maxOcc + 1);\r\n for (const auto [num, occ] : numToOcc) {\r\n occToUct[occ]++;\r\n }\r\n print_vi(occToUct);\r\n int occ = 1;\r\n while (occ < occToUct.size()) {\r\n if (occToUct[occ] == 0) {\r\n occ++;\r\n continue;\r\n }\r\n if (occ > k) break;\r\n k -= occ;\r\n occToUct[occ]--;\r\n }\r\n print_vi(occToUct);\r\n int uctTotal = 0;\r\n for (int uct : occToUct) {\r\n uctTotal += uct;\r\n }\r\n return uctTotal;\r\n }\r\n};",
"memory": "63800"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n void print_vi(const vector<int> &vi) {\r\n cout << \"{ \";\r\n for (int i : vi) {\r\n cout << i << \", \";\r\n }\r\n cout << '}' << endl;\r\n }\r\n void print_umii(const unordered_map<int, int> &umii) {\r\n cout << \"{ \";\r\n for (const auto [a, b] : umii) {\r\n cout << '(' << a << ':' << b << \") \";\r\n }\r\n cout << '}' << endl;\r\n }\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> numToOcc;\r\n int maxOcc = 0;\r\n for (int num : arr) {\r\n numToOcc[num]++;\r\n maxOcc = max(maxOcc, numToOcc[num]);\r\n }\r\n //print_umii(numToOcc);\r\n vector<int> occToUct(maxOcc + 1);\r\n for (const auto [num, occ] : numToOcc) {\r\n occToUct[occ]++;\r\n }\r\n //print_vi(occToUct);\r\n int occ = 1;\r\n while (occ < occToUct.size()) {\r\n if (occToUct[occ] == 0) {\r\n occ++;\r\n continue;\r\n }\r\n if (occ > k) break;\r\n k -= occ;\r\n occToUct[occ]--;\r\n }\r\n ///print_vi(occToUct);\r\n int uctTotal = 0;\r\n for (int uct : occToUct) {\r\n uctTotal += uct;\r\n }\r\n return uctTotal;\r\n }\r\n};",
"memory": "63800"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n void print_vi(const vector<int> &vi) {\r\n cout << \"{ \";\r\n for (int i : vi) {\r\n cout << i << \", \";\r\n }\r\n cout << '}' << endl;\r\n }\r\n void print_umii(const unordered_map<int, int> &umii) {\r\n cout << \"{ \";\r\n for (const auto [a, b] : umii) {\r\n cout << '(' << a << ':' << b << \") \";\r\n }\r\n cout << '}' << endl;\r\n }\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> numToOcc;\r\n int maxOcc = 0;\r\n for (int num : arr) {\r\n numToOcc[num]++;\r\n maxOcc = max(maxOcc, numToOcc[num]);\r\n }\r\n print_umii(numToOcc);\r\n vector<int> occToUct(maxOcc + 1);\r\n for (const auto [num, occ] : numToOcc) {\r\n occToUct[occ]++;\r\n }\r\n print_vi(occToUct);\r\n for (int occ = 1; occ < occToUct.size(); /* */) {\r\n if (occToUct[occ] == 0) {\r\n occ++;\r\n continue;\r\n }\r\n if (occ <= k) {\r\n k -= occ;\r\n occToUct[occ]--;\r\n if (occToUct[occ] == 0) {\r\n occ++;\r\n }\r\n } else {\r\n break;\r\n }\r\n }\r\n print_vi(occToUct);\r\n int uctTotal = 0;\r\n for (int uct : occToUct) {\r\n uctTotal += uct;\r\n }\r\n return uctTotal;\r\n }\r\n};",
"memory": "63900"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> um;\r\n map<int, int> m;\r\n \r\n for(int& a: arr) um[a]++;\r\n int uniq = um.size();\r\n\r\n for(auto& i: um) m[i.second]++;\r\n\r\n for(map<int, int>::iterator it=m.begin(); it!=m.end(); it++){\r\n int a = it->first, b = (*it).second;\r\n // cout<<k<<\" \"<<a*b<<\" \"; 4 1 \r\n if(k > a*b){\r\n k -= a*b;\r\n uniq -= b;\r\n } else{\r\n // cout<<uniq<<\" \"<<(b - (a*b-k)/a - (a*b-k)%a)<<endl;\r\n // cout<<uniq<<\" \"<<(b - ceil(double(a*b-k)/a));\r\n return uniq - (b - ceil(double(a*b-k)/a));\r\n }\r\n }\r\n return uniq;\r\n }\r\n};",
"memory": "64000"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\r\n int n=arr.size();\r\n unordered_map<int,int>mp;\r\n for(auto it : arr){\r\n mp[it]++;\r\n }\r\n vector<int>temp(n+1,0);\r\n for(auto it : mp)temp[it.second]++;\r\n int totalEle=mp.size();\r\n for(int i=1;i<=n;i++){\r\n int rem=min(k/i,temp[i]);\r\n totalEle-=rem;\r\n k=k-(rem*i);\r\n if(k<i)break;\r\n }\r\n return totalEle;\r\n }\r\n};",
"memory": "64500"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int> mp;\r\n int unique = 0;\r\n for(int i: arr){\r\n if(!mp.count(i)){\r\n unique++;\r\n mp[i]=1;\r\n }\r\n else mp[i]++;\r\n }\r\n vector<int> mp2(k+1,0);\r\n for(auto i:mp){\r\n if(i.second<=k){\r\n mp2[i.second]++;\r\n }\r\n }\r\n\r\n int i = 1;\r\n while(i<=k){\r\n int n = mp2[i];\r\n if(n*i > k){\r\n unique -= (k/i);\r\n return unique;\r\n }\r\n k-=n*i;\r\n unique-=n;\r\n i++;\r\n }\r\n return unique;\r\n\r\n\r\n }\r\n};",
"memory": "64700"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n std::unordered_map<int, int> counter;\r\n\r\n for (auto const& num: arr){\r\n counter[num]++;\r\n }\r\n\r\n std::vector<int> scounter;\r\n scounter.reserve(counter.size());\r\n for (auto const& [num, count]:counter){\r\n scounter.emplace_back(count);\r\n }\r\n std::sort(scounter.begin(), scounter.end());\r\n\r\n int cumu{}, ans{};\r\n for (int i{}; i<scounter.size(); i++){\r\n cumu+=scounter[i];\r\n if (cumu>k){\r\n ans=scounter.size()-i;\r\n break;\r\n }\r\n }\r\n\r\n return ans;\r\n }\r\n};",
"memory": "65000"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n std::unordered_map<int, int> counter;\r\n\r\n for (auto const& num: arr){\r\n counter[num]++;\r\n }\r\n\r\n std::vector<int> scounter;\r\n scounter.reserve(counter.size());\r\n for (auto const& [num, count]:counter){\r\n scounter.emplace_back(count);\r\n }\r\n std::sort(scounter.begin(), scounter.end());\r\n\r\n int cumu{}, ans{};\r\n for (int i{}; i<scounter.size(); i++){\r\n cumu+=scounter[i];\r\n if (cumu>k){\r\n ans=scounter.size()-i;\r\n break;\r\n }\r\n }\r\n\r\n return ans;\r\n }\r\n};",
"memory": "65099"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n\r\n ios_base::sync_with_stdio(false);\r\n cin.tie(NULL);\r\n cout.tie(NULL);\r\n\r\n unordered_map<int, int> factor;\r\n int value;\r\n priority_queue<int> min_heap;\r\n int input = 0;\r\n\r\n for (int n : arr)\r\n factor[n]++;\r\n\r\n for (auto i : factor) {\r\n min_heap.push(i.second * -1);\r\n }\r\n\r\n value = factor.size();\r\n\r\n while (k) {\r\n\r\n if (abs(min_heap.top()) > 1) {\r\n\r\n input = abs(min_heap.top());\r\n min_heap.pop();\r\n --input;\r\n min_heap.push(input * -1);\r\n\r\n } else {\r\n value--;\r\n min_heap.pop();\r\n }\r\n k--;\r\n }\r\n return value;\r\n }\r\n};",
"memory": "65600"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\r\n unordered_map<int,int> mp;\r\n for(int i=0;i<arr.size();i++)\r\n mp[arr[i]]++;\r\n vector<int> result;\r\n for(auto val:mp)\r\n result.push_back(val.second);\r\n sort(result.begin(),result.end());\r\n int count=0;\r\n for(auto val:result)\r\n if(k-val>=0)\r\n {\r\n k-=val;\r\n count++;\r\n }\r\n else break;\r\n return result.size()-count;\r\n }\r\n};",
"memory": "65700"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n \r\n unordered_map<int,int> mp;\r\n\r\n for(int x : arr) mp[x]++;\r\n\r\n vector<pair<int,int>> tmp(mp.begin(), mp.end());\r\n\r\n sort(tmp.begin(), tmp.end(), [&](pair<int,int> p1, pair<int,int> p2){\r\n return p1.second < p2.second;\r\n });\r\n\r\n int z = tmp.size();\r\n int cnt = 0;\r\n\r\n for(int i=0;i<z;i++){\r\n\r\n if(tmp[i].second > k) break;\r\n\r\n cnt++;\r\n k -= tmp[i].second;\r\n\r\n }\r\n\r\n return z - cnt;\r\n\r\n }\r\n};",
"memory": "66000"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> mp;\r\n for(int num: arr) {\r\n mp[num]++;\r\n }\r\n priority_queue<int, vector<int>, greater<int>> pq;\r\n for(auto it: mp) {\r\n pq.push(it.second);\r\n }\r\n int s = pq.size();\r\n while(!pq.empty()) {\r\n k-= pq.top();\r\n if(k < 0)\r\n return s;\r\n s-=1;\r\n pq.pop();\r\n }\r\n return s;\r\n }\r\n};",
"memory": "66100"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int> count;\r\n for (int i = 0; i < arr.size(); i++) {\r\n count[arr[i]]++;\r\n }\r\n\r\n priority_queue<int, vector<int>, greater<int>> pq;\r\n for(auto i : count) pq.push(i.second);\r\n\r\n while(pq.size() && k > 0){\r\n int freq = pq.top();\r\n if(k < freq) {\r\n k = 0;\r\n \r\n }\r\n else{ \r\n k -= freq;\r\n pq.pop();\r\n }\r\n }\r\n\r\n return pq.size();\r\n }\r\n};",
"memory": "66200"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 0 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n int n = arr.size();\r\n priority_queue<int, vector<int>, greater<int>> pq;\r\n unordered_map<int,int> m;\r\n for(auto x: arr){\r\n m[x]++;\r\n }\r\n\r\n for(auto x: m){\r\n pq.push(x.second);\r\n }\r\n\r\n int count = 0;\r\n\r\n while(!pq.empty() and k){\r\n int top = pq.top();\r\n pq.pop();\r\n if(k >= top){\r\n k-=top;\r\n }else{\r\n k = 0;\r\n top -=k;\r\n pq.push(top);\r\n }\r\n }\r\n\r\n return pq.size();\r\n }\r\n};",
"memory": "66200"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 1 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int> cnt;\r\n\r\n for(auto el:arr){\r\n cnt[el]++;\r\n }\r\n\r\n priority_queue<int,vector<int>,greater<int>> pq;\r\n\r\n for(auto el:cnt){\r\n pq.push(el.second);\r\n }\r\n\r\n while(k>0){\r\n if(pq.top()==1){\r\n pq.pop();\r\n }\r\n else{\r\n int n=pq.top();\r\n pq.pop();\r\n n--;\r\n pq.push(n);\r\n }\r\n k--;\r\n\r\n }\r\n\r\n return pq.size();\r\n }\r\n};",
"memory": "66300"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 1 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> counts;\r\n\r\n for(int i = 0; i < arr.size(); i++)\r\n counts[arr[i]]++;\r\n\r\n priority_queue<int, vector<int>, greater<int>> repeats;\r\n for(auto& pair : counts)\r\n repeats.push(pair.second);\r\n\r\n while(arr.size() && k) {\r\n int amount = repeats.top();\r\n \r\n if (amount <= k) {\r\n k -= amount;\r\n repeats.pop();\r\n } else {\r\n break;\r\n }\r\n }\r\n\r\n return repeats.size();\r\n }\r\n};",
"memory": "66300"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 1 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int>mp;\r\n for(auto &num:arr)\r\n {\r\n mp[num]++;\r\n }\r\n vector<int>result;\r\n for(auto &a:mp)\r\n {\r\n result.push_back(a.second);\r\n }\r\n sort(result.begin(),result.end());\r\n\r\n for(int i=0;i<result.size();i++)\r\n {\r\n k-=result[i];\r\n if(k<0)\r\n return( result.size()-i);\r\n }\r\n return 0;\r\n }\r\n};",
"memory": "66400"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 1 | {
"code": "\nclass Solution {\npublic:\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\n int n = arr.size();\n\t\tunordered_map<int, int> frMap;\n\t\tfor (int &x: arr) {\n\t\t\tfrMap[x]++;\n\t\t}\n\t\tvector<int> fr;\n\t\tfor (auto &p: frMap) {\n\t\t\tfr.push_back(p.second);\n\t\t} \n\t\tsort(fr.begin(), fr.end());\n\t\tint cnt = 0, del = 0;\n\t\tfor (int &x: fr) {\n\t\t\tif (del + x <= k) {\n\t\t\t\tcnt++;\n\t\t\t\tdel += x;\n\t\t\t} else {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn (int) fr.size() - cnt;\n }\n};\n\n",
"memory": "66400"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 1 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> counts;\r\n vector<int> heap;\r\n for (auto n : arr)\r\n ++counts[n];\r\n for (auto &p: counts)\r\n heap.push_back(p.second);\r\n make_heap(begin(heap), end(heap), greater<int>());\r\n while (k > 0) {\r\n k -= heap.front();\r\n pop_heap(begin(heap), end(heap), greater<int>()); \r\n if (k >= 0)\r\n heap.pop_back();\r\n }\r\n return heap.size();\r\n }\r\n};",
"memory": "66500"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 1 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> frequencyMap;\r\n\r\n int result = 0;\r\n\r\n vector<int> frequencyArray;\r\n for(int num : arr){\r\n frequencyMap[num]++;\r\n }\r\n\r\n for(auto itr : frequencyMap){\r\n frequencyArray.push_back(itr.second);\r\n }\r\n\r\n sort(frequencyArray.begin(), frequencyArray.end());\r\n int elementsRemoved = 0;\r\n\r\n for(int i = 0; i < frequencyArray.size(); i++){\r\n elementsRemoved += frequencyArray[i];\r\n\r\n if(elementsRemoved > k){\r\n return frequencyArray.size() - i;\r\n }\r\n }\r\n return result;\r\n }\r\n};",
"memory": "66500"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 2 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n // Greedy approach, remove from increasing count order: 1, 2, 3\r\n unordered_map<int, int> countMap;\r\n for(int& num : arr)\r\n {\r\n countMap[num]++;\r\n }\r\n\r\n int distinct = 0;\r\n priority_queue<int, vector<int>, greater<int>> minHeap;\r\n for(auto [num, count] : countMap)\r\n {\r\n minHeap.push(count);\r\n distinct++;\r\n }\r\n\r\n while(!minHeap.empty() && k > 0)\r\n {\r\n k -= minHeap.top();\r\n minHeap.pop();\r\n \r\n if (k >= 0)\r\n {\r\n distinct--;\r\n }\r\n }\r\n\r\n return distinct;\r\n }\r\n};",
"memory": "66600"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 2 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n std::unordered_map<int, int> num_map;\r\n for (int v: arr) {\r\n if (num_map.contains(v)) {\r\n num_map[v]++;\r\n } else {\r\n num_map.insert({v, 1});\r\n }\r\n }\r\n\r\n int num_nums = num_map.size();\r\n\r\n std::deque<int> occurs;\r\n for (auto it = num_map.begin(); it != num_map.end(); it++) {\r\n occurs.push_back((*it).second);\r\n }\r\n std::sort(occurs.begin(), occurs.end());\r\n\r\n while (k > 0 && !occurs.empty()) {\r\n if (occurs.front() > k) break;\r\n k -= occurs.front();\r\n occurs.pop_front();\r\n num_nums--;\r\n }\r\n\r\n return num_nums;\r\n\r\n }\r\n};",
"memory": "66700"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 2 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int, int> counts;\r\n for (int x : arr) counts[x] += 1;\r\n priority_queue<int, vector<int>> pq;\r\n for (auto p : counts) {\r\n if (pq.size()<k) pq.push(p.second);\r\n else if (pq.size()>0 and p.second < pq.top()) {\r\n pq.pop();\r\n pq.push(p.second);\r\n }\r\n }\r\n stack<int> st;\r\n while (pq.size()>0) {\r\n st.push(pq.top());\r\n pq.pop();\r\n }\r\n\r\n int res = 0;\r\n int cur_sum = 0;\r\n while (st.size()>0) {\r\n if (cur_sum + st.top()>k) break;\r\n cur_sum += st.top();\r\n res += 1;\r\n st.pop();\r\n } \r\n return counts.size() - res;\r\n }\r\n};",
"memory": "66800"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 2 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n int n = arr.size();\r\n map<int,int> occur;\r\n for(int x: arr){\r\n occur[x]++;\r\n }\r\n\r\n vector<int> freqofoccur(n+1,0);\r\n for(auto it: occur){\r\n freqofoccur[it.second]++;\r\n }\r\n\r\n int ans = occur.size();\r\n\r\n for(int i = 1; i<= n; i++){\r\n int temp = freqofoccur[i];\r\n if(temp == 0){\r\n continue;\r\n }\r\n int t = min(temp, k/ i);\r\n ans -= t;\r\n k -= i*t;\r\n }\r\n\r\n return ans;\r\n }\r\n};",
"memory": "67000"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 2 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<int,int>mp;\r\n for(auto &it:arr){\r\n mp[it]++;\r\n }\r\n int n=arr.size();\r\n int unique=mp.size();\r\n vector<int>freqcount(n+1,0);\r\n for(auto &it:mp){\r\n freqcount[it.second]++;\r\n }\r\n for(int freq=1;freq<=n;freq++){\r\n int nooftypes=min(k/freq,freqcount[freq]);\r\n k-=(freq*nooftypes);\r\n unique-=nooftypes;\r\n if(k<=freq){\r\n return unique;\r\n }\r\n }\r\n return unique;\r\n }\r\n};",
"memory": "67100"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 2 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n map<int,int>mp;\r\n for(auto &it:arr){\r\n mp[it]++;\r\n }\r\n int n=arr.size();\r\n int unique=mp.size();\r\n vector<int>freqcount(n+1,0);\r\n for(auto &it:mp){\r\n freqcount[it.second]++;\r\n }\r\n for(int freq=1;freq<=n;freq++){\r\n int nooftypes=min(k/freq,freqcount[freq]);\r\n k-=(freq*nooftypes);\r\n unique-=nooftypes;\r\n if(k<=freq){\r\n return unique;\r\n }\r\n }\r\n return unique;\r\n }\r\n};",
"memory": "67100"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 2 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\r\n std::map<int,int> m;\r\n std::priority_queue<int> pq;\r\n for(auto& i:arr){\r\n m[i]++;\r\n }\r\n // push negative to pq so smallest vals at top\r\n for(auto& [k,v]:m){\r\n pq.push(-v);\r\n }\r\n int res = m.size();\r\n while(k>=-pq.top()){\r\n k+=pq.top(); pq.pop();\r\n res--;\r\n }\r\n return res;\r\n }\r\n};",
"memory": "67200"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 2 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int> freqMap;\r\n for(auto x: arr){\r\n freqMap[x]++;\r\n }\r\n vector<pair<int,int>> v(freqMap.begin(), freqMap.end());\r\n sort(v.begin(), v.end(), [](pair<int,int> &a, pair<int,int> &b){\r\n return a.second < b.second;\r\n });\r\n int sum = 0;\r\n for(int i = 0; i < v.size(); i++){\r\n sum +=v[i].second;\r\n if(sum <= k){\r\n v[i].second=0;\r\n }\r\n }\r\n\r\n vector<int> uniques;\r\n for(auto freq:v){\r\n if(freq.second > 0) {\r\n uniques.push_back(freq.first);\r\n cout<<freq.first<< \" \";\r\n }\r\n \r\n }\r\n\r\n return uniques.size();\r\n }\r\n};",
"memory": "67400"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 2 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arrr, int k) {\r\n map<int, int> mpp;\r\n for (auto it : arrr) {\r\n mpp[it]++;\r\n }\r\n\r\n // Extract the elements into a vector of pairs\r\n vector<pair<int, int>> arr(mpp.begin(), mpp.end());\r\n\r\n // Sort by frequency (second element) in ascending order\r\n sort(arr.begin(), arr.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\r\n return a.second < b.second;\r\n });\r\n\r\n int id = 0;\r\n int uniqueCount = arr.size(); // Initial number of unique integers\r\n\r\n // Reduce the frequency of elements by removing `k` elements in total\r\n while (k > 0 && id < arr.size()) {\r\n if (arr[id].second <= k) {\r\n k -= arr[id].second;\r\n uniqueCount--; // One unique element removed\r\n id++;\r\n } else {\r\n break; // If we can't remove all of the current frequency, break\r\n }\r\n }\r\n\r\n return uniqueCount;\r\n }\r\n};\r\n",
"memory": "67500"
} |
1,604 | <p>Given an array of integers <code>arr</code> and an integer <code>k</code>. Find the <em>least number of unique integers</em> after removing <strong>exactly</strong> <code>k</code> elements<b>.</b></p>
<ol>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input: </strong>arr = [5,5,4], k = 1
<strong>Output: </strong>1
<strong>Explanation</strong>: Remove the single 4, only 5 is left.
</pre>
<strong class="example">Example 2:</strong>
<pre>
<strong>Input: </strong>arr = [4,3,1,1,3,3,2], k = 3
<strong>Output: </strong>2
<strong>Explanation</strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10^5</code></li>
<li><code>1 <= arr[i] <= 10^9</code></li>
<li><code>0 <= k <= arr.length</code></li>
</ul> | 2 | {
"code": "class Solution {\r\npublic:\r\n int findLeastNumOfUniqueInts(vector<int>& arr, int k) {\r\n unordered_map<int,int> freq;\r\n for(int &x : arr) {\r\n freq[x]++;\r\n }\r\n vector<int> fast;\r\n for(auto x : freq) {\r\n fast.push_back(x.second);\r\n }\r\n make_heap(fast.begin(),fast.end());\r\n priority_queue<int, vector<int>, greater<int>> pq(fast.begin(), fast.end());\r\n int x;\r\n for(int i = 0; i < k; i++) {\r\n if(pq.top() > 1) {\r\n x = pq.top();\r\n pq.pop();\r\n pq.push(x - 1);\r\n }\r\n else {\r\n pq.pop();\r\n }\r\n }\r\n return pq.size();\r\n }\r\n};",
"memory": "67600"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.