题目如下:

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

Each turn, you pick up a stone at an endpoint (ie., either the lowest or highest position stone), and move it to an unoccupied position between those endpoints.  Formally, 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:

  1. Input: a = 1, b = 2, c = 5
  2. Output: [1,2]
  3. Explanation: Move the stone from 5 to 3, or move the stone from 5 to 4 to 3.

Example 2:

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

Example 3:

  1. Input: a = 3, b = 5, c = 1
  2. Output: [1,2]
  3. Explanation: Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.

Note:

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

解题思路:首先对a,b,c进行排序,使得a最小,b其次,c最大。最大的移动次数很好求,就是a与c分别往b移动,每次移动一步,很容易得到最大移动次数是abs(c-b-1) + abs(b-a-1)。最小移动次数似乎很直接,就是a与c直接一步移动到b的两侧,这种情况下值应该是2,但是有几种特殊场景:第一是a,b,c都相邻,那么值是0;第二种是a与b相邻或者b与c相邻,那么值为1;第三种是a与b的差值为2或者b与c的差值位2,这时可以直接那剩余那个元素一步移动到a与b或者b与c的直接的空位中。

代码如下:

  1. class Solution(object):
  2. def numMovesStones(self, a, b, c):
  3. """
  4. :type a: int
  5. :type b: int
  6. :type c: int
  7. :rtype: List[int]
  8. """
  9. l = sorted([a,b,c])
  10. a,b,c = l[0],l[1],l[2]
  11. maxv = abs(c-b-1) + abs(b-a-1)
  12. minv = 0
  13. left = (b-a-1)
  14. right = (c-b-1)
  15. if left == 0 and right == 0:
  16. minv = 0
  17. elif left == 0 or right == 0:
  18. minv = 1
  19. elif left == 1 or right == 1:
  20. minv = 1
  21. else:
  22. minv = 2
  23. return [minv,maxv]

【leetcode】1033. Moving Stones Until Consecutive的更多相关文章

  1. 【LeetCode】1033. Moving Stones Until Consecutive 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 脑筋急转弯 日期 题目地址:https://leet ...

  2. 【Leetcode_easy】1033. Moving Stones Until Consecutive

    problem 1033. Moving Stones Until Consecutive 参考 1. Leetcode_easy_1033. Moving Stones Until Consecut ...

  3. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  4. 【LeetCode】Jewels and Stones(宝石与石头)

    这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...

  5. 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...

  6. 【LeetCode】967. Numbers With Same Consecutive Differences 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  7. 【LeetCode】947. Most Stones Removed with Same Row or Column 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...

  8. 【Leetcode】Jewels and Stones

    Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...

  9. 【leetcode】1171. Remove Zero Sum Consecutive Nodes from Linked List

    题目如下: Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum ...

随机推荐

  1. Linux安装nslookup命令

    做DNS的人都知道nslookup命令是做什么用的,windows系统自带的.但是linux系统是不自带这个命令的,需要人手动安装.如果您不记得这是哪个软件包提供这个命令的话,那您还真会有些麻烦了.下 ...

  2. ORACLE动态监听

    动态监听的原理 pmon在数据库启动到mount或open时,动态从参数文件中读取service_names值.service_names可以为多值(可以有64个,其中包括两个系统的).  servi ...

  3. Denali NAND FLASH控制器的验证

    NAND FLASH的结构如图所示: Denali NAND FLASH控制器模块提供了从AHB总线到外部NAND FLASH存储器芯片IO管脚的访问功能.主要技术特性包括: 1.标准32位AHB总线 ...

  4. C#和.NET获取绝对路径

    c#获取绝对路径:System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"); .net获取绝 ...

  5. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第5节 String类_3_字符串的常量池

    字符换是可以共享使用的,那么怎么去共享使用呢 三种方式去创建字符串.然后三种分别进行比较 3的地址和1.2的地址不一样 在堆里面有一块空间叫做字符串常量池,从jdk1.7开始.字符串常量池在堆中 字符 ...

  6. mkdir: 无法创建目录"kk": 只读文件系统

    创建文件提示 root@hap1:/test>mkdir kk mkdir: 无法创建目录"kk": 只读文件系统 root@hap1:/test>mount .... ...

  7. tensorflow学习笔记二:入门基础 好教程 可用

    http://www.cnblogs.com/denny402/p/5852083.html tensorflow学习笔记二:入门基础   TensorFlow用张量这种数据结构来表示所有的数据.用一 ...

  8. 应用安全-CTF-格式串漏洞

    主要影响c库中print家族函数 - > printf,sprintf,fprintf等 利用: SIP请求URI中格式串

  9. 1.关于狗书《Flask Web开发 基于Python的web开发应用实战》身份验证的改进

    在我学习用户身份验证的时候,我发现这里有个小弊端,在用户注册完成后想要验证邮箱的时候,点击邮箱中的网址进行验证,此时还要登陆,这及其不符合我们的习惯.一般情况下我们只需要点击网址就可以验证成功并且进入 ...

  10. js 一道题目引发的正则的学习

    正则表达式中的特殊字符 字符 含意 \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为匹配一个 ...