DP动态规划专题七 :LeetCode 403. Frog Jump

LeetCode 403. 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.

我的解法:由于每个stone出发可以跳3种情况,因此每个stone都可以由不同的step跳过来,我们要记录每个stone可以不可以被跳到以及可能跳过来的step,这样才可以覆盖到全部之后可以跳到的stones,因此我用了一个hashmap来存每个stone及跳到这个stone的可能的step。最后判断最后一个hashset里是否有step即可。

版本1:

    public boolean canCross(int[] stones) {
        if (stones.length <= 1) return true;
        if (stones.length == 2 && stones[1] != 1) return false;
        if (stones.length == 2 && stones[1] == 1) return true;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < stones.length; i++) {
            map.put(stones[i],i);
        }
        List<HashSet<Integer>> list = new ArrayList<>();
        for (int i = 0 ; i < stones.length; i++) {
            HashSet<Integer> cur = new HashSet<>();
            list.add(cur);
        }
        list.get(0).add(1);
        for (int i = 0; i < stones.length - 1; i++) {
            for (Integer step : list.get(i)) {
                for (int j = -1; j <= 1; j++) {
                    if (step + j > 0) {
                        int stone = stones[i] + step + j;
                        //System.out.println("stone: " + stone);
                        if (map.containsKey(stone)) {
                            int pos = map.get(stone);
                            list.get(pos).add(step + j);
                            //System.out.println(list.get(pos));
                        }
                    } 
                    
                }
            }
        }
        return list.get(list.size() - 1).size() == 0 ? false : true;
    }

优化后的版本1:

    public boolean canCross(int[] stones) {
        if (stones.length <= 1) return true;
        if (stones.length == 2 && stones[1] != 1) return false;
        if (stones.length == 2 && stones[1] == 1) return true;
        HashMap<Integer, HashSet<Integer>> map = new HashMap<>();
        for (int i = 0; i < stones.length; i++) {
            map.put(stones[i],new HashSet<>());
        }
        map.get(0).add(1);
        for (int i = 0; i < stones.length - 1; i++) {
            HashSet<Integer> set = map.get(stones[i]);
            for (Integer step : set) {
                for (int j = -1; j <= 1; j++) {
                    if (step + j > 0) {
                        int stone = stones[i] + step + j;
                        if (map.containsKey(stone)) {
                            map.get(stone).add(step + j);
                        }
                    }   
                }
            }
        }
        return map.get(stones[stones.length - 1]).size() == 0 ? false : true;
    }

版本2:优化版,即:每次都先尝试走的最快的,如果不行,再走第二快的,依次下去,如果都不行,那么之后每次走到这一步就直接返回false.

public boolean canCross(int[] s) {
        Map<Integer, Integer> m = new HashMap<>();
        List<Set<Integer>> l = new ArrayList<>();
        for(int i = 0; i < s.length; i++){
            l.add(new HashSet<>());
            m.put(s[i], i);
        }
        return run(s, 0, 1, l, m);
    }
    
    public boolean run(int[] s, int i, int v, List<Set<Integer>> l, Map<Integer, Integer> m){
        if(i == s.length-1)return true;
        if(l.get(i).contains(v))return false;
        if(i!=0 && m.containsKey(s[i]+v+1) && run(s, m.get(s[i]+v+1), v+1, l, m))return true;
        if(m.containsKey(s[i]+v) && run(s, m.get(s[i]+v), v, l, m))return true;
        if(v > 1 && m.containsKey(s[i]+v-1) && run(s, m.get(s[i]+v-1), v-1, l, m))return true;
        l.get(i).add(v);
        return false;
    }

猜你喜欢

转载自blog.csdn.net/katrina95/article/details/85387307