打印按照最近添加的顺序排序的队列中最常见的元素

huangapple go评论71阅读模式
英文:

Printing most frequent elements in a sorted queue by recently added order

问题

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

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

我正在尝试打印前5个元素。如果多个元素具有相同的数量,它们应该按照最近性 - (最近添加)排序。此外,对于没有值的情况,我需要打印一个空的字符串类型数组。

我缺失或编写错误的部分是什么?所有方法及其参数应保持不变,因为我应该将时间复杂度保持为N²,空间复杂度保持为1。

英文:

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  

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.

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 N<sup>2</sup> 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 + &quot; | &quot; + this.numVisits;
    }

}

public class PartBSolution {

    private static Queue&lt;SiteStats&gt; sites = new LinkedList&lt;SiteStats&gt;();



    public static void listTopVisitedSites(Queue&lt;SiteStats&gt; sites, int n) {

        sortQueue(sites);

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

    }
    public static void insertMaxToRear(Queue&lt;SiteStats&gt; sites,
                                       int max_index)
    {
        SiteStats max_value = null;
        int s = sites.size();
        for (int i = 0; i &lt; 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&lt;SiteStats&gt; sites)
    {
        for(int i = 1; i &lt;= sites.size(); i++)
        {
            int max_index = maxIndex(sites,sites.size() - i);
            insertMaxToRear(sites, max_index);
        }
    }

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

            sites.poll();
            if (current.getNumVisits() &gt;= max_value &amp;&amp; i &lt;= 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 &lt; 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 = { &quot;www.google.com&quot;, &quot;www.google.com&quot;, &quot;www.facebook.com&quot;, &quot;www.aol.com&quot;, &quot;www.google.com&quot;, &quot;www.youtube.com&quot;,
                &quot;www.facebook.com&quot;, &quot;www.aol.com&quot;, &quot;www.facebook.com&quot;, &quot;www.google.com&quot;, &quot;www.microsoft.com&quot;, &quot;www.9gag.com&quot;, &quot;www.netflix.com&quot;,
                &quot;www.netflix.com&quot;, &quot;www.9gag.com&quot;, &quot;www.microsoft.com&quot;, &quot;www.amazon.com&quot;, &quot;www.amazon.com&quot;, &quot;www.uber.com&quot;, &quot;www.amazon.com&quot;,
                &quot;www.microsoft.com&quot;, &quot;www.aol.com&quot; };

        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
*/

答案1

得分: 1

传入listTopVisitedSites(sites, 5);的参数 n 不再被使用,因此不能指望它只列出5个。

英文:

The parameter n that you pass into listTopVisitedSites(sites, 5); is never used again, so you can not expect it to only list the 5

答案2

得分: 1

你没有编写 listTopVisitedSites 函数的参数 n 的逻辑。请找到更新后的代码。

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

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

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

    }

huangapple
  • 本文由 发表于 2020年8月28日 11:13:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63626935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定