【LeetCode】1033. Moving Stones Until Consecutive 解题报告(C++)
作者: 负雪明烛
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 <= a <= 100
1 <= b <= 100
1 <= c <= 100
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++)的更多相关文章
- 【Leetcode_easy】1033. Moving Stones Until Consecutive
problem 1033. Moving Stones Until Consecutive 参考 1. Leetcode_easy_1033. Moving Stones Until Consecut ...
- 【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 ...
- 【LeetCode】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】760. Find Anagram Mappings 解题报告
[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
随机推荐
- plyr包使用
#-------------------------------- # plyr包使用# 建议直接保存为R文件到Rstudio中运行 #-------------------------------- ...
- logname
logname命令用来显示用户名称. 语法 logname(选项) 选项 --help:在线帮助: --vesion:显示版本信息.
- R绘图布局包 customLayout
今天介绍一个R画图布局的包,地址如下: https://github.com/zzawadz/customLayout https://www.customlayout.zstat.pl/index. ...
- 【leetcode】451. Sort Characters By Frequency
Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...
- Sibel Tools和Siebel Cilent的安装步骤
关于Siebel的资料在网上是少之又少,当时安装开发工具的时候花了挺长时间的,把步骤记录了下来. 一安装win32_11gR2_client 首先要安装Oracle数据库的客户端,必须是32位,安装过 ...
- go 代理
环境变量中设置 #GO111MODULE=auto GOPROXY=https://goproxy.io 如果不第一次,则在命令行设置 go env -w GO111MODULE=on go env ...
- java使用在线api实例
字符串 strUrl为访问地址和参数 public String loadAddrsApi() { StringBuffer sb; String strUrl = "https://api ...
- 【C/C++】函数的分文件编写
创建同名的头文件(.h)和cpp文件. 在头文件里写函数声明,在cpp文件中写函数定义. 在cpp文件中写#include "xx.h" //自定义头文件名 框架(include ...
- java多线程并发编程中的锁
synchronized: https://www.cnblogs.com/dolphin0520/p/3923737.html Lock:https://www.cnblogs.com/dolphi ...
- Unity——WegGL打包问题
Rendering设置 Gamma和Linear颜色空间,两者有色差,Gamma有个2.25左右的修正值: WebGL2.0可用的情况,只支持Deferred Render延迟渲染,且只支持Linea ...