码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 数组题目总结 ---- 田忌赛马


    目录

    • 一. 优势洗牌
      • 思路和代码:
        • 法一:优先队列法
        • 法二:索引排序法

    一. 优势洗牌

    • 题目链接:https://leetcode.cn/problems/advantage-shuffle/description/

    思路和代码:

    • 本质上就是两个队列给它排序,我方最大和敌方max打,
      • 打得过,就将我方max加入res当中;
      • 如果打不过,就让我方min打地方的max,将我方min放入res当中。

    法一:优先队列法

    class Solution {
        public int[] advantageCount(int[] nums1, int[] nums2) {
            int n = nums1.length;
    		//设置优先队列,排序的规则是从大到小排列
            PriorityQueue<int[]> maxpq = new PriorityQueue<>(
                (int[] pair1, int[] pair2) ->{
                    return pair2[1] - pair1[1];
                }
            );
            //将nums1排序(从小到大)
            Arrays.sort(nums1);
    
            int left = 0, right = n-1;
            int[] res = new int[n];
            for(int i = 0; i < n; i++)
                maxpq.offer(new int[]{i, nums2[i]});
            
            while(!maxpq.isEmpty()){
                int[] pair = maxpq.poll();
                //maxpq 第一个是max,而对于nums1来说,最右边是max
                int i = pair[0], maxval = pair[1];
                if(maxval < nums1[right]){
                    res[i] = nums1[right];
                    right--;
                }
                else{
                    res[i] = nums1[left];
                    left++;
                }
                   
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    法二:索引排序法

    class Solution {
        public int[] advantageCount(int[] nums1, int[] nums2) {
            int n = nums1.length;
    
            Integer[] index = new Integer[n];
    
            for(int i = 0; i < n; i++)
                index[i] = i;
            
    
            Arrays.sort(nums1);
            //从小到大排序
            Arrays.sort(index, (i, j) -> nums2[i] - nums2[j]);
    
            int left = 0, right = n-1;
    		
    		//直接再nums2上更新结果,如果num大放到前面,如果num小,放到最后面去
            for(int num : nums1){
                if(num > nums2[index[left]])
                    nums2[index[left++]] = num;
                else
                    nums2[index[right--]] = num;
            }
            return nums2;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 时间复杂度:O(nlogn),堆排序的时间,Arrays.sort()底层用的是归并排序,PriorityQueue底层用的是二叉堆来实现的。
    • 空间复杂度:O(n),存储下标用的n个空间。
      参考:https://labuladong.github.io/algo/di-yi-zhan-da78c/shou-ba-sh-48c1d/tian-ji-sa-7baa4/
  • 相关阅读:
    【智慧医疗】Springboot+Vue+Element-UI前后端分离的医疗管理平台
    【Spring】注解
    Ubuntu架设mc_server服务器详细过程
    高效阅读JDK源码,保姆级JDK源码笔记真香
    【C++】红黑树的模拟实现
    安达发|在医药化工行业中好用的APS计划排程软件推荐
    【Linux】第十二站:进程
    HTTP/1.1协议中的响应报文
    13【JSR303校验】
    黄子韬直播风暴揭秘经济人风波
  • 原文地址:https://blog.csdn.net/qq_39671159/article/details/132999029
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号