Why Leetcode doesn't support SortedSet?


  • 0
    A

    My code passed, but I got error when using SortedSet in my code; don't know why. Instead, I have to find a workaround. Anyway I can add an import statement in my solution?

    public boolean canJump(int[] A) {
            // edge case
            if (A.length <= 1)
                return true;
            
            TreeSet<Integer> remainIndices = new TreeSet<Integer>();
            for (int i = 0; i < A.length; i++)
            	remainIndices.add(i);
            
            Deque<Integer> indices = new LinkedList<Integer>();
            Integer target = A.length - 1;
            indices.add(0);
            remainIndices.remove(0);
            while (!indices.isEmpty()) {
                int index = indices.pollFirst();
                int value = A[index];
                if (value == 0)
                	continue;
                int lower = index;
                int upper = index + value;
                Set<Integer> nextIndices = remainIndices.subSet(lower, upper + 1);
                if (nextIndices.isEmpty())
                	continue;
                if (nextIndices.contains(target))
                	return true;
                else {
                	Iterator<Integer> itr = nextIndices.iterator();
                	while (itr.hasNext()) {
                		indices.addLast(itr.next());
                		itr.remove();
                	}
                }
            }
            
            return false;
        }

Log in to reply
 

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