{"id":3168,"date":"2018-07-14T22:43:37","date_gmt":"2018-07-15T05:43:37","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3168"},"modified":"2018-08-27T08:11:28","modified_gmt":"2018-08-27T15:11:28","slug":"leetcode-870-advantage-shuffle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-870-advantage-shuffle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 870. Advantage Shuffle"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given two arrays\u00a0<code>A<\/code>\u00a0and\u00a0<code>B<\/code>\u00a0of equal size, the\u00a0<em>advantage of\u00a0<code>A<\/code>\u00a0with respect to\u00a0<code>B<\/code><\/em>\u00a0is the number of indices\u00a0<code>i<\/code>\u00a0for which\u00a0<code>A[i] &gt; B[i]<\/code>.<\/p>\n<p>Return\u00a0<strong>any<\/strong>\u00a0permutation of\u00a0<code>A<\/code>\u00a0that maximizes its advantage with respect to\u00a0<code>B<\/code>.<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>A = <span id=\"example-input-1-1\">[2,7,11,15]<\/span>, B = <span id=\"example-input-1-2\">[1,10,4,11]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[2,11,7,15]<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>A = <span id=\"example-input-2-1\">[12,24,8,32]<\/span>, B = <span id=\"example-input-2-2\">[13,25,32,11]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[24,32,8,12]<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= A.length = B.length &lt;= 10000<\/code><\/li>\n<li><code>0 &lt;= A[i] &lt;= 10^9<\/code><\/li>\n<li><code>0 &lt;= B[i] &lt;= 10^9<\/code><\/li>\n<\/ol>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\">\u00a0<\/ins><\/p>\n<h1><strong>Solution: Greedy \u7530\u5fcc\u8d5b\u9a6c<\/strong><\/h1>\n<p>Use the smallest unused number A[j] in A such that A[j] &gt; B[i], if not possible, use the smallest number in A.<\/p>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 124 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; advantageCount(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {\r\n    multiset&lt;int&gt; s(begin(A), end(A));\r\n    vector&lt;int&gt; ans;    \r\n    for (int b : B) {\r\n      auto it = s.upper_bound(b);\r\n      if (it == s.end()) it = s.begin();      \r\n      ans.push_back(*it);\r\n      s.erase(it);\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given two arrays\u00a0A\u00a0and\u00a0B\u00a0of equal size, the\u00a0advantage of\u00a0A\u00a0with respect to\u00a0B\u00a0is the number of indices\u00a0i\u00a0for which\u00a0A[i] &gt; B[i]. Return\u00a0any\u00a0permutation of\u00a0A\u00a0that maximizes its advantage with respect to\u00a0B.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[320,177,335,15],"class_list":["post-3168","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-inversion","tag-medium","tag-multiset","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3168","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=3168"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3168\/revisions"}],"predecessor-version":[{"id":3724,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3168\/revisions\/3724"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}