LeetCode:执行的最小和最大移动次数


Three stones are on a number line at position a,b,and c.Each turn,you pick up  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 x or position z,and move that stone to an integer position
 k,with x< y < 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 maximumum 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.

题目大意:
三枚石子放置在数轴上,位置分别为a,b,c.
每一回合,你可以从两端之一拿起一枚石子(位置最大或最小),并将其放入两端之间的任一空闲位置.形式上,假设这三枚石子当前
分别位于位置 x,y,z 且x< y< z.那么就可以从位置x或者是位置z拿起一枚石子,并将该石子移动到某一整数位置k处,其中
x< k < z且k!=y.
当你无法进行任何移动时,即这些石子的位置连续时,游戏结束.
要使游戏结束,你可以执行的最小和最大移动次数分别是多少?以长度为2的数组形式返回答案:answer=[minimum_moves, 
maximum_moves].

解题思路:
这道题每次回合只要移动坐标轴上的左右两边的那个数,而且不能等于中间的数,要使其向区间内部移动.可以利
用一个数组先对其进行排序,根据结果考虑最小步数,只要满足最大和最小之差为1时,就直接返回结果{0,0},或
相邻两者之差小于等于2时最小
的步数为1,否则为2.最大步数即为每次移动一个步数,直到不能移动为止,这是否其实只要最大值减去最小值-2.

算法优化,不需要数组.

C++语言实现

#include <iostream>
#include <vector>
#include <algorithm>

using namespace  std;
class Solution {
public:
    vector<int> numMovesStones(int a, int b, int c) {
        vector<int> res{a,b,c};
        sort(res.begin(),res.end(),less<int>());
        int minNum=0,maxNum=0;
        if(res[2]-res[0]==2){
            return {minNum,maxNum};
        }
        if(res[1]-res[0]<=2||res[2]-res[1]<=2){
            minNum=1;
        }else{
            minNum=2;
        }
        maxNum=res[2]-res[0]-2;
        return {minNum,maxNum};
    }
    vector<int> numMovesStonesOne(int a, int b, int c) {
        int sum=a+b+c;
        int x=min(a,min(b,c));
        int z=max(a,max(b,c));
        int y=sum-x-z;
        int minNum=0;
        if(z-x==2){
            return {0,0};
        }
        if(y-x<=2||z-y<=2){
            minNum=1;
        }else{
            minNum=2;
        }
        return {minNum,z-x-2};
    }
};

int main(int argc,char* argv[]){
    vector<int> ans=Solution().numMovesStones(7,4,1);
    for(auto& a:ans){
        cout<<a<<" ";
    }
    cout<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路上的追梦人

您的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值