- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址: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
.
Thegame 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.
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.
Note:
1、 1<=a<=100
;
2、 1<=b<=100
;
3、 1<=c<=100
;
4、 a!=b,b!=c,c!=a
;
a,b,c表示三个位置,在三个位置上各有一个石头。现在要移动三个石头中的若干个,每次移动都必须选两端石头的里面的位置,最终使得它们三个放在连续的位置。问最少需要多少次移动,最多需要多少次移动。
这个题不是算法题,是个脑筋急转弯题。分情况讨论:
如果三个石头本来就连续,则不用移动。例:1,2,3
如果三个石头本来不连续,则: 最少移动次数: 1. 有两个石头之间的距离小于等于2,则最少只需要一次移动。例:1,2,4,把4移动到3即可;或者例1,3,5,把5移到2即可。 2. 所有石头之间的最小距离>2,则最少需要移动两个石头。例:1,4,7,需要把两个石头移动到另一个的旁边。 最多移动次数: 题目说了,只能像两端石头里面的那些位置上放,所以最多移动的次数就是本来两端石头中间包含的点(并且去掉中间的石头),策略是每次向内移动一步。例:1,3,5,在1和5中间之间共有2个可以放的点(分别为2,4),所以最多只能有max_ - min_ - 2次移动。
C++代码如下:
class Solution {
public:
vector<int> numMovesStones(int a, int b, int c) {
int sum_ = a + b + c;
int min_ = min(a, min(b, c));
int max_ = max(a, max(b, c));
int mid_ = sum_ - min_ - max_;
if (max_ - min_ == 2)
return {0, 0};
int min_move = min(mid_ - min_, max_ - mid_) <= 2 ? 1 : 2;
int max_move = max_ - min_ - 2;
return {min_move, max_move};
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2022
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
C++ 中的标准“映射”容器允许您插入右值: T x; std::map m; // m[1]; // populate "1" auto it = m.insert(std::make_pair(
我们知道 std::move does not actually move anything .它只是将左值引用 (&) 转换为右值引用 (&&)。 那么在下面的例子中,拷贝构造函数是如何被调用的呢?
http://en.cppreference.com/w/cpp/language/rule_of_three 几个月前我开始使用 c++11并观看了五人规则。 所以..我开始将复制构造函数/复制赋值
这个问题在这里已经有了答案: In what scenarios should I expect to explicitly need to implement a move constructor
我有一个类似于下面的对象,我正在尝试为它实现一个 move 构造函数,这样你就可以为 std::vector 插入一个. struct Mesh { std::vector vPoint
这个问题在这里已经有了答案: How to create an std::function from a move-capturing lambda expression? (3 个回答) 关闭7年前
我有一个源文件,我正在使用它 move 到一个存档目录 关闭。 move (srcfile,dstdir) 但是当存档目标目录中已经存在相同的文件时,它会抛出一个错误,指出无法 move 文件已经存在
这应该有效,但无效并给出以下错误(如下)。 我读过几篇关于 stackoverflow 的帖子 here和 here但在这种情况下似乎没有一个好的答案。我真的希望我只是错过了一些愚蠢的东西,我已经在这
我似乎无法弄清楚为什么会这样。当我运行以下代码时: $uref = APACHE_ROOT . UPLOAD_PATH . $applicant . "_ref_{$email}_{$year}";
我似乎无法弄清楚为什么会这样。当我运行以下代码时: $uref = APACHE_ROOT . UPLOAD_PATH . $applicant . "_ref_{$email}_{$year}";
我的表格行可以上下 move ,但我的问题是数据表行取代了表格标题(第一行)。 我想要一个固定的第一行,这样当您单击向上箭头时,您就不会向上 move 该行来替换标题。 我尝试了一些条件逻辑来检查当前
正如我在Move constructor/operator=中询问的那样,过了一段时间,我同意并接受了这个问题的正确答案,我只是在想,是否有类似“移动析构函数” 这样的东西会在每次移动的对象上调用会有
如果我有一个像这样的 C 类: class C { std::string s; public: C(std::string& s) : s(s) {} C(std::str
我是 C++11 的新手,发现 move 语义和复制省略非常适合编写优雅高效的代码。不过我有一些问题想请教。这里我写了一个模板类 matrix.hpp 并用它来测试 move 语义的行为。 #incl
我在我们的项目中遇到了这样的代码: class A { public: A(A&& obj): valid_(false), data_(obj.data_) {} //... void
move 语义在这个例子中是如何工作的: struct test { int ii[10]; int i; }; test f() { test a; std::cou
假设我有一个类型为 A 的对象 a。 如果我想将其 move 到函数foo(A)。 一个选择是执行 foo(std::move(a)),这将调用 move 构造函数。 但是,假设我正在使用一个我无法控
我用 move 复制构造函数和 move 复制赋值运算符创建了一个简单的应用程序,并且在它们中的每一个上我都做了一个 cout 语句来告诉我,它们正在执行。但是在执行过程中,我没有看到 move 复制
相关问题: Why this move constructor is not called wtih rvalue temporary? [duplicate] Move Constructor vs
我正在努力研究 move 构造函数,并希望通过这个问题获得更多见解。这是一个简单的类。 class A { private: vector Bs; public: /* ..
我是一名优秀的程序员,十分优秀!