7 lines Java


  • 0
    W

    8 lines dp

    public boolean canCross(int[] stones) {
      Set<Integer> set = Arrays.stream(stones).boxed().collect(Collectors.toSet()), empty = new HashSet<>();
      Map<Integer, Set<Integer>> map = new HashMap<>();
      for (int start : stones)
        for (int lastStep : start == 0 ? Arrays.asList(0) : map.getOrDefault(start, empty))
          for (int i = 0, next = start + --lastStep; i++ < 3; next++)
            if (next > start && set.contains(next)) map.computeIfAbsent(next, k -> new HashSet<>()).add(next - start);
      return !map.getOrDefault(stones[stones.length - 1], empty).isEmpty();
    }
    

Log in to reply
 

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