1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 01:49:41 +01:00
LeetCode/problems/minimum-moves-to-reach-target-score.cpp
Matej Focko 6c3cfcd876
chore: reorganize files
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-07-23 12:53:31 +02:00

33 lines
670 B
C++

#include <cassert>
class Solution {
public:
int minMoves(int target, int maxDoubles) {
int moves;
for (moves = 0; target > 1 && maxDoubles > 0; moves++) {
if (target % 2 == 0) {
maxDoubles--;
target /= 2;
} else {
target--;
}
}
// remaining increments
moves += target - 1;
return moves;
}
};
int main() {
Solution s;
assert(s.minMoves(5, 0) == 4);
assert(s.minMoves(19, 2) == 7);
assert(s.minMoves(10, 4) == 4);
assert(s.minMoves(766972377, 92));
assert(s.minMoves(1000000000, 5));
return 0;
}