← Home

1345. Jump Game IV

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
graph traversalC++Markdown
134

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1345. Jump Game IV, the solution in this repository is mainly a graph traversal solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: graph traversal.

The notes already sitting in the source point us in the right direction:

  • BFS

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are minJumps, visited.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • The queue gives the solution a level-by-level or frontier-style traversal.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//BFS
02//Runtime: 492 ms, faster than 24.29% of C++ online submissions for Jump Game IV.
03//Memory Usage: 61.6 MB, less than 18.84% of C++ online submissions for Jump Game IV.
04class Solution {
05public:
06    int minJumps(vector<int>& arr) {
07        unordered_map<int, unordered_set<int>> val2idx;
08        int n = arr.size();
09        
10        for(int i = 0; i < n; ++i){
11            val2idx[arr[i]].insert(i);
12        }
13        
14        int cur = 0, jumps = 0;
15        queue<int> q;
16        q.push(cur);
17        
18        vector<int> visited(n, false);
19        visited[cur] = true;
20        
21        while(!q.empty()){
22            int level_size = q.size();
23            
24            while(level_size-- > 0){
25                cur = q.front(); q.pop();
26                if(cur == n-1) return jumps;
27                
28                if(cur+1 < n && !visited[cur+1]){
29                    visited[cur+1] = true;
30                    q.push(cur+1);
31                }
32                
33                if(cur-1 >= 0 && !visited[cur-1]){
34                    visited[cur-1] = true;
35                    q.push(cur-1);
36                }
37                
38                //unordered_map<int, vector<int>> val2idx;
39                //TLE
40                // for(int j : val2idx[arr[cur]]){
41                //     if(!visited[j]){
42                //         visited[j] = true;
43                //         q.push(j);
44                //     }
45                // }
46                
47                for(auto it = val2idx[arr[cur]].begin(); it != val2idx[arr[cur]].end(); ){
48                    if(!visited[(*it)]){
49                        visited[(*it)] = true;
50                        q.push((*it));
51                        it = val2idx[arr[cur]].erase(it);
52                    }else{
53                        ++it;
54                    }
55                }
56            }
57            
58            
59            ++jumps;
60        }
61        
62        return jumps;
63    }
64};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.