大堆求解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;
        }
    }
      
}

全部评论

相关推荐

粗心的雪碧不放弃:纯学历问题,我这几个月也是一直优化自己的简历,后来发现优化到我自己都觉得牛逼的时候,发现面试数量也没有提升,真就纯学历问题
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务