{"id":6883,"date":"2020-06-06T22:30:36","date_gmt":"2020-06-07T05:30:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6883"},"modified":"2020-06-06T22:37:22","modified_gmt":"2020-06-07T05:37:22","slug":"leetcode-1471-the-k-strongest-values-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1471-the-k-strongest-values-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1471. The k Strongest Values in an Array"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>arr<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>A value&nbsp;<code>arr[i]<\/code>&nbsp;is said to be stronger than a value&nbsp;<code>arr[j]<\/code>&nbsp;if&nbsp;<code>|arr[i] - m| &gt; |arr[j]&nbsp;- m|<\/code>&nbsp;where&nbsp;<code>m<\/code>&nbsp;is the&nbsp;<strong>median<\/strong>&nbsp;of the array.<br>If&nbsp;<code>|arr[i] - m| == |arr[j] - m|<\/code>, then&nbsp;<code>arr[i]<\/code>&nbsp;is said to be stronger than&nbsp;<code>arr[j]<\/code>&nbsp;if&nbsp;<code>arr[i] &gt; arr[j]<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>a list of the strongest&nbsp;<code>k<\/code><\/em>&nbsp;values in the array. return the answer&nbsp;<strong>in any arbitrary order<\/strong>.<\/p>\n\n\n\n<p><strong>Median<\/strong>&nbsp;is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position&nbsp;<code>((n - 1) \/ 2)<\/code>&nbsp;in the sorted list&nbsp;<strong>(0-indexed)<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For&nbsp;<code>arr =&nbsp;[6, -3, 7, 2, 11]<\/code>,&nbsp;<code>n = 5<\/code>&nbsp;and the median is obtained by sorting the array&nbsp;<code>arr = [-3, 2, 6, 7, 11]<\/code>&nbsp;and the median is&nbsp;<code>arr[m]<\/code>&nbsp;where&nbsp;<code>m = ((5 - 1) \/ 2) = 2<\/code>. The median is&nbsp;<code>6<\/code>.<\/li><li>For&nbsp;<code>arr =&nbsp;[-7, 22, 17,\u20093]<\/code>,&nbsp;<code>n = 4<\/code>&nbsp;and the median is obtained by sorting the array&nbsp;<code>arr = [-7, 3, 17, 22]<\/code>&nbsp;and the median is&nbsp;<code>arr[m]<\/code>&nbsp;where&nbsp;<code>m = ((4 - 1) \/ 2) = 1<\/code>. The median is&nbsp;<code>3<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,2,3,4,5], k = 2\n<strong>Output:<\/strong> [5,1]\n<strong>Explanation:<\/strong> Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also <strong>accepted<\/strong> answer.\nPlease note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 &gt; 1.\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> arr = [1,1,3,5,5], k = 2\n<strong>Output:<\/strong> [5,5]\n<strong>Explanation:<\/strong> Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5].\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [6,7,11,7,6,8], k = 5\n<strong>Output:<\/strong> [11,8,6,6,7]\n<strong>Explanation:<\/strong> Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7].\nAny permutation of [11,8,6,6,7] is <strong>accepted<\/strong>.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [6,-3,7,2,11], k = 3\n<strong>Output:<\/strong> [-3,11,2]\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [-7,22,17,3], k = 2\n<strong>Output:<\/strong> [22,17]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 10^5<\/code><\/li><li><code>-10^5 &lt;= arr[i] &lt;= 10^5<\/code><\/li><li><code>1 &lt;= k &lt;= arr.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: quick selection + sort<\/strong><\/h2>\n\n\n\n<p>Step 1: find the median element m<br>Step 2: Sort the array according to m<br>Step 3: output the first k elements of the sorted array.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<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  vector<int> getStrongest(vector<int>& arr, int k) {\n    const int n = arr.size();\n    const int i = (n - 1) \/ 2;\n    \/\/ sort(begin(arr), end(arr));\n    nth_element(begin(arr), begin(arr) + i, end(arr));\n    const int m = arr[i];  \n    sort(begin(arr), end(arr), [&](const int x, const int y) {      \n      return abs(x - m) != abs(y - m) ? abs(x - m) > abs(y - m) : x > y;      \n    });    \n    return {begin(arr), begin(arr) + k};\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;arr&nbsp;and an integer&nbsp;k. A value&nbsp;arr[i]&nbsp;is said to be stronger than a value&nbsp;arr[j]&nbsp;if&nbsp;|arr[i] &#8211; m| &gt; |arr[j]&nbsp;&#8211; m|&nbsp;where&nbsp;m&nbsp;is the&nbsp;median&nbsp;of the array.If&nbsp;|arr[i] -&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[11,573,15],"class_list":["post-6883","post","type-post","status-publish","format-standard","hentry","category-array","tag-median","tag-quickselect","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6883","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=6883"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6883\/revisions"}],"predecessor-version":[{"id":6885,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6883\/revisions\/6885"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}