[Swift]LeetCode1033. 边框着色 | Moving Stones Until Consecutive

[Swift]LeetCode1033. 边框着色 | Moving Stones Until Consecutive,第1张

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

Three stones are on a number line at positions @H_403_7@a, @H_403_7@b,and @H_403_7@c.

Each turn,let‘s say the stones are currently at positions @H_403_7@x,y,z with @H_403_7@x < y < z.  You pick up the stone at either position @H_403_7@x or position @H_403_7@z,and move that stone to an integer position @H_403_7@k,with @H_403_7@x < k < z and @H_403_7@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: @H_403_7@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. @H_404_47@   

Example 2:

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

Note:

@H_403_7@1 <= a <= 100 @H_403_7@1 <= b <= 100 @H_403_7@1 <= c <= 100 @H_403_7@a != b,b != c,c != a

三枚石子放置在数轴上,位置分别为 @H_403_7@a,@H_403_7@b,@H_403_7@c。

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

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

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

示例 1:

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

示例 2:

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

提示:

@H_403_7@1 <= a <= 100 @H_403_7@1 <= b <= 100 @H_403_7@1 <= c <= 100 @H_403_7@a != b,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 +=  111         }12         else13         {14             if b - a > 215             {16                 minNum +=  117             }18             if  c - b > 219             {20                 minNum +=  121             }22         }23         return [minNum,c - a - 2]24     }25 }@H_404_47@                                	          总结       

以上是内存溢出为你收集整理的[Swift]LeetCode1033. 边框着色 | Moving Stones Until Consecutive全部内容,希望文章能够帮你解决[Swift]LeetCode1033. 边框着色 | Moving Stones Until Consecutive所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1012632.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存