按最近添加的顺序打印已排序队列中最频繁的元素 [英] Printing most frequent elements in a sorted queue by recently added order

查看:81
本文介绍了按最近添加的顺序打印已排序队列中最频繁的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ListTopVisitedSites(sites,5)应该返回以下输出:

ListTopVisitedSites(sites, 5) is supposed to return the following output:

www.google.com | 4  
www.aol.com | 3  
www.microsoft.com | 3  
www.amazon.com | 3  
www.facebook.com | 3  

我正在尝试打印前5个元素。如果多个元素的数量相同,则应按新近度-(最近添加)对其进行排序。另外,我需要打印一个没有值的字符串类型的空数组。

I am trying to print the top 5 elements. If multiple elements have the same quantity, they should be ordered by recency - (recently added). Also, I need to print an empty array of type string for no value.

我缺少哪一部分,或者编码不正确?所有方法及其参数都应保持不变,因为我应该将时间复杂度保持为N 2 并将空间复杂度保持为1。

Which part am I missing, or have coded incorrectly? All the methods and their parameters should remain the same, as I am supposed to keep the time complexity as N2 and space complexity as 1.

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
class SiteStats {

    private String url;
    private int numVisits;


    public SiteStats(String url, int numVisits) {
        this.url = url;
        this.numVisits = numVisits;
    }


    public int getNumVisits() {
        return this.numVisits;
    }

    public String getUrl() {
        return this.url;
    }

    public void setNumVisits(int updatedNumVisits) {
        this.numVisits = updatedNumVisits;
    }

    public String toString() {
        return this.url + " | " + this.numVisits;
    }

}

public class PartBSolution {

    private static Queue<SiteStats> sites = new LinkedList<SiteStats>();



    public static void listTopVisitedSites(Queue<SiteStats> sites, int n) {

        sortQueue(sites);

        while(sites.isEmpty()== false)
        {
            System.out.println(sites.peek() + " ");
            sites.poll();
        }

    }
    public static void insertMaxToRear(Queue<SiteStats> sites,
                                       int max_index)
    {
        SiteStats max_value = null;
        int s = sites.size();
        for (int i = 0; i < s; i++)
        {
            SiteStats current = sites.peek();
            sites.poll();
            if (i != max_index)
                sites.add(current);
            else
                max_value = current;
        }
        sites.add(max_value);
    }

    public static void sortQueue(Queue<SiteStats> sites)
    {
        for(int i = 1; i <= sites.size(); i++)
        {
            int max_index = maxIndex(sites,sites.size() - i);
            insertMaxToRear(sites, max_index);
        }
    }

    public static int maxIndex(Queue<SiteStats> sites,
                               int sortIndex)
    {
        int max_index = -1;
        int max_value = 0;
        int s = sites.size();
        for (int i = 0; i < s; i++)
        {
            SiteStats current = sites.peek();

            sites.poll();
            if (current.getNumVisits() >= max_value && i <= sortIndex)
            {
                max_index = i;
                max_value = current.getNumVisits();
            }
            sites.add(current);
        }
        return max_index;
    }


    public static void updateCount(String url) {
        boolean flag=false;
        int size2=sites.size();
        for(int i = 0; i < size2 ; i++)
        {
            SiteStats temp=sites.peek();
            sites.poll();
            if(temp.getUrl().equals(url))
            {
                temp.setNumVisits(temp.getNumVisits()+1);
                flag=true;
                sites.add(temp);
                break;
            }
            sites.add(temp);
        }
        if(!flag)
            sites.add(new SiteStats(url,1));

    }

    public static void main(String[] args) {
        String[] visitedSites = { "www.google.com", "www.google.com", "www.facebook.com", "www.aol.com", "www.google.com", "www.youtube.com",
                "www.facebook.com", "www.aol.com", "www.facebook.com", "www.google.com", "www.microsoft.com", "www.9gag.com", "www.netflix.com",
                "www.netflix.com", "www.9gag.com", "www.microsoft.com", "www.amazon.com", "www.amazon.com", "www.uber.com", "www.amazon.com",
                "www.microsoft.com", "www.aol.com" };

        for (String url : visitedSites) {
            updateCount(url);
        }
        listTopVisitedSites(sites, 5);

    }

}


/**
www.google.com | 4
www.aol.com | 3
www.microsoft.com | 3
www.amazon.com | 3
www.facebook.com | 3
*/


推荐答案

您没有写listTopVisitedSites的参数n的逻辑。

You did not write the logic for parameter n of listTopVisitedSites. Please find the updated one.

   public static void listTopVisitedSites(Queue<SiteStats> sites, int n) {
        sortQueue(sites);
        int iterate = 1;
        while (sites.isEmpty() == false && iterate <= n) {
            System.out.println(sites.peek() + " ");
            sites.poll();
            iterate++;
        }

    }

这篇关于按最近添加的顺序打印已排序队列中最频繁的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