Johnson Trotter Algorithm

前端 未结 4 661
醉酒成梦
醉酒成梦 2020-12-06 23:53

Overview:

I am trying to implement the Johnson Trotter Algorithm in Java so that I can solve a problem on Project Euler. I have looked and looked bu

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 00:11

    Although you are not posting the complete code here (how you decide if an element is mobile or immobile would be helpful), I suspect your error comes from here:

     public int getLargestMobileIndex(ArrayList elements)
          {
             int maxIndex = 0;
    
             for(int i = 0; i < elements.size(); i++)
             {
                if(elements.get(i).isMobile() && i > maxIndex) //<---------- It seems that 
                // you should compare the i-th element to the maxIndex-th element, not i and
                // maxIndex
                {
                   maxIndex = i;
                }
             }
    
             return maxIndex;
          }
    

    Since the algorithm said find the largest mobile element K.

    Also, I suspect there are problems for your isMobile method, but cannot be sure.

    Hope this helps!

提交回复
热议问题