作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/moving-stones-until-consecutive/

题目描述

Three stones are on a number line at positions a, b, 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:

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. 1 <= a <= 100
  2. 1 <= b <= 100
  3. 1 <= c <= 100
  4. a != b, b != c, c != a

题目大意

a,b,c表示三个位置,在三个位置上各有一个石头。现在要移动三个石头中的若干个,每次移动都必须选两端石头的里面的位置,最终使得它们三个放在连续的位置。问最少需要多少次移动,最多需要多少次移动。

解题方法

脑筋急转弯

这个题不是算法题,是个脑筋急转弯题。分情况讨论:

如果三个石头本来就连续,则不用移动。例:1,2,3

如果三个石头本来不连续,则:
最少移动次数
1. 有两个石头之间的距离小于等于2,则最少只需要一次移动。例:1,2,4,把4移动到3即可;或者例1,3,5,把5移到2即可。
2. 所有石头之间的最小距离>2,则最少需要移动两个石头。例:1,4,7,需要把两个石头移动到另一个的旁边。
最多移动次数
题目说了,只能像两端石头里面的那些位置上放,所以最多移动的次数就是本来两端石头中间包含的点(并且去掉中间的石头),策略是每次向内移动一步。例:1,3,5,在1和5中间之间共有2个可以放的点(分别为2,4),所以最多只能有max_ - min_ - 2次移动。

C++代码如下:

class Solution {
public:
vector<int> numMovesStones(int a, int b, int c) {
int sum_ = a + b + c;
int min_ = min(a, min(b, c));
int max_ = max(a, max(b, c));
int mid_ = sum_ - min_ - max_; if (max_ - min_ == 2)
return {0, 0}; int min_move = min(mid_ - min_, max_ - mid_) <= 2 ? 1 : 2;
int max_move = max_ - min_ - 2;
return {min_move, max_move};
}
};

日期

2019 年 8 月 31 日 —— 赶在月底做个题

【LeetCode】1033. Moving Stones Until Consecutive 解题报告(C++)的更多相关文章

  1. 【Leetcode_easy】1033. Moving Stones Until Consecutive

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

  2. 【leetcode】1033. Moving Stones Until Consecutive

    题目如下: Three stones are on a number line at positions a, b, and c. Each turn, you pick up a stone at ...

  3. 【LeetCode】468. Validate IP Address 解题报告(Python)

    [LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  4. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

  5. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  6. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  7. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  8. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  9. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. fluidity详解

    fluidity详解 1.fluidity编译过程 1.1.femtools库调用方法 编译fluidity/femtools目录下所有文件,打包为libfemtools.a静态库文件: 通过-lfe ...

  2. R包对植物进行GO,KEGG注释

    1.安装,加载所用到到R包 用BiocManager安装,可同时加载依赖包 source("https://bioconductor.org/biocLite.R") BiocMa ...

  3. 【Workflows】 WGS/WES Mapping to Variant Calls

    WGS/WES Mapping to Variant Calls - Version 1.0 htslib官网上给的一个WGS/WES的流程.关于htslib.samtools和bcftools之间的 ...

  4. du命令之计算文件大小

    在linux中,常用du命令来计算文件或目录的大小 名称: du - 计算每个文件的磁盘用量,目录则取总用量. 用法: du [选项]... [文件]... 常用选项 -a, --all 输出所有文件 ...

  5. Python压缩&解压缩

    Python中常用的压缩模块有zipfile.tarfile.gzip 1.zipfile模块的简单使用 import zipfile # 压缩 z1 = zipfile.ZipFile('zip_t ...

  6. 9. Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  7. C++/Python冒泡排序与选择排序算法详解

    冒泡排序 冒泡排序算法又称交换排序算法,是从观察水中气泡变化构思而成,原理是从第一个元素开始比较相邻元素的大小,若大小顺序有误,则对调后再进行下一个元素的比较,就仿佛气泡逐渐从水底逐渐冒升到水面一样. ...

  8. python2 第二天

    requests库 编码和解码 输入和输出,在Python中,为了更好的调试和输出,我们需要对字符串进⾏格式化的输出,⽐如我们定义了姓名和年龄,但是我 们需要输出完整的信息,那么就涉及到字符串格式化的 ...

  9. 深入理解mysql锁与事务隔离级别

    一.锁 1.锁的定义     锁即是一种用来协调多线程或进程并发使用同一共享资源的机制 2.锁的分类 从性能上分类:乐观锁和悲观锁 从数据库操作类型上分类:读锁和写锁 从操作粒度上分类:表锁和行锁 2 ...

  10. 零基础学习java------20---------反射

    1. 反射和动态代理 参考博文:https://blog.csdn.net/sinat_38259539/article/details/71799078 1.0 什么是Class: 我们都知道,对象 ...