二分查找--215. 数组中的第K个最大元素/medium 理解度B(wait opt)

本文介绍了一种使用快排分治思想解决数组中第K个最大元素的问题,提供了一个O(n)复杂度的代码示例,并讨论了在不同场景下的应用,如数据分析、算法设计、游戏开发等。

1、题目

给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。

请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

你必须设计并实现时间复杂度为 O(n) 的算法解决此问题。

 

示例 1:

输入: [3,2,1,5,6,4], k = 2
输出: 5

示例 2:

输入: [3,2,3,1,2,4,5,5,6], k = 4
输出: 4

 

提示:

  • 1 <= k <= nums.length <= 105
  • -104 <= nums[i] <= 104
Related Topics
  • 数组
  • 分治
  • 快速选择
  • 排序
  • 堆(优先队列)

2、题目分析

借助快排的分治思想,找到区间支点,再判断支点所在位置是否为第k大的位置

3、复杂度最优解代码示例

    Integer kNum = null;
    public int findKthLargest(int[] nums, int k) {
        findKth(nums, k, 0, nums.length - 1);
        return kNum == null ? -1 : nums[kNum];
    }
    
    public void findKth(int[] nums, int k, int left, int right) {
        if (kNum != null || left > right) {
            // 退出递归,已找到第k大,或者左指针大于右指针。
            return;
        }
        int leftArgs = left;
        int rightArgs = right;
        // 支点标识:true-支点为左指针,false-支点为右指针
        boolean isLeft = true;
        while (left <= right) {
            if (nums[left] >= nums[right]) {
                // 若左指针数据大于右指针数据,则互换位置,并标识新的支点指针。
                // 注:nums[left] == nums[right] 时,从结果角度看,可不互换。但若不互换,在特殊情况,比如数组元素均相等时,时间复杂度会从O(nlogn)退化为O(n²)
                int temp = nums[right];
                nums[right] = nums[left];
                nums[left] = temp;

                isLeft = !isLeft;
            }
            if (isLeft) {
                // isLeft=true,表明支点为左指针,此时移动右指针
                right--;
            } else {
                // isLeft=false,表明支点为右指针,此时移动左指针
                left++;
            }
        }

        int point = isLeft ? left : right;
        if (point == nums.length - k) {
            // 判断支点是否为第k大,是则不再递归分治。
            kNum = point;
        } else if (point > nums.length - k) {
            // 支点所在位置大于第k大,故第k大在当前支点的左边区间
            findKth(nums, k, leftArgs, point - 1);
        } else {
            // 支点所在位置小于第k大,故第k大在当前支点的右边区间
            findKth(nums, k, point + 1, rightArgs);
        }
    }

4、适用场景

  1. 数据分析:在处理大量数据时,找出第K个最大元素可以帮助我们了解数据的分布情况,例如在销售数据中找出销售额排名第K的商品。

  2. 算法设计:在设计一些需要排序的算法时,如快速排序、堆排序等,找出第K个最大元素可以作为算法的一部分。

  3. 游戏开发:在一些游戏中,可能需要找出得分排名第K的玩家,这时就需要找出数组中的第K个最大元素。

  4. 网络编程:在网络编程中,可能需要找出传输速度排名第K的节点,这时也需要找出数组中的第K个最大元素。

  5. 机器学习:在机器学习中,可能需要找出预测结果排名第K的模型,这时也需要找出数组中的第K个最大元素。

  6. 数据库查询:在数据库查询中,可能需要找出某个字段值排名第K的记录,这时也需要找出数组中的第K个最大元素。

