志,敢教日月换新天。为有牺牲多壮

[Swift]LeetCode1033. 移动石子直到连续 | Moving Stones Until Consecutive

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10783233.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

Each turn, 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 stone from 5 to 4 then to 3, or we can move it directly to 3.

Example 2:

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

Note:

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

三枚石子放置在数轴上,位置分别为 abc

每一回合,我们假设这三枚石子当前分别位于位置 x, y, z 且 x < y < z。从位置 x 或者是位置 z 拿起一枚石子,并将该石子移动到某一整数位置 k 处,其中 x < k < z 且 k != y

当你无法进行任何移动时,即,这些石子的位置连续时,游戏结束。

要使游戏结束,你可以执行的最小和最大移动次数分别是多少? 以长度为 2 的数组形式返回答案:answer = [minimum_moves, maximum_moves]

示例 1:

输入:a = 1, b = 2, c = 5
输出:[1, 2]
解释:将石子从 5 移动到 4 再移动到 3,或者我们可以直接将石子移动到 3。

示例 2:

输入:a = 4, b = 3, c = 2
输出:[0, 0]
解释:我们无法进行任何移动。

提示:

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

Runtime: 8 ms
Memory Usage: 19.1 MB
复制代码
 1 class Solution {
 2     func numMovesStones(_ a: Int, _ b: Int, _ c: Int) -> [Int] {
 3         var arr:[Int] = [a,b,c].sorted(by:<)
 4         let a = arr[0]
 5         let b = arr[1]
 6         let c = arr[2]
 7         var minNum:Int = 0
 8         if b - a == 2 || c - b == 2
 9         {
10             minNum +=  1
11         }
12         else
13         {
14             if b - a > 2
15             {
16                 minNum +=  1
17             }
18             if  c - b > 2
19             {
20                 minNum +=  1
21             }
22         }
23         return [minNum, c - a - 2]
24     }
25 }
复制代码

8ms 
复制代码
 1 class Solution {
 2     func numMovesStones(_ a: Int, _ b: Int, _ c: Int) -> [Int] {
 3         let arr = [a, b, c].sorted()
 4         let a = arr[0]
 5         let b = arr[1]
 6         let c = arr[2]
 7         
 8         if b - a == 1 {
 9             if c - b == 1 {
10                 return [0, 0]
11             }
12             return [1, c - b - 1]
13         } else if c - b == 1 {
14             return [1, b - a - 1]
15         } else {
16             if b - a == 2 || c - b == 2 {
17                 return [1, c - a - 2]
18             }
19             return [2, c - a - 2]
20         }
21     }
22 }
复制代码

 

posted @   为敢技术  阅读(594)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°