1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/contest/weekly-contest-276/minimum-moves-to-reach-target-score.cpp
Matej Focko 5d50c507d8
contest(weekly-contest-276): add solutions
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-01-19 21:08:27 +01: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;
}