Add Minimum Number of Rungs LeetCode solution in C++

1
class Solution {
2
public:
3
int addRungs(vector<int> &rungs, int dist) {
4
// to keep the track of the number of extra rung to be added
5
long long int count = 0;
6

7
// our curr pos at the beggining
8
long long int currpos = 0;
9

10
// to keep the track of the next pos to be climed
11
long long int nextposidx = 0;
12

13
while (true) {
14
if (currpos == rungs[rungs.size() - 1]) {
15
break;
16
}
17

18
if ((rungs[nextposidx] - currpos) <= dist) {
19
currpos = rungs[nextposidx];
20
nextposidx++;
21
} else {
22
// cout<<"hello"<<endl;
23
long long int temp = (rungs[nextposidx] - currpos);
24
// cout<<"temp = "<<temp<<endl;
25

26
if ((temp % dist) == 0) {
27
long long int val = temp / dist;
28
count = count + (val - 1);
29
} else {
30
long long int val = floor(((temp * 1.00) / (dist * 1.00)));
31
count = count + (val);
32
}
33
currpos = rungs[nextposidx];
34
}
35
}
36
return count;
37
}
38
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0