We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here is the same algorithm, without any duplicated/copied code (uses the main method as given); I think this is among the smallest if not the smallest Java solution on here (could be even smaller if comments and extra readability lines removed); passes all tests:
staticintequalStacks(int[]queue0,int[]queue1,int[]queue2){int[][]arrays=newint[][]{queue0,queue1,queue2};LinkedList<Integer>[]queues=newLinkedList[]{newLinkedList<>(),newLinkedList<>(),newLinkedList<>()};int[]heights=newint[]{0,0,0};// Load queues & Count starting heightsfor(intcurQueueNum=0;curQueueNum<=2;curQueueNum++){for(intcurInt:arrays[curQueueNum]){queues[curQueueNum].add(curInt);heights[curQueueNum]+=curInt;}}// Pop queues (in order of tallest) while updating heights,// until heights equalwhile(heights[0]!=heights[1]||heights[1]!=heights[2]){// heights not equal yetintmaxHeight=Math.max(heights[0],Math.max(heights[1],heights[2]));for(intcurQueueNum=0;curQueueNum<=2;curQueueNum++){if(heights[curQueueNum]==maxHeight){// Pop tallest queue & shorten its heightheights[curQueueNum]-=queues[curQueueNum].poll();break;}}}returnheights[0];}
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
Here is the same algorithm, without any duplicated/copied code (uses the main method as given); I think this is among the smallest if not the smallest Java solution on here (could be even smaller if comments and extra readability lines removed); passes all tests: