题目:

有N个长度不一的数组,所有的数组都是有序的,请从大到小打印这N个数组整体最大的前K个数。

例如:

输入含有N行元素的二维数组代表N个一维数组。

219,405,538,845,971

148,558

52,99,348,691

再输入整数K=5,则打印:

Top 5:971,845,961,558,538。

要求:

1.如果所有数组的元素个数小于K,则从小到大打印所有的数。

2.时间复杂度为O(KlogN)。

解答:

利用堆结构和堆排序完成。

  1. import java.util.Arrays;
  2.  
  3. public class Problem_20_PrintMaxTopK {
  4.  
  5. 	public static class HeapNode {
  6.  
  7. 		public int value;
  8.  
  9. 		public int arrNum;
  10.  
  11. 		public int index;
  12.  
  13. 		public HeapNode(int value, int arrNum, int index) {
  14.  
  15. 			this.value = value;
  16.  
  17. 			this.arrNum = arrNum;
  18.  
  19. 			this.index = index;
  20.  
  21. 		}
  22.  
  23. 	}
  24.  
  25. 	public static void printTopK(int[][] matrix, int topK) {
  26.  
  27. 		int heapSize = matrix.length;
  28.  
  29. 		HeapNode[] heap = new HeapNode[heapSize];
  30.  
  31. 		for (int i = 0; i != heapSize; i++) {
  32.  
  33. 			int index = matrix[i].length - 1;
  34.  
  35. 			heap[i] = new HeapNode(matrix[i][index], i, index);
  36.  
  37. 			heapInsert(heap, i);
  38.  
  39. 		}
  40.  
  41. 		System.out.println("TOP " + topK + " : ");
  42.  
  43. 		for (int i = 0; i != topK; i++) {
  44.  
  45. 			if (heapSize == 0) {
  46.  
  47. 				break;
  48.  
  49. 			}
  50.  
  51. 			System.out.print(heap[0].value + " ");
  52.  
  53. 			if (heap[0].index != 0) {
  54.  
  55. 				heap[0].value = matrix[heap[0].arrNum][--heap[0].index];
  56.  
  57. 			} else {
  58.  
  59. 				swap(heap, 0, --heapSize);
  60.  
  61. 			}
  62.  
  63. 			heapify(heap, 0, heapSize);
  64.  
  65. 		}
  66.  
  67. 	}
  68.  
  69. 	public static void heapInsert(HeapNode[] heap, int index) {
  70.  
  71. 		while (index != 0) {
  72.  
  73. 			int parent = (index - 1) / 2;
  74.  
  75. 			if (heap[parent].value < heap[index].value) {
  76.  
  77. 				swap(heap, parent, index);
  78.  
  79. 				index = parent;
  80.  
  81. 			} else {
  82.  
  83. 				break;
  84.  
  85. 			}
  86.  
  87. 		}
  88.  
  89. 	}
  90.  
  91. 	public static void heapify(HeapNode[] heap, int index, int heapSize) {
  92.  
  93. 		int left = index * 2 + 1;
  94.  
  95. 		int right = index * 2 + 2;
  96.  
  97. 		int largest = index;
  98.  
  99. 		while (left < heapSize) {
  100.  
  101. 			if (heap[left].value > heap[index].value) {
  102.  
  103. 				largest = left;
  104.  
  105. 			}
  106.  
  107. 			if (right < heapSize && heap[right].value > heap[largest].value) {
  108.  
  109. 				largest = right;
  110.  
  111. 			}
  112.  
  113. 			if (largest != index) {
  114.  
  115. 				swap(heap, largest, index);
  116.  
  117. 			} else {
  118.  
  119. 				break;
  120.  
  121. 			}
  122.  
  123. 			index = largest;
  124.  
  125. 			left = index * 2 + 1;
  126.  
  127. 			right = index * 2 + 2;
  128.  
  129. 		}
  130.  
  131. 	}
  132.  
  133. 	public static void swap(HeapNode[] heap, int index1, int index2) {
  134.  
  135. 		HeapNode tmp = heap[index1];
  136.  
  137. 		heap[index1] = heap[index2];
  138.  
  139. 		heap[index2] = tmp;
  140.  
  141. 	}
  142.  
  143. 	public static int[][] generateRandomMatrix(int maxRow, int maxCol,
  144.  
  145. 			int maxValue) {
  146.  
  147. 		if (maxRow < 0 || maxCol < 0) {
  148.  
  149. 			return null;
  150.  
  151. 		}
  152.  
  153. 		int[][] matrix = new int[(int) (Math.random() * maxRow) + 1][];
  154.  
  155. 		for (int i = 0; i != matrix.length; i++) {
  156.  
  157. 			matrix[i] = new int[(int) (Math.random() * maxCol) + 1];
  158.  
  159. 			for (int j = 0; j != matrix[i].length; j++) {
  160.  
  161. 				matrix[i][j] = (int) (Math.random() * maxValue);
  162.  
  163. 			}
  164.  
  165. 			Arrays.sort(matrix[i]);
  166.  
  167. 		}
  168.  
  169. 		return matrix;
  170.  
  171. 	}
  172.  
  173. 	public static void printMatrix(int[][] matrix) {
  174.  
  175. 		for (int i = 0; i != matrix.length; i++) {
  176.  
  177. 			for (int j = 0; j != matrix[i].length; j++) {
  178.  
  179. 				System.out.print(matrix[i][j] + " ");
  180.  
  181. 			}
  182.  
  183. 			System.out.println();
  184.  
  185. 		}
  186.  
  187. 	}
  188.  
  189. 	public static void main(String[] args) {
  190.  
  191. 		int[][] matrix = generateRandomMatrix(5, 10, 1000);
  192.  
  193. 		printMatrix(matrix);
  194.  
  195. 		System.out.println("===========================");
  196.  
  197. 		printTopK(matrix, 100);
  198.  
  199. 	}
  200.  
  201. }
  202.  

