403. Frog Jump Hard

@problem@discussion
#Array#Dynamic Programming



1/**
2 * [403] Frog Jump
3 *
4 * A frog is crossing a river. The river is divided into some number of 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.
5 * Given a list of stones' positions (in units) in sorted ascending order, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be 1 unit.
6 * If the frog's last jump was k units, its next jump must be either k - 1, k, or k + 1 units. The frog can only jump in the forward direction.
7 *  
8 * Example 1:
9 *
10 * Input: stones = [0,1,3,5,6,8,12,17]
11 * Output: true
12 * Explanation: 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.
13 *
14 * Example 2:
15 *
16 * Input: stones = [0,1,2,3,4,8,9,11]
17 * Output: false
18 * Explanation: There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large.
19 *
20 *  
21 * Constraints:
22 *
23 * 2 <= stones.length <= 2000
24 * 0 <= stones[i] <= 2^31 - 1
25 * stones[0] == 0
26 * stones is sorted in a strictly increasing order.
27 *
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/frog-jump/
32// discuss: https://leetcode.com/problems/frog-jump/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36use std::collections::HashMap;
37use std::collections::HashSet;
38
39impl Solution {
40    pub fn can_cross(stones: Vec<i32>) -> bool {
41        if stones.is_empty() {
42            return true;
43        }
44
45        let mut map: HashMap<usize, HashSet<i32>> = HashMap::new();
46        map.insert(0, HashSet::new());
47        map.entry(0).and_modify(|x| {
48            x.insert(1);
49        });
50        for i in 1..stones.len() {
51            map.insert(stones[i] as usize, HashSet::new());
52        }
53
54        for i in 0..stones.len() - 1 {
55            let steps = map.get(&(stones[i] as usize));
56            if steps.is_none() {
57                continue;
58            }
59            for step in steps.unwrap().clone() {
60                let reach = step + stones[i];
61                if reach == stones[stones.len() - 1] {
62                    return true;
63                }
64
65                map.entry(reach as usize).and_modify(|x| {
66                    x.insert(step);
67                    if step > 1 {
68                        x.insert(step - 1);
69                    }
70                    x.insert(step + 1);
71                });
72            }
73        }
74
75        false
76    }
77}
78
79// submission codes end
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn test_403() {
87        assert!(Solution::can_cross(vec![0, 1, 3, 5, 6, 8, 12, 17]));
88    }
89}
90


Back
© 2025 bowen.ge All Rights Reserved.