{"id":9722,"date":"2022-05-03T17:56:59","date_gmt":"2022-05-04T00:56:59","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9722"},"modified":"2022-05-03T17:57:14","modified_gmt":"2022-05-04T00:57:14","slug":"leetcode-2260-minimum-consecutive-cards-to-pick-up","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2260-minimum-consecutive-cards-to-pick-up\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2260. Minimum Consecutive Cards to Pick Up"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>cards<\/code>&nbsp;where&nbsp;<code>cards[i]<\/code>&nbsp;represents the&nbsp;<strong>value<\/strong>&nbsp;of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;card. A pair of cards are&nbsp;<strong>matching<\/strong>&nbsp;if the cards have the&nbsp;<strong>same<\/strong>&nbsp;value.<\/p>\n\n\n\n<p>Return<em>&nbsp;the&nbsp;<strong>minimum<\/strong>&nbsp;number of&nbsp;<strong>consecutive<\/strong>&nbsp;cards you have to pick up to have a pair of&nbsp;<strong>matching<\/strong>&nbsp;cards among the picked cards.<\/em>&nbsp;If it is impossible to have matching cards, return&nbsp;<code>-1<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> cards = [3,4,2,3,4,7]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> We can pick up the cards [3,4,2,3] which contain a matching pair of cards with value 3. Note that picking up the cards [4,2,3,4] is also optimal.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> cards = [1,0,5,3]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> There is no way to pick up a set of consecutive cards that contain a pair of matching cards.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= cards.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= cards[i] &lt;= 10<sup>6<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Record the last position of each number,<br>ans = min{card<sub>i<\/sub> &#8211; last[<meta charset=\"utf-8\">card<sub>i<\/sub>]}<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minimumCardPickup(vector<int>& cards) {\n    const int n = cards.size();\n    unordered_map<int, int> m;\n    int ans = INT_MAX;\n    for (int i = 0; i < n; ++i) {\n      if (m.count(cards[i]))\n        ans = min(ans, i - m[cards[i]] + 1);\n      m[cards[i]] = i;\n    }\n    return ans == INT_MAX ? -1 : ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;cards&nbsp;where&nbsp;cards[i]&nbsp;represents the&nbsp;value&nbsp;of the&nbsp;ith&nbsp;card. A pair of cards are&nbsp;matching&nbsp;if the cards have the&nbsp;same&nbsp;value. Return&nbsp;the&nbsp;minimum&nbsp;number of&nbsp;consecutive&nbsp;cards you have to pick up to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177],"class_list":["post-9722","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9722"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9722\/revisions"}],"predecessor-version":[{"id":9724,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9722\/revisions\/9724"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}