【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, z
withx < y < z
. You pick up the stone at either positionx
or positionz
, and move that stone to an integer positionk
, withx < k < z
andk != 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 <= 100
1 <= b <= 100
1 <= c <= 100
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的直接的空位中。
代码如下:
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 ...
随机推荐
- vi不能使用jk 映射?
vi不能使用jk 映射? 因为vi 不支持inormap 这种键映射! 要安装vim-enhanced后才能使用vim命令, 也才能够使用 键映射!
- 红帽虚拟化RHEV-PXE批量安装RHEV-H
目录 目录 前言 软件环境 前提 部署 PXE 环境 使用 yum 安装所需软件 配置 DHCP 配置 TFTP-Server 配置 vsftpd 服务用于提供安装系统所需软件包 安装 kicksta ...
- Linux_FTP服务器
目录 目录 FTP FTP Server FTP configuration Global config Anonymous user FTP Config Virtual user FTP Loca ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_05 List集合_1_List集合_介绍&常用方法
有序的,还包含索引,允许有重复的值 . add 打印出来的不是地址,说明重写了toString的方法 remove方法 返回的是被移除的元素 set方法 get 索引越界异常 几种越界的异常
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_7_File类创建删除功能的方法
createNewFile() createNewFile抛出了异常 抛出了一个IO异常 所有我们调用方法的时候必须处理异常 throws这个异常 返回结果为true 最终创建好的文件 再次执行代码. ...
- nginx windows安装基础
nginx在 window上运行需要1.17.3以上. 官方文件https://nginx.org/en/docs/windows.html nginx启动: 1:进入安装目录,双击nginx.exe ...
- JDK8以后接口是可以定义实现方法,必须需要default修饰符修饰
package com.company.java.oop.cls; interface IB { default void doMethod1() { System.out.println(" ...
- python爬虫——爬取淘票票正在热映电影
今天正好学习了一下python的爬虫,觉得收获蛮大的,所以写一篇博客帮助想学习爬虫的伙伴们. 这里我就以一个简单地爬取淘票票正在热映电影为例,介绍一下一个爬虫的完整流程. 首先,话不多说,上干货——源 ...
- python基础-4.1 open 打开文件练习:修改haproxy配置文件
1.如何在线上环境优雅的修改配置文件? 配置文件名称ini global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 in ...
- c语言秋季作业2
问题 答案 这个作业属于哪个课程 C语言程序设计I 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/8657 我在这 ...