一.问题描述

Three stones are on a number line at positions ab, and c.

Each turn, let's say the stones are currently at positions x, y, z with x < y < z.  You pick up the stone at either position x or position z, and move that stone to an integer position k, with x < k < z and k != y.

The game ends when you cannot make any more moves, ie. the stones are in consecutive positions.

When the game ends, what is the minimum and maximum number of moves that you could have made?  Return the answer as an length 2 array: answer = [minimum_moves, maximum_moves]

Example 1:

Input: a = 1, b = 2, c = 5
Output: [1, 2]
Explanation: Move stone from 5 to 4 then to 3, or we can move it directly to 3.

Example 2:

Input: a = 4, b = 3, c = 2
Output: [0, 0]
Explanation: We cannot make any moves.

Note:

  1. 1 <= a <= 100
  2. 1 <= b <= 100
  3. 1 <= c <= 100
  4. a != b, b != c, c != a

二.解题思路

核心是对最小和最大step情况的判断

对于最小来说,步数范围是0~2,0的情况就是三个直接在一起,1的情况就是三个中有两个相邻,或者有两个之间的距离是1,可以把另一个直接插在中间。2的话就是不是上诉情况,要把两个石头分别移到中间石头的两边。

最大step就很简单了,固定的,因为石头只能在两个边界之间走,所以最大就是两个边界之间能走的都走完。因为就是一次一步步走嘛,并且边界的石头如果越过了中间那块石头,那他就从边界的石头变成了中间的石头,之后边界的石头还是得一步步走。意思就是不管什么走法,最大的走法他们都会把两个边界之间能走的地方走满,(想证明的直接用反证法,比这种情况的maxstep还大的意思就是有某个位置被走了1步以上,想想看有什么情况哪个位置才能走一步以上)。所以最大就是两个边界值相减减去2,减2是因为他们仨连在一起之后,移动的那俩还得占两个坑。

更多leetcode算法题解法请关注我的专栏leetcode算法从零到结束或关注我

欢迎大家一起套路一起刷题一起ac

三.源码

class Solution:def numMovesStones(self, a: int, b: int, c: int) -> List[int]:listtmp=[a,b,c]listtmp.sort()a,b,c=listtmpif b-a==1 and c-b ==1:return [0,0]minstep=2if b-a==1 or c-b==1 or c-b==2 or b-a==2:minstep-=1maxstep=c-amaxstep-=2return [minstep,maxstep]

leetcode 1033. Moving Stones Until Consecutive 解法 python相关推荐

  1. LeetCode刷题——哈希表(python语言)

    LeetCode刷题--哈希表(python语言) 一.哈希表 1.1 哈希表的概念 哈希表,也叫散列表.其实可以很像python的字典,也就是键(key)值(Hash(key))对,最简单也最常用的 ...

  2. 最大质因子递归解法python

    最大质因子的递归解法&Python: 问题描述: 求一个任意正整数的最大质因子,呈上代码及注释: # 最大质因子 def calc(x:int)->int:# 函数参数类型设置以及返回值 ...

  3. 【LeetCode】35. 搜索插入位置---JavaScript解法

    [LeetCode]35. 搜索插入位置-JavaScript解法 原题链接:https://leetcode-cn.com/problems/search-insert-position/ /*** ...

  4. Moving stones

    题目连接: Moving stones 大致题意: 有n堆石头, 你每次可以选择一堆, 然后从剩下的n-1堆石头每堆中拿出一个放到选择的这堆里. 操作后不能有石头堆的石头数小于0, 问你能否平衡n堆石 ...

  5. leetcode每日一题·买卖股票问题(Python)

    leetcode每日一题·买卖股票问题(Python) 买卖股票的最佳时机(股票最大利润) 题目链接 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的 ...

  6. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

  7. LeetCode 53. Maximum Subarray--动态规划--C++,Python解法

    题目地址:Maximum Subarray - LeetCode Given an integer array nums, find the contiguous subarray (containi ...

  8. LeetCode 136. Single Number--异或--Java,C++,Python解法

    题目地址:Single Number - LeetCode Given a non-empty array of integers, every element appears twice excep ...

  9. LeetCode 题 - 53. 最大子序和 python解法

    题目 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续 ...

  10. leetcode 1217. Play with Chips 解法 python

    一.问题描述 There are some chips, and the i-th chip is at position chips[i]. You can perform any of the t ...

最新文章

  1. java代码审查规范
  2. C++ 复数类加减法运算重载为成员函数形式
  3. DOS MD命令三种用法
  4. 关于eclipse没有http jar的问题解释
  5. mac虚拟机linux性能测试,Veertu 1.0.4 – Mac上最轻便的虚拟机,可以运行Windows/Linux。...
  6. Mozilla Firefox 66 将使用更少的内存,提高扩展性能
  7. 解析json结构绘制canvas
  8. Hex2Bin使用方法
  9. USB协议详解第9讲(USB描述符-HID描述符)
  10. 2022-2028年全球与中国粮食种植行业市场深度调研及投资预测分析
  11. 杭州电子科技大学ACM 1018
  12. 阿里云香港服务器带宽太贵怎么办?香港节点全球CDN加速你值得拥有
  13. 无人再谈“永恒牛市”
  14. 计算机无法登录到你的账户,win10电脑无法登陆到你的账户怎么办?win10电脑无法登陆到你的账户相关讲解...
  15. 后缀名htm与html的区别
  16. 车联网真要来了?全球汽车网络安全市场将实现跨越式增长
  17. 核桃、石头和榴莲——中年男人の致富经
  18. 1875. 贝茜的报复
  19. 互联网新机遇: 移动社交电商将成为下一个风口?
  20. 自动驾驶外卖上线,美团即将配送一切-1

热门文章

  1. 汪华:未来三年的移动互联网创业
  2. 网络流媒体(七)———PTSP
  3. 无线扩展器中继器 和 电力猫 性能比较
  4. 五步轻松搞定QGIS三维地形浏览
  5. 楠哥Java SE总结详细笔记
  6. 研发团队绩效评定机制细则
  7. FTP上传软件常见错误问题集锦
  8. 如何用ftp上传到服务器视频文件,ftp如何将文件上传到服务器上
  9. 2021-06-30
  10. BGP双平面实验---bgp的选路