我如何解决这个java编程挑战,包括内存需求最少的队列?

我如何解决这个java编程挑战,包括内存需求最少的队列?,java,eclipse,algorithm,queue,Java,Eclipse,Algorithm,Queue,因此,我有一个编程挑战,我有一个队列中的人员列表,每当一个人到达第一个位置时,它就会加倍并到达队列的末尾 例如,我有x,y,z x、 下一个循环是 y、 z,x,x,z,x,x,y,y,x,x,y,y,z,x,y,y,y,z,x,x,x,x,x,x,x,x,x,x,x,x等 这是我在eclipse中完美运行的代码 public class Line { static Node first; static Node last; static int N; private

因此,我有一个编程挑战,我有一个队列中的人员列表,每当一个人到达第一个位置时,它就会加倍并到达队列的末尾

例如,我有x,y,z

x、 下一个循环是 y、 z,x,x,z,x,x,y,y,x,x,y,y,z,x,y,y,y,z,x,x,x,x,x,x,x,x,x,x,x,x等

这是我在eclipse中完美运行的代码

public class Line {

static Node first;
    static Node last;
    static int N;

    private static class Node
    {
        String name;
        Node next;
    }

    public static boolean isEmpty() { return first == null; }

    public static void enqueue(String name)
    {
        Node oldlast = last;
        last = new Node();
        last.name = name;
        last.next = oldlast;
        if(isEmpty()) first = last;
        else oldlast.next = last;
        N++;
    }

    public static String dequeue()
    {
        String name = first.name;
        first = first.next;
        N--;
        if(isEmpty()) last = null;
        return name;
    }

    public static String WhoIsNext(String[] names, int n)
    {
        // Your code is here...

        for(int i = 0; i < names.length; i++) enqueue(names[i]);

        for(int i = 0; i < n; i++)
        {
            String name = dequeue();
            enqueue(name);
            enqueue(name);
        }

        return last.name;
    }
}

您不应该使用简单的算法来解决这个问题。正如您在test20中所看到的,当n=100000000时,解决问题的成本太高


你可以用纯数学来解决这个问题。例如,将x、y、z分成(xyz)(xxyyz)(xxxyyzzz)这样的组,并减少每个组的数量以获得特定的组,然后mod n(根据您找到的组)以确定应该是哪个输出。

我认为您不能在队列中创建这么多字符串对象

您可以使用整数队列,其值为persons数组的索引

编辑:

在看到url上的问题后,我认为你真的不需要移动这个人

  • 把问题分成几个小步骤,每个人都能解决

  • 队列中每个人的数量在每一步后增加两倍

  • 最后一步不能处理所有的人,所以要计算该人的指数

  • 以下是可接受的代码:

    public class Line {
        public static String WhoIsNext(String[] names, int n)
        {
            // Your code is here...
            final int personNum = names.length;
            int loop = 1;
            int curPersonQueueNum = personNum * loop;
            while (n > curPersonQueueNum){
                n -= curPersonQueueNum;
                loop*=2;
                curPersonQueueNum = personNum * loop;
            }
            n = (int) Math.ceil((double)n / loop);
            return names[n - 1];
        }
    }
    

    @FallAndLearn让我们假设一个队列中有3个人,n是我们需要做的出队次数或队列中的移动次数,所以如果n为0,它保持不变,当n为1时,现在是y,那么如果n为2,那么现在是z,如果是3,那么x,y,z,依此类推。我们需要返回给定n的队列中最后一个人的姓名。同样,如果他们给我们n=2,我们会得到这样的队列z x y,因此我们返回y的名称,因为y是队列中的最后一项。
    public class Line {
        public static String WhoIsNext(String[] names, int n)
        {
            // Your code is here...
            final int personNum = names.length;
            int loop = 1;
            int curPersonQueueNum = personNum * loop;
            while (n > curPersonQueueNum){
                n -= curPersonQueueNum;
                loop*=2;
                curPersonQueueNum = personNum * loop;
            }
            n = (int) Math.ceil((double)n / loop);
            return names[n - 1];
        }
    }