Does anyone could offer me a test case (size is smaller than around 20) that will fail my algorithm? Thank you.


  • -1
    F

    I didn't find any logic errors. And it passed many cases that I can think of. But when I submit it, it cannot pass a very long test case with just a few differences from the answer. I don't know what's wrong with my algorithms. Or maybe there will be multiple correct answers for this problmes.

    public class Solution {
        public int[][] reconstructQueue(int[][] people) {
            Arrays.sort(people, new Comparator<int[]>() {
                @Override
                public int compare(int[] p1, int[] p2) {
                    final Integer h1 = p1[0];
                    final Integer h2 = p2[0];
                    final Integer h11 = p1[1];
                    final Integer h22 = p2[1];
                    if (h1 != h2) {
                        return h1.compareTo(h2);
                    }
                    else {
                        return h11.compareTo(h22);
                    }
                }
            });
            
            int right = people.length - 1;
            int left = right;
            while (left >= 0) {
                while (left >= 0 && people[left][0] == people[right][0]) {
                    left--;
                }
                left++;
                
                for (int i = right; i >= left; i--) {
                    if (people[i][1] > i - left) {
                        moveNext(people, i, people[i][1] - (i - left));
                    }
                }
                
                right = left - 1;
                left = right;
            }
            
            return people;
            
        }
        
        
        
        private void moveNext(int[][] people, int index, int step) {
            int[] temp = people[index];
            for (int i = index; i < index + step; i++) {
                people[i] = people[i + 1];
            }
            people[index + step] = temp;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.