[root@yfw ~]# cd /opt/openfire/bin [root@yfw bin]# ./start.sh Using Java: /usr/lib/jvm/java-17-openjdk/bin/java Java Version: openjdk version "17.0.1" 2021-10-19 LTS OpenJDK Runtime Environment 21.9 (build 17.0.1+12-LTS) OpenJDK 64-Bit Server VM 21.9 (build 17.0.1+12-LTS, mixed mode, sharing) Classpath: /opt/openfire/resources:/opt/openfire/lib/activation-1.1.jar:/opt/openfire/lib/apache-el-9.0.107.jar:/opt/openfire/lib/apache-jsp-9.0.107.jar:/opt/openfire/lib/asm-9.8.jar:/opt/openfire/lib/asm-commons-9.8.jar:/opt/openfire/lib/asm-tree-9.8.jar:/opt/openfire/lib/bcpg-jdk18on-1.78.1.jar:/opt/openfire/lib/bcpkix-jdk18on-1.78.1.jar:/opt/openfire/lib/bcprov-jdk18on-1.78.1.jar:/opt/openfire/lib/bcutil-jdk18on-1.78.1.jar:/opt/openfire/lib/caffeine-3.2.0.jar:/opt/openfire/lib/checker-qual-3.33.0.jar:/opt/openfire/lib/common-image-3.9.4.jar:/opt/openfire/lib/common-io-3.9.4.jar:/opt/openfire/lib/common-lang-3.9.4.jar:/opt/openfire/lib/commons-codec-1.15.jar:/opt/openfire/lib/commons-dbcp2-2.9.0.jar:/opt/openfire/lib/commons-ip-math-1.32.jar:/opt/openfire/lib/commons-lang3-3.18.0.jar:/opt/openfire/lib/commons-logging-1.2.jar:/opt/openfire/lib/commons-pool2-2.9.0.jar:/opt/openfire/lib/commons-text-1.10.0.jar:/opt/openfire/lib/dom4j-2.1.4.jar:/opt/openfire/lib/dwr-3.0.2-RELEASE.jar:/opt/openfire/lib/ecj-3.33.0.jar:/opt/openfire/lib/error_prone_annotations-2.18.0.jar:/opt/openfire/lib/failureaccess-1.0.1.jar:/opt/openfire/lib/guava-32.0.1-jre.jar:/opt/openfire/lib/hsqldb-2.7.1.jar:/opt/openfire/lib/httpclient-4.5.13.jar:/opt/openfire/lib/httpcore-4.4.13.jar:/opt/openfire/lib/i18n-5.0.2.jar:/opt/openfire/lib/imageio-bmp-3.9.4.jar:/opt/openfire/lib/imageio-core-3.9.4.jar:/opt/openfire/lib/istack-commons-runtime-3.0.11.jar:/opt/openfire/lib/j2objc-annotations-2.8.jar:/opt/openfire/lib/jakarta.activation-1.2.2.jar:/opt/openfire/lib/jakarta.annotation-api-1.3.5.jar:/opt/openfire/lib/jakarta.transaction-api-1.3.3.jar:/opt/openfire/lib/jakarta.xml.bind-api-2.3.3.jar:/opt/openfire/lib/jansi-1.18.jar:/opt/openfire/lib/javax.activation-api-1.2.0.jar:/opt/openfire/lib/javax.mail-1.6.2.jar:/opt/openfire/lib/jaxb-api-2.3.1.jar:/opt/openfire/lib/jaxb-runtime-2.3.3.jar:/opt/openfire/lib/jaxen-1.2.0.jar:/opt/openfire/lib/jcip-annotations-1.0.jar:/opt/openfire/lib/jcl-over-slf4j-2.0.9.jar:/opt/openfire/lib/jetty-ee-12.0.24.jar:/opt/openfire/lib/jetty-ee8-annotations-12.0.24.jar:/opt/openfire/lib/jetty-ee8-apache-jsp-12.0.24.jar:/opt/openfire/lib/jetty-ee8-nested-12.0.24.jar:/opt/openfire/lib/jetty-ee8-plus-12.0.24.jar:/opt/openfire/lib/jetty-ee8-security-12.0.24.jar:/opt/openfire/lib/jetty-ee8-servlet-12.0.24.jar:/opt/openfire/lib/jetty-ee8-webapp-12.0.24.jar:/opt/openfire/lib/jetty-ee8-websocket-jetty-api-12.0.24.jar:/opt/openfire/lib/jetty-ee8-websocket-jetty-common-12.0.24.jar:/opt/openfire/lib/jetty-ee8-websocket-jetty-server-12.0.24.jar:/opt/openfire/lib/jetty-ee8-websocket-servlet-12.0.24.jar:/opt/openfire/lib/jetty-http-12.0.24.jar:/opt/openfire/lib/jetty-io-12.0.24.jar:/opt/openfire/lib/jetty-jmx-12.0.24.jar:/opt/openfire/lib/jetty-jndi-12.0.24.jar:/opt/openfire/lib/jetty-plus-12.0.24.jar:/opt/openfire/lib/jetty-security-12.0.24.jar:/opt/openfire/lib/jetty-server-12.0.24.jar:/opt/openfire/lib/jetty-servlet-api-4.0.6.jar:/opt/openfire/lib/jetty-session-12.0.24.jar:/opt/openfire/lib/jetty-util-12.0.24.jar:/opt/openfire/lib/jetty-websocket-core-common-12.0.24.jar:/opt/openfire/lib/jetty-websocket-core-server-12.0.24.jar:/opt/openfire/lib/jetty-xml-12.0.24.jar:/opt/openfire/lib/jmdns-1.0.jar:/opt/openfire/lib/jsmpp-2.3.10.jar:/opt/openfire/lib/json-20231013.jar:/opt/openfire/lib/jspecify-1.0.0.jar:/opt/openfire/lib/jsr305-3.0.2.jar:/opt/openfire/lib/jtds-1.3.1.jar:/opt/openfire/lib/jzlib-1.1.3.jar:/opt/openfire/lib/libidn-1.35.jar:/opt/openfire/lib/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:/opt/openfire/lib/log4j-api-2.20.0.jar:/opt/openfire/lib/log4j-core-2.20.0.jar:/opt/openfire/lib/log4j-slf4j2-impl-2.20.0.jar:/opt/openfire/lib/mssql-jdbc-9.4.1.jre11.jar:/opt/openfire/lib/mysql-connector-java-5.1.49.jar:/opt/openfire/lib/netty-all-4.1.118.Final.jar:/opt/openfire/lib/netty-buffer-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-dns-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-haproxy-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-http2-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-http-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-memcache-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-mqtt-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-redis-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-smtp-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-socks-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-stomp-4.1.118.Final.jar:/opt/openfire/lib/netty-codec-xml-4.1.118.Final.jar:/opt/openfire/lib/netty-common-4.1.118.Final.jar:/opt/openfire/lib/netty-handler-4.1.118.Final.jar:/opt/openfire/lib/netty-handler-proxy-4.1.118.Final.jar:/opt/openfire/lib/netty-handler-ssl-ocsp-4.1.118.Final.jar:/opt/openfire/lib/netty-resolver-4.1.118.Final.jar:/opt/openfire/lib/netty-resolver-dns-4.1.118.Final.jar:/opt/openfire/lib/netty-resolver-dns-classes-macos-4.1.118.Final.jar:/opt/openfire/lib/netty-resolver-dns-native-macos-4.1.118.Final-osx-aarch_64.jar:/opt/openfire/lib/netty-resolver-dns-native-macos-4.1.118.Final-osx-x86_64.jar:/opt/openfire/lib/netty-transport-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-classes-epoll-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-classes-kqueue-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-native-epoll-4.1.118.Final-linux-aarch_64.jar:/opt/openfire/lib/netty-transport-native-epoll-4.1.118.Final-linux-riscv64.jar:/opt/openfire/lib/netty-transport-native-epoll-4.1.118.Final-linux-x86_64.jar:/opt/openfire/lib/netty-transport-native-kqueue-4.1.118.Final-osx-aarch_64.jar:/opt/openfire/lib/netty-transport-native-kqueue-4.1.118.Final-osx-x86_64.jar:/opt/openfire/lib/netty-transport-native-unix-common-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-rxtx-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-sctp-4.1.118.Final.jar:/opt/openfire/lib/netty-transport-udt-4.1.118.Final.jar:/opt/openfire/lib/ojdbc11-23.7.0.25.01.jar:/opt/openfire/lib/ons-23.7.0.25.01.jar:/opt/openfire/lib/oraclepki-23.7.0.25.01.jar:/opt/openfire/lib/orai18n-23.7.0.25.01.jar:/opt/openfire/lib/postgresql-42.7.7.jar:/opt/openfire/lib/rsi-23.7.0.25.01.jar:/opt/openfire/lib/shaj-0.5.jar:/opt/openfire/lib/simplefan-23.7.0.25.01.jar:/opt/openfire/lib/sitemesh-2.5.0.jar:/opt/openfire/lib/slf4j-api-2.0.9.jar:/opt/openfire/lib/startup.jar:/opt/openfire/lib/taglibs-standard-impl-1.2.5.jar:/opt/openfire/lib/taglibs-standard-spec-1.2.5.jar:/opt/openfire/lib/tinder-2.1.0.jar:/opt/openfire/lib/txw2-2.3.3.jar:/opt/openfire/lib/ucp-23.7.0.25.01.jar:/opt/openfire/lib/xdb-23.7.0.25.01.jar:/opt/openfire/lib/xmppserver-5.0.2.jar:/opt/openfire/lib/xpp3-1.1.4c.0.jar Starting OpenFire... OpenFire started (PID 1138145) Check logs: tail -f /opt/openfire/logs/nohup.out [root@yfw bin]#
最新发布
11-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值