大堆求解topK

打印N个数组整体最大的Top K

http://www.nowcoder.com/questionTerminal/5727b69bf80541c98c06ab90cf4c509e

import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); 
        String[] str = sc.readLine().split(" ");
        int length = Integer.parseInt(str[0]);
        int topk = Integer.parseInt(str[1]);
        
        int[][] matrix = new int[length][];
        for(int i = 0; i<length ; i++){
            String[] str2 = sc.readLine().split(" ");
            matrix[i] = new int[str2.length];
            for(int j = 0 ; j< str2.length ; j++){
                matrix[i][j] = Integer.parseInt(str2[j]);
            }
        }
        
        printTopK(matrix,topk);
    }
    
    public static void printTopK(int[][] matrix, int topK){
        int heapSize = matrix.length;
        HeapNode[] heap = new HeapNode[heapSize];
        for(int i=0 ; i< heapSize ; i++){
            int maxIndex = matrix[i].length-1;
            heap[i] = new HeapNode(matrix[i][maxIndex],i,maxIndex);
            heapInsert(heap,i);
        }
        
        for(int i = 0 ;i< topK ; i++){
            if(heapSize==0){
                break;
            }
            System.out.print(heap[0].value+" ");
            if(heap[0].index!=0){
                heap[0].value = matrix[heap[0].arrayNum][--heap[0].index];
            }
            else{
                swap(heap,0,--heapSize);
            }
            heapfy(heap,0,heapSize);
        }
        
    }
    
    public static void heapfy(HeapNode[] heap, int index, int heapSize){
        int left = 2*index+1;
        int right = 2*index +2;
        int larger = index;
        while(left<heapSize){
            if(heap[left].value > heap[index].value){
                larger = left;
            }
            
            if(right<heapSize&& heap[right].value>heap[larger].value){
                larger = right;
            }
            if(larger!=index){
                swap(heap,index,larger);
            }
            else{
                break;
            }
            index = larger ;
            left = 2*index +1 ;
            right = 2*index +2;
        }
    }
    
    
    public static void heapInsert(HeapNode[] heap, int index){
        while(index!=0){
            int parent = (index-1)/2;
            if(heap[parent].value < heap[index].value){
                swap(heap,parent,index);
                index = parent; 
            }
            else{
                break;
            }
        }
    }
    
    
    
    
    public static void swap(HeapNode[] heap, int index1, int index2){
        HeapNode temp = heap[index1];
        heap[index1] = heap[index2];
        heap[index2] = temp;
    }
    
    
    
    public static class HeapNode{
        public int value ;
        public int arrayNum;
        public int index;
        
        public HeapNode(int value,int arrayNum, int index){
            this.value = value ;
            this.arrayNum = arrayNum;
            this.index = index;
        }
    }
      
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
10-05 10:13
已编辑
HHHHaos:让这些老登来现在秋招一下,简历都过不去
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务