当前位置: 首页 > news >正文

分治算法+题目

分治算法+题目

  • 分治算法是什么
  • 题目:合并K个升序链表
  • 总结

分治算法是什么

问题分解后进行求解,相比于不分解直接求解,时间复杂度更低。符合这个特征的算法,我们才称之为「分治算法」。

题目:合并K个升序链表

题目
在这里插入图片描述
分治算法:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    //分治的思想 二分分治 时间复杂度是O(N*logN)
    public ListNode mergeKLists(ListNode[] lists) {
        int sz=lists.length;
        return merge(lists,0,sz-1);
    }
    //定义:合并lists[startIndex...endIndex]上的链表
    ListNode merge(ListNode[] lists,int startIndex,int endIndex){
        //没有链表
        if(startIndex>endIndex){
            return null;
        }
     
        //一条链表
        if(endIndex==startIndex){
            return lists[startIndex];
        }


        //分成两段 [startIndex,mid][mid+1,endIndex]
        int mid=(startIndex+endIndex)/2;

        //合并左边的
       ListNode left= merge(lists,startIndex,mid);

        //合并右边的
       ListNode right= merge(lists,mid+1,endIndex);

        //合并左右边的
       return mergeTwoLists(left,right);
    }
    ListNode mergeTwoLists(ListNode list1,ListNode list2){
         ListNode dummy=new ListNode(-1);
        ListNode cur=dummy;
        ListNode p1=list1;
        ListNode p2=list2;
        while(p1!=null && p2!=null){
            if(p1.val<p2.val){
                cur.next=p1;
                p1=p1.next;
            }else{
                cur.next=p2;
                p2=p2.next;
            }
            cur=cur.next;
        }
       while(p1!=null ){
            cur.next=p1;
            p1=p1.next;
            cur=cur.next;
       }
        while(p2!=null ){
            cur.next=p2;
            p2=p2.next;
            cur=cur.next;
       }
       cur.next=null;
        return dummy.next;
    }
}

优先级队列:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    //把所有的节点放入 优先级队列中 然后一个个取出
    public ListNode mergeKLists(ListNode[] lists) {
        // 优先级队列,最小堆
        PriorityQueue<ListNode> pq = new PriorityQueue<>(new Comparator<ListNode>() {
        	@Override
        	public int compare(ListNode o1, ListNode o2) {
        		// TODO Auto-generated method stub
        		return o1.val-o2.val;
        	}
		});
        //取出所有链表 将每个节点入队列
       for(ListNode list:lists) {
    	  ListNode cur=list;
    	  while(cur!=null) {
    		  pq.add(cur);
    		  cur=cur.next;
    	  }
       }
       //取出所有节点
       ListNode dummy=new ListNode(-1),cur=dummy;
       while(!pq.isEmpty()) {
    	    cur.next=pq.poll();
    	   cur=cur.next;
       }
       cur.next=null;
       return dummy.next;
    }
}

总结

在这里插入图片描述

把递归算法抽象成递归树,如果递归树节点的时间复杂度和树的深度相关,那么使用分治思想对问题进行二分,就可以使递归树尽可能平衡,进而优化总的时间复杂度

相关文章:

  • 基金 word-->pdf图片模糊的解决方法
  • MyBatis调用外部静态方法
  • 飞致云开源社区月度动态报告(2025年2月)
  • 基于HTML的贪吃蛇小游戏
  • 目前主流 AI 大模型体系全解析:架构、特点与应用
  • 代码规范和简化标准
  • macos下myslq图形化工具之Sequel Ace
  • 记Android12上一个原生bug引起的system_server crash
  • Scala的match表达式的基本简单介绍
  • ​Java 实体类中的常见问题:@Data 注解与 is 前缀字段的陷阱​
  • DrissionPage动态库过(某网)滑块(100%成功)
  • LeetCode279完全平方数(动态规划)
  • LeetCode 2 - 两数相加
  • 关于博客系统的测试报告
  • 机器人学习模拟框架 robosuite (3) 机器人控制代码示例
  • 【redis】redis持久化
  • Mysql的基础命令有哪些?
  • 学习Java:全面解析构造方法重载详解
  • 如何为Java面试准备项目经验
  • JDBC、Mybatis基础知识
  • 石家庄微信网站制作/自媒体135网站免费下载安装
  • 深圳网站制作培训/南京关键词seo公司
  • 专门做搞笑视频的网站/东莞seo整站优化
  • 公司网站有收录没排名/销售怎么做
  • 矢量网站动画怎么做/互联网广告营销
  • 做百度移动网站吗/seo外包服务方案