{"id":9827,"date":"2022-09-17T13:04:24","date_gmt":"2022-09-17T20:04:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9827"},"modified":"2022-09-17T13:06:12","modified_gmt":"2022-09-17T20:06:12","slug":"leetcode-2410-maximum-matching-of-players-with-trainers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-2410-maximum-matching-of-players-with-trainers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2410.\u00a0Maximum Matching of Players With Trainers"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>players<\/code>, where&nbsp;<code>players[i]<\/code>&nbsp;represents the&nbsp;<strong>ability<\/strong>&nbsp;of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;player. You are also given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>trainers<\/code>, where&nbsp;<code>trainers[j]<\/code>&nbsp;represents the&nbsp;<strong>training capacity&nbsp;<\/strong>of the&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;trainer.<\/p>\n\n\n\n<p>The&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;player can&nbsp;<strong>match<\/strong>&nbsp;with the&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;trainer if the player&#8217;s ability is&nbsp;<strong>less than or equal to<\/strong>&nbsp;the trainer&#8217;s training capacity. Additionally, the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;player can be matched with at most one trainer, and the&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;trainer can be matched with at most one player.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;number of matchings between&nbsp;<\/em><code>players<\/code><em>&nbsp;and&nbsp;<\/em><code>trainers<\/code><em>&nbsp;that satisfy these conditions.<\/em><\/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> players = [4,7,9], trainers = [8,2,5,8]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nOne of the ways we can form two matchings is as follows:\n- players[0] can be matched with trainers[0] since 4 &lt;= 8.\n- players[1] can be matched with trainers[3] since 7 &lt;= 8.\nIt can be proven that 2 is the maximum number of matchings that can be formed.\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> players = [1,1,1], trainers = [10]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>\nThe trainer can be matched with any of the 3 players.\nEach player can only be matched with one trainer, so the maximum answer is 1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= players.length, trainers.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= players[i], trainers[j] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sort + Two Pointers<\/strong><\/h2>\n\n\n\n<p>Sort players and trainers.<\/p>\n\n\n\n<p>Loop through players, skip trainers until he\/she can match the current players.<\/p>\n\n\n\n<p>Time complexity: O(nlogn + mlogm + n + m)<br>Space complexity: O(1)<\/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 matchPlayersAndTrainers(vector<int>& players, vector<int>& trainers) {\n    sort(begin(players), end(players));\n    sort(begin(trainers), end(trainers));\n    const int n = players.size();\n    const int m = trainers.size();\n    int ans = 0;\n    for (int i = 0, j = 0; i < n &#038;&#038; j < m; ++i) {\n      while (j < m &#038;&#038; players[i] > trainers[j]) ++j;\n      if (j++ == m) break;\n      ++ans;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;players, where&nbsp;players[i]&nbsp;represents the&nbsp;ability&nbsp;of the&nbsp;ith&nbsp;player. You are also given a&nbsp;0-indexed&nbsp;integer array&nbsp;trainers, where&nbsp;trainers[j]&nbsp;represents the&nbsp;training capacity&nbsp;of the&nbsp;jth&nbsp;trainer. The&nbsp;ith&nbsp;player can&nbsp;match&nbsp;with the&nbsp;jth&nbsp;trainer if the player&#8217;s ability&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[177,23,175],"class_list":["post-9827","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-medium","tag-sort","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9827","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=9827"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9827\/revisions"}],"predecessor-version":[{"id":9830,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9827\/revisions\/9830"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}