LeetCode //C - 1033. Moving Stones Until Consecutive

1033. Moving Stones Until Consecutive

There are three stones in different positions on the X-axis. You are given three integers a, b, and c, the positions of the stones.

In one move, you pick up a stone at an endpoint (i.e., 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, and 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 (i.e., the stones are in three consecutive positions).

Return an integer array answer of length 2 where:

  • answer0 is the minimum number of moves you can play, and
  • answer1 is the maximum number of moves you can play.
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.

Constraints:
  • 1 <= a, b, c <= 100
  • a, b, and c have different values.

From: LeetCode

Link: 1033. Moving Stones Until Consecutive


Solution:

Ideas:
  • First sort the three positions so a < b < c.
  • Let the gaps be b-a and c-b.
  • Maximum moves = total empty spots between the outer stones = c - a - 2.
  • Minimum moves:
    • 0 if already consecutive.
    • 1 if one gap is 2 or less, because one move can make them consecutive.
    • otherwise 2.
Code:
c 复制代码
static void sort3(int *x, int *y, int *z) {
    int t;
    if (*x > *y) { t = *x; *x = *y; *y = t; }
    if (*y > *z) { t = *y; *y = *z; *z = t; }
    if (*x > *y) { t = *x; *x = *y; *y = t; }
}

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* numMovesStones(int a, int b, int c, int* returnSize) {
    sort3(&a, &b, &c);   // make sure a < b < c
    
    int *ans = (int *)malloc(sizeof(int) * 2);
    *returnSize = 2;
    
    int leftGap = b - a;
    int rightGap = c - b;
    
    /* maximum moves:
       keep shrinking the larger interval one step at a time */
    ans[1] = (c - a - 2);
    
    /* minimum moves */
    if (leftGap == 1 && rightGap == 1) {
        ans[0] = 0;              // already consecutive
    } else if (leftGap <= 2 || rightGap <= 2) {
        ans[0] = 1;              // one special jump can finish it
    } else {
        ans[0] = 2;              // otherwise need two moves
    }
    
    return ans;
}
相关推荐
kkeeper~2 小时前
0基础C语言积跬步之数据在内存中的存储
c语言·数据结构·算法
wabs6664 小时前
关于贪心算法的一些自我总结【力扣45.跳跃游戏II】【灵感来源:代码随想录】
算法·贪心算法·复盘
2401_876964134 小时前
【湖北专升本】2026湖北专升本真题PDF+备考资料汇总
数据结构·人工智能·经验分享·深度学习·算法·计算机视觉
qq3862461964 小时前
更新补发第6天:7天学会C语言,每天5分钟,不需要基础
c语言·for循环·循环语句·while循环·do-while循环
嗝o゚4 小时前
CANN GE 算子融合——融合算法与调度策略
算法·昇腾·cann·ge
小江的记录本5 小时前
【JVM虚拟机】垃圾回收GC:垃圾回收算法:标记-清除、标记-复制、标记-整理、分代收集(附《思维导图》+《面试高频考点清单》)
java·jvm·后端·python·算法·安全·面试
Ulyanov6 小时前
用声明式语法重新定义Python桌面UI:QML+PySide6现代开发入门(一)
开发语言·python·算法·ui·系统仿真·雷达电子对抗仿真
数据科学小丫6 小时前
特征工程处理
人工智能·算法·机器学习
z落落7 小时前
C#参数区别
java·算法·c#
c238567 小时前
vector(下)
数据结构·算法