zoukankan      html  css  js  c++  java
  • [LeetCode] 1471. The k Strongest Values in an Array

    Given an array of integers arr and an integer k.

    A value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array.
    If |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j].

    Return a list of the strongest k values in the array. return the answer in any arbitrary order.

    Median 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 ((n - 1) / 2) in the sorted list (0-indexed).

    • For arr = [6, -3, 7, 2, 11]n = 5 and the median is obtained by sorting the array arr = [-3, 2, 6, 7, 11] and the median is arr[m] where m = ((5 - 1) / 2) = 2. The median is 6.
    • For arr = [-7, 22, 17, 3]n = 4 and the median is obtained by sorting the array arr = [-7, 3, 17, 22] and the median is arr[m] where m = ((4 - 1) / 2) = 1. The median is 3.

    Example 1:

    Input: arr = [1,2,3,4,5], k = 2
    Output: [5,1]
    Explanation: 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 accepted answer.
    Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1.
    

    Example 2:

    Input: arr = [1,1,3,5,5], k = 2
    Output: [5,5]
    Explanation: 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].
    

    Example 3:

    Input: arr = [6,7,11,7,6,8], k = 5
    Output: [11,8,6,6,7]
    Explanation: Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7].
    Any permutation of [11,8,6,6,7] is accepted.
    

    Example 4:

    Input: arr = [6,-3,7,2,11], k = 3
    Output: [-3,11,2]
    

    Example 5:

    Input: arr = [-7,22,17,3], k = 2
    Output: [22,17]

    Constraints:

    • 1 <= arr.length <= 10^5
    • -10^5 <= arr[i] <= 10^5
    • 1 <= k <= arr.length

    数组中的 k 个最强值。

    给你一个整数数组 arr 和一个整数 k 。

    设 m 为数组的中位数,只要满足下述两个前提之一,就可以判定 arr[i] 的值比 arr[j] 的值更强:

     |arr[i] - m| > |arr[j] - m|
     |arr[i] - m| == |arr[j] - m|,且 arr[i] > arr[j]
    请返回由数组中最强的 k 个值组成的列表。答案可以以 任意顺序 返回。

    中位数 是一个有序整数列表中处于中间位置的值。形式上,如果列表的长度为 n ,那么中位数就是该有序列表(下标从 0 开始)中位于 ((n - 1) / 2) 的元素。

    例如 arr = [6, -3, 7, 2, 11],n = 5:数组排序后得到 arr = [-3, 2, 6, 7, 11] ,数组的中间位置为 m = ((5 - 1) / 2) = 2 ,中位数 arr[m] 的值为 6 。
    例如 arr = [-7, 22, 17, 3],n = 4:数组排序后得到 arr = [-7, 3, 17, 22] ,数组的中间位置为 m = ((4 - 1) / 2) = 1 ,中位数 arr[m] 的值为 3 。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/the-k-strongest-values-in-an-array
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    思路是two pointer。首先为了求中位数,对input数组排序是不可避免的了,排序完之后就可以找到中位数。同时,因为input数组被排序了所以我们可以利用左右两个指针,分别根据规则计算左右指针指向的元素的强度。因为中位数一定是在排序后的数组中比较中间的位置,同时强度的计算又和数字与中位数的距离挂钩,所以数组两端的数字应该是input数组中强度最大的。接下来我们就可以按照规则计算找到强度最大的前K个元素,并视情况而定去移动左右指针。

    时间O(nlogn)

    空间O(1)

    Java实现

     1 class Solution {
     2     public int[] getStrongest(int[] arr, int k) {
     3         Arrays.sort(arr);
     4         int median = arr[(arr.length - 1) / 2];
     5         int[] res = new int[k];
     6         int i = 0;
     7         int left = 0;
     8         int right = arr.length - 1;
     9         while (left <= right && i < k) {
    10             if (median - arr[left] > arr[right] - median) {
    11                 res[i] = arr[left];
    12                 i++;
    13                 left++;
    14             } else {
    15                 res[i] = arr[right];
    16                 i++;
    17                 right--;
    18             }
    19         }
    20         return res;
    21     }
    22 }

    LeetCode 题目总结 

  • 相关阅读:
    关于GridView Master-Detail 不支持明细属性为IEnumerable、IList问题
    GridControl摘录
    DevExpress中chartControl中实现统计图功能
    DevExpress中GridControl列转义的实现方法
    DevExpress实现根据行,列索引来获取RepositoryItem的方法
    修改yum源为阿里云的
    使用sendmail来发邮件
    nginx默认访问目录时显示403错误
    同时调整lv分区的大小(减少一个,增加另一个)
    ORA-00257错误的解决办法
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14275294.html
Copyright © 2011-2022 走看看