【leetcode】1033. Moving Stones Until Consecutive
题目如下:
Three stones are on a number line at positions
a,b, andc.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, zwithx < y < z. You pick up the stone at either positionxor positionz, and move that stone to an integer positionk, withx < k < zandk != 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 the stone from 5 to 3, or move the stone from 5 to 4 to 3.Example 2:
Input: a = 4, b = 3, c = 2
Output: [0,0]
Explanation: We cannot make any moves.Example 3:
Input: a = 3, b = 5, c = 1
Output: [1,2]
Explanation: Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.Note:
1 <= a <= 1001 <= b <= 1001 <= c <= 100a != 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的直接的空位中。
代码如下:
class Solution(object):
def numMovesStones(self, a, b, c):
"""
:type a: int
:type b: int
:type c: int
:rtype: List[int]
"""
l = sorted([a,b,c])
a,b,c = l[0],l[1],l[2]
maxv = abs(c-b-1) + abs(b-a-1)
minv = 0
left = (b-a-1)
right = (c-b-1)
if left == 0 and right == 0:
minv = 0
elif left == 0 or right == 0:
minv = 1
elif left == 1 or right == 1:
minv = 1
else:
minv = 2
return [minv,maxv]
【leetcode】1033. Moving Stones Until Consecutive的更多相关文章
- 【LeetCode】1033. Moving Stones Until Consecutive 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 脑筋急转弯 日期 题目地址:https://leet ...
- 【Leetcode_easy】1033. Moving Stones Until Consecutive
problem 1033. Moving Stones Until Consecutive 参考 1. Leetcode_easy_1033. Moving Stones Until Consecut ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】Jewels and Stones(宝石与石头)
这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...
- 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...
- 【LeetCode】967. Numbers With Same Consecutive Differences 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- 【LeetCode】947. Most Stones Removed with Same Row or Column 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...
- 【Leetcode】Jewels and Stones
Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...
- 【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 ...
随机推荐
- CMakeLists.txt 语法
命令不区分大小写(参数区分大小写) add_executable(demo main.cpp main.h main.rc) 用main.cpp源文件,main.h文件,main.rc文件构造可执行文 ...
- leetcode-mid-array-334 Increasing Triplet Subsequence-NO
mycode time limited class Solution(object): def increasingTriplet(self, nums): """ ...
- Markdown Memo(memorandum)
居中 html语法 <center>居中</center> 左对齐 <p align="left">左对齐</p> 右对齐 < ...
- HTTP代理(转)
个人总结: 两篇文章介绍了https代理的两种方式: ·一种是普通http请求代理 ·一种是通过隧道进行基于tcp的代理 转两篇好文: HTTP 代理原理及实现(一) https://imququ.c ...
- Android.应用软件.常用程序下载地址_20190913
1. 1.1. 健康友行 微信 官网 https://weixin.qq.com/ 抖音 chrome 百度网盘(账号:osskill)中有 1.2. 支付宝 官网 https://mobile.al ...
- 001--PowerDesigner连接MySQL
PowerDesigner连接MySQL(一) 博客地址:https://blog.csdn.net/codemonkey_king/article/details/53263597 https:// ...
- 获取文件夹中前N个文件
@echo off set input="list.txt" set srcDir="%1" set /a fileCount=10 set /a curInd ...
- python基础-6 正则表达式
一 python正则简介 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现. 正则表达式模式被编译成一系列的字 ...
- Ansible--常用命令整理
由于最近使用ansible在多台服务器部署程序,运行命令的时候,发现对Linux和ansible自动运维工具用的不太熟练,所以搜集整理一些,方便日后复习提升,达到熟练运用的目的. 对于详细的安装教程和 ...
- C++ 中赋值运算符重载以及深拷贝浅拷贝解析
转载自:http://blog.csdn.net/business122/article/details/21242857 关键词:构造函数,浅拷贝,深拷贝,堆栈(stack),堆heap,赋值运算符 ...