【Leetcode】NO.5194 得到目标值的最少行动次数(Python) [周赛]
2022/1/16 22:07:22
本文主要是介绍【Leetcode】NO.5194 得到目标值的最少行动次数(Python) [周赛],对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
题目:5194. 得到目标值的最少行动次数
你正在玩一个整数游戏。从整数 1 开始,期望得到整数 target 。
在一次行动中,你可以做下述两种操作之一:
递增,将当前整数的值加 1(即, x = x + 1)。
加倍,使当前整数的值翻倍(即,x = 2 * x)。
在整个游戏过程中,你可以使用 递增 操作 任意 次数。但是只能使用 加倍 操作 至多 maxDoubles 次。
给你两个整数 target 和 maxDoubles ,返回从 1 开始得到 target 需要的最少行动次数。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-moves-to-reach-target-score
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
代码
class Solution: def minMoves(self, target: int, maxDoubles: int) -> int: cnt = 0 if maxDoubles == 0: return target-1 while target>1: if (maxDoubles==0): cnt += target-1 return cnt if (target%2): target=target-1 else: target = target//2 maxDoubles = maxDoubles - 1 cnt = cnt + 1 return cnt
这篇关于【Leetcode】NO.5194 得到目标值的最少行动次数(Python) [周赛]的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python