[算法]打印N个数组的整体最大Top K的更多相关文章

  1. 《程序员代码面试指南》第八章 数组和矩阵问题 打印N 个数组整体最大的Top K

    题目 打印N 个数组整体最大的Top K java代码 package com.lizhouwei.chapter8; /** * @Description: 打印N 个数组整体最大的Top K * ...

  2. LeetCode初级算法的Python实现--数组

    LeetCode初级算法的Python实现--数组 # -*- coding: utf-8 -*- """ @Created on 2018/6/3 17:06 @aut ...

  3. 每天一道算法题(14)——N个降序数组,找到最大的K个数

     题目: 假定有20个有序数组,每个数组有500个数字,降序排列,数字类型32位uint数值,现在需要取出这10000个数字中最大的500个. 思路 (1).建立大顶堆,维度为数组的个数,这里为20( ...

  4. 算法与数据结构基础 - 数组(Array)

    数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...

  5. 使用基础知识完成java小作业?强化练习-1.输入数组计算最大值-2.输出数组反向打印-3.求数组平均值与总和-4.键盘输两int,并求总和-5.键盘输三个int,并求最值;

    完成几个小代码练习?让自己更加强大?学习新知识回顾一下基础? 1.输入数组计算最大值 2.输出数组反向打印 3.求数组平均值与总和 4.键盘输两int,并求总和 5.键盘输三个int,并求最值 /* ...

  6. 前端与算法 leetcode 189. 旋转数组

    目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单 ...

  7. 代码实现:定义一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)

    package com.loaderman.test; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; im ...

  8. 坐标轴刻度取值算法-基于魔数数组-源于echarts的y轴刻度计算需求

    本文链接:https://blog.csdn.net/qq_26909801/article/details/96966372数值型坐标轴刻度计算算法前言算法描述上代码代码运行效果结语前言因实习的公司 ...

  9. 某些时候 v-if 会导致 v-modal 内部数据获取不到 也watch不到,这个时候用v-show即可,另外提一下数组要整体赋值才有双向绑定

    某些时候 v-if 会导致 v-modal 内部数据获取不到 也watch不到,这个时候用v-show即可,另外提一下数组要整体赋值才有双向绑定

随机推荐

  1. java中不能用小数点(.)来做分隔符

    split()括号里是一个String的参数,所以一定要符合这种:split(".")形式,即点'.'要用双引号""括起来"."在java中 ...

  2. STM32F4先设置寄存器还是先使能时钟

    http://zhidao.baidu.com/link?url=gdVNuIgLOJcV37QzbCx0IrFip5pskiPQDWpoZayr_xBEe120p4d_iWtrfDl1d4tSFaH ...

  3. brew Error: Formulae found in multiple taps

    Mac PHP brew install php56-apcu Error: Formulae found in multiple taps: * homebrew/php/php56-apcu * ...

  4. nginx內建模块使用

    目录 nginx內建模块使用 1. 內建模块的引入 1.1 查看安装信息 1.2 重新指定配置信息 2. 內建模块的使用 2.1 http_stub_status_module 2.2 http_ra ...

  5. Spring Boot内嵌Tomcat session超时问题

    最近让Spring Boot内嵌Tomcat的session超时问题给坑了一把. 在应用中需要设置session超时时间,然后就习惯的在application.properties配置文件中设置如下, ...

  6. 【elasticsearch】安装合集

    [elasticsearch](1)centos7 使用yum安装elasticsearch 2.X [elasticsearch](2)centos7 超简单安装elasticsearch 的监控. ...

  7. mysql case then 语句

  8. 最小生成树——Prim(普利姆)算法

    [0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解Prim算法的idea 并用 源代码加以实现: 0.2)最小生成树的基础知识,参见 http://blog. ...

  9. ubuntu 16.04.3 安装完成后的一些初始化工作

    虚拟机安装前记得把桥接调好! 1. 重置root密码 sudo passwd, 然后系统会让你输入密码,这时输入的密码就是root用户的密码,su root切用户 2. 设置固定IP,有重启服务功能令 ...

  10. iOS 蓝牙功能 bluetooth

    现将创建蓝牙工程的要点总结一下,由于工程主要涉及中心模式,所以只总结中心模式的用法 1,引入CoreBluetooth.framework 2,实现蓝牙协议,如: .h文件如下 @protocol C ...