DP

LC403. Frog Jump

Posted by freeCookie🍪 on January 13, 2017

LC403. Frog Jump

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.

Given a list of stones’ positions (in units) in sorted ascending order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit.

If the frog’s last jump was k units, then its next jump must be either k - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction.

Note:

  • The number of stones is ≥ 2 and is < 1,100.
  • Each stone’s position will be a non-negative integer < 231.
  • The first stone’s position is always 0.

Example 1:

[0,1,3,5,6,8,12,17]

There are a total of 8 stones.
The first stone at the 0th unit, second stone at the 1st unit,
third stone at the 3rd unit, and so on...
The last stone at the 17th unit.

Return true. The frog can jump to the last stone by jumping 
1 unit to the 2nd stone, then 2 units to the 3rd stone, then 
2 units to the 4th stone, then 3 units to the 6th stone, 
4 units to the 7th stone, and 5 units to the 8th stone.

Example 2:

[0,1,2,3,4,8,9,11]

Return false. There is no way to jump to the last stone as 
the gap between the 5th and 6th stone is too large.

求🐸能不能跳到最后一块石头上。很明显用DP来解嘛。

public class Solution {
    public boolean canCross(int[] stones) {
        ArrayList<Integer> stone = new ArrayList<Integer>();
        for(int s: stones)  stone.add(s);
        return helper(stone, 0, 1, stones.length);
    }
    
    private boolean helper(List<Integer> stones, int pos, int step, int len){
        if(pos + step == stones.get(len - 1)){
            return true;
        }else if(stones.contains(pos + step) && stones.indexOf(pos + step) > stones.indexOf(pos)){
            return helper(stones, pos + step, step, len) || helper(stones, pos + step, step + 1, len) || helper(stones, pos + step, step - 1, len);
        }else{
            return false;
        }
    }
}

这种naiveDP不TLE才怪了。。。挫败

所以用map来存一下呢?

public class Solution {
    public boolean canCross(int[] stones) {
        ArrayList<Integer> stone = new ArrayList<Integer>();
        for(int s: stones)  stone.add(s);
        HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
        helper(stone, 0, 1, map);
        return map.containsKey(stones[stones.length-1]) && map.get(stones[stones.length-1]) == true;
    }
    
    private boolean helper(List<Integer> stones, int pos, int step,  HashMap<Integer, Boolean> map){
        if(map.containsKey(step + pos) && map.get(step+pos) != null)    return map.get(step + pos);
        if(pos + step == stones.get(stones.size() - 1)){
            map.put(step + pos, true);
            return true;
        }else if(stones.contains(pos + step)){
            boolean t = (helper(stones, pos + step, step, map) || helper(stones, pos + step, step + 1, map)); 
            if(step - 1 > 0) t = t || helper(stones, pos + step, step - 1, map);
            if(t) map.put(step + pos, t);
            return t;
        }else{
            map.put(step + pos, false);
            return false;
        }
    }
}

还是超时_(:з」∠)_

Java solution use hashmap

Map里存每个位置可以向后跳的步数,每次都更新下一个可到达位置的step,同时查找是否到达终点。

public class Solution {
    public boolean canCross(int[] stones) {
        if(stones == null || stones.length == 0)  return false;
        Map<Integer, Set<Integer>> steps = new HashMap<Integer, Set<Integer>>();
        steps.put(stones[0], new HashSet<Integer>());
        steps.get(stones[0]).add(1);
        for(int i = 1; i < stones.length; i++){
            steps.put(stones[i], new HashSet<Integer>());
        }
        for(int i = 0; i < stones.length - 1; i++){
            int s = stones[i];
            for(int step: steps.get(s)){
                int next = s + step;
                if(next == stones[stones.length - 1])   return true;
                Set<Integer> list = steps.get(next);
                if(list != null){
                    list.add(step);
                    if(step - 1 > 0)    list.add(step - 1);
                    list.add(step + 1);
                }
            }
        }
        return false;
    }
}

O(n), O(n)