[AlgoExpert] Min Number Of Jumps

Min Number Of Jumps

  • Time : O(n)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <vector>
using namespace std;

int minNumberOfJumps(vector<int> array) {
int ma = 0, res = 0, n = array.size(), jump = 0;
for(int i = 0; i < n - 1; i++) {
ma = max(ma, i + array[i]);
if(jump == i) {
res++;
jump = ma;
}
}

return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/11/PS/AlgoExpert/min-number-of-jumps/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.