1033. Moving Stones Until Consecutive

复制代码
package LeetCode_1033

/**
 * 1033. Moving Stones Until Consecutive
 * 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.
 * */
class Solution {
    /*
    * solution: sort and calculate the minimum and maximum moves,
    * Time complexity:O(nlogn), Space complexity:O(1)
    * */
    fun numMovesStones(a: Int, b: Int, c: Int): IntArray {
        var minimumMove = 0
        var maximumMove = 0
        val array = intArrayOf(a, b, c)
        array.sort()
        //if each num differs by 1, mean minimumMove is 0
        if (array[1] - array[0] == 1 && array[2] - array[1] == 1) {
            minimumMove = 0
        } else if (array[1] - array[0] <= 2 || array[2] - array[1] <= 2) {
            //if each num differs by 2, mean minimumMove is 0
            minimumMove = 1
        } else {
            minimumMove = 2
        }
        //the maximum move is: two endpoint stone move one space each time, -2 because jump over b
        maximumMove = array[2] - array[0] - 2

        return intArrayOf(minimumMove, maximumMove)
    }
}
复制代码

 

posted @   johnny_zhao  阅读(84)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示