[InterviewBit] Min Jumps Array

Min Jumps Array

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int Solution::jump(vector<int> &A) {
if(A.size() == 1) return 0;
int ma = A[0], now = 0, res = 0;
while(now < A.size() - 1 and now <= ma) {
if(ma >= A.size() - 1) return res + 1;
int jump = -1;
for(int i = now; i <= ma and i < A.size(); i++) jump = max(jump, i + A[i]);
if(ma >= jump and ma != A.size() - 1) return -1;
now = ma;
ma = jump;
res++;
}
return -1;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/11/PS/interviewbit/min-jumps-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.