本文涉及知识点
C++差分数组
LeetCode3229. 使数组等于目标数组所需的最少操作次数
给你两个长度相同的正整数数组 nums 和 target。
在一次操作中,你可以选择 nums 的任何子数组,并将该子数组内的每个元素的值增加或减少 1。
返回使 nums 数组变为 target 数组所需的 最少 操作次数。
示例 1:
输入: nums = [3,5,1,2], target = [4,6,2,4]
输出: 2
解释:
执行以下操作可以使 nums 等于 target:
- nums[0…3] 增加 1,nums = [4,6,2,3]。
- nums[3…3] 增加 1,nums = [4,6,2,4]。
示例 2:
输入: nums = [1,3,2], target = [2,1,4]
输出: 5
解释:
执行以下操作可以使 nums 等于 target: - nums[0…0] 增加 1,nums = [2,3,2]。
- nums[1…1] 减少 1,nums = [2,2,2]。
- nums[1…1] 减少 1,nums = [2,1,2]。
- nums[2…2] 增加 1,nums = [2,1,3]。
- nums[2…2] 增加 1,nums = [2,1,4]。
提示:
1 <= nums.length == target.length <= 105
1 <= nums[i], target[i] <= 108
差分数组
显然调整操作顺序,不影响结果。那我们按子数组的left升序排序。
如果对子数组nums[i…j] +1,则不会对子数组[x1,x2]-1,i <= x1 <=j 。
如果x2 <= j,拆分[i…x1-1]和[x2+1…j]两个子数组,操作次数一样多。
如果x2 > j,拆分[i…x1-1] ,nsum[j+1,x2],操作次数一样多。
即任意x只会+1,或只会-1。
修改nums[i]有两种方法:
一,增加新数组,以i开始。
二,延长以nums[i-1]结尾的数组。
pre记录以i-1结尾的子数组数量,正数表示+1的子数组数量,负数表示以-1的子数组数量。
needAdd = target[i] - nums[i]
curNeed = (needAdd*pre>0) ? (abs(needAdd) - abs(pre)) : abs(needAdd)
curNeed = max(0,curNeed)
curNeed之和就是答案。
代码
核心代码
class Solution {
public:
long long minimumOperations(vector<int>& nums, vector<int>& target) {
int pre = 0;
long long ret = 0;
for (int i = 0; i < nums.size(); i++) {
const int needAdd = target[i] - nums[i];
int cur = abs(needAdd);
if ((long long)pre * needAdd > 0) {
cur = max(0, cur - abs(pre));
}
ret += cur;
pre = needAdd;
}
return ret;
}
};
单元测试
vector<int> nums, target;
TEST_METHOD(TestMethod11)
{
nums = { 3, 5, 1, 2 }, target = { 4, 6, 2, 4 };
auto res = Solution().minimumOperations(nums, target);
AssertEx(2LL, res);
}
TEST_METHOD(TestMethod12)
{
nums = { 1,3,2}, target = { 2,1,4};
auto res = Solution().minimumOperations(nums, target);
AssertEx(5LL, res);
}
TEST_METHOD(TestMethod13)
{
nums = { 27365,12257,22134,59798,42430,45234,23451,17474,30245,18748,15156,24804,5127,26494,3699,45569,36298,54271,29728,15718,18849,1286,11209,11282,23755,27453,23051,42873,3196,20479,1097,16921,51123,12684,59417,14407,38527,16036,30541,41185,9492,27339,59887,45402,13260,57491,54209,21949,31623,11629,61359,20819,10794,28506,35507,47322,52923,16993,51123,12732,40738,55985,52234,39826,36490,40394,563,5310,19641,57708,21112,60485,50550,50958,10069,6700,45207,46580,5339,30829,21522,29769,61995,12618,40604,4687,8404,28513,10003,22243,59850,8166,46924,17686,4045,10437,16855,9626,23986,32359,36935,45058,49710,15050,12895,1637,6728,25410,2265,10073,23083,14044,31335,32223,26228,44618,24473,33009,50736,48303,9918,5760,29877,34949,41350,60372,1755,27769,36828,7551,32660,42727,48621,34092,42288,9600,3521,6967,42093,62477,26025,55079,39038,21280,16525,53348,62391,44270,4606,2356,1783,56534,356,60748,49351,31625,34405,29026,34421,8953,5625,41826,41637,51672,21626,37991,5508,56225,10104,17071,18839,25845,833,35591,5559,4487,35578,10052,11051,34710,21643,5701,33470,14741,43166,291,31857,37696,5783,34785,19408,26497,21704,60364,34579,51376,24727,45252,21819,3314,27055,11585,25925,4103,25528,52583,47166,37792,19482,7572,14110,4238,25691,60053,10691,9906,22067,21461,60027,11762,24607,20195,3582,21376,4489,4597,56479,35739,33935,15663,35443,4640,52095,39902,57636,21941,21681,4285,61790,52096,49361,11117,60475,44534,42343,9133,52709,58833,43482,36502,40496,30990,27773,23233,21518,23812,51120,59225,14868,47112,54639,8028,14684,6388,27480,53519,40526,37554,42411,43004,15116,43512,36249,32544,24343,30162,4192,23720,41090,20036,28034,40594,16128,45881,39653,39237,58633,41049,10504,46133,43447,3742,50296,33068,58338,38355,23706,60654,29244,36820,41301,44202,38622,7029,28669,50049,45995,8849,36113,55336,34463,6345,34541,38141,29664,11161,58189,12166,28987,30618,58026,42333,26714,34205,30256,55305,41764,25361,37582,42203,12950,3404,1067,10158,38872,61473,3244,34040,14835,2456,47150,52419,55166,42491,43095,31392,45188,43896,34973,2218,42027,16057,12268,36771,33429,19100,26961,57475,60984,3670,55446,49732,54177,46380,33181,52252,1420,47532,20569,14046,52741,41263,30228,50618,5430,52696,11958,32094,34838,30848,54209,9527,45989,16148,12105,9450,59434,33377,18109,1769,4934,14632,27751,28122,19370,25114 },
target = { 51103,44402,7105,38971,26824,17531,29942,9148,38778,44441,41611,50620,12182,54248,26669,52545,42288,45331,39030,1759,59026,5697,24530,36958,1070,42016,25749,6643,11793,32735,46870,29002,21764,60173,1032,32765,33717,47168,20824,43334,43138,55640,11844,32119,21850,282,53623,38330,27261,47738,22105,56436,27137,12624,55095,14551,26246,42006,40504,15919,4351,25377,52738,14451,17720,50729,8804,17583,51583,53315,59805,1487,19984,18563,20701,22741,56488,56531,50488,39490,1635,806,13625,5044,2223,53773,30353,27711,45843,42300,22451,36846,45938,42975,24105,4717,33577,36902,25897,3870,7314,49736,32744,13751,13699,4833,18358,56278,1601,7354,47598,9509,52872,42,40610,31734,31753,22322,36992,17047,54787,7483,27163,8809,25491,35183,17156,21800,28115,49977,23158,4376,45525,55535,27986,49905,53785,43973,1568,53257,47722,39045,46922,26521,7166,25457,54026,55515,5212,11178,28615,36148,5403,29457,26258,9532,43841,51487,44875,52659,246,35050,8035,11885,36262,55220,47908,48759,18301,19433,15629,7745,10084,51963,37346,56219,21876,53387,22099,18642,32952,55986,28308,75,55870,45884,4286,52878,36779,23367,44834,4496,49992,54297,7559,56785,55526,39154,58139,13482,48189,3423,8784,22279,14660,30144,16426,16291,7249,59547,27077,28247,30127,20123,58178,33231,39826,61962,46740,32787,20843,55633,49687,3776,6782,25369,46623,58356,25718,5123,56224,20703,40289,20302,61267,57214,1052,44339,57320,6633,27407,39223,31101,22246,9119,54045,23456,50493,17854,12852,32314,468,11630,28538,20646,29716,27297,33757,2097,23455,61209,41210,38673,10104,15608,57786,9945,20341,28456,11161,14518,29257,43096,12390,18739,30448,27463,17346,3140,6827,500,30829,35181,15212,59606,25991,44299,39711,50384,54345,40473,56647,51391,11843,54934,8781,13699,31637,38225,10101,49648,49313,12919,25960,54156,5839,29946,61715,14777,42030,34248,12597,38642,10626,55921,36260,43072,40927,26773,26454,39465,26826,39595,55338,50220,41655,47471,18870,29379,24498,45358,26491,35785,55618,49829,41192,28044,54276,42830,29181,52731,35713,18919,58570,13101,30558,42625,1744,21922,48347,57073,43139,45139,39547,28131,33623,50487,52425,28545,34694,39831,5211,55983,13289,30979,53518,48606,16142,57170,32623,51579,28591,43110,33916,59474,12717,25677,5803,16152,54254,13156,47751,33501,49746,33628,14144,23691,645,7313,55919,40504,42762,7746,10467,60083,31712 };
auto res = Solution().minimumOperations(nums, target);
AssertEx(5573479LL, res);
}
类似题目
洛谷P3078: 某人有n堆牌,每次操作可以将i堆到第j堆各打一张牌出来。问最少多次可以打光牌。
扩展阅读
我想对大家说的话 |
---|
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
如果程序是一条龙,那算法就是他的是睛 |
失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176
测试环境
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。