给你一个整数数组 nums ,和两个整数 limit 与 goal 。数组 nums 有一条重要属性:abs(nums[i]) <= limit 。
返回使数组元素总和等于 goal 所需要向数组中添加的 最少元素数量 ,添加元素 不应改变 数组中 abs(nums[i]) <= limit 这一属性。
注意,如果 x >= 0 ,那么 abs(x) 等于 x ;否则,等于 -x 。
示例 1:
输入:nums = [1,-1,1], limit = 3, goal = -4
输出:2
解释:可以将 -2 和 -3 添加到数组中,数组的元素总和变为 1 - 1 + 1 - 2 - 3 = -4 。
示例 2:
输入:nums = [1,-10,9,1], limit = 100, goal = 0
输出:1
提示:
1 <= nums.length <= 105
1 <= limit <= 106
-limit <= nums[i] <= limit
-109 <= goal <= 109
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-elements-to-add-to-form-a-given-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
首先介绍一下我最开始采用的方法,由于这个方法在使用的时候需要考虑的地方不想对比较多,坑也比较多,耗时比较长,导致我在周测中的分数也不高,当然和本人的技术水平低也有很大的关系。
int get_cnt_and_goal_gap(long long cnt, int goal, int limit)
{
int gap = 0;
printf("line:%d cnt:%lld goal:%d limit:%d\n", __LINE__, cnt, goal, limit);
if (abs(cnt) <= abs(goal)) {
printf(" #line:%d cnt:%lld goal:%d limit:%d\n", __LINE__, cnt, goal, limit);
if (cnt != 0 && goal / cnt <= 0) {
printf(" #1#line:%d cnt:%lld goal:%d limit:%d\n", __LINE__, cnt, goal, limit);
gap = abs(goal/limit) + abs(cnt/limit) + (abs(goal%limit) + abs(cnt%limit))/limit;
printf(" #2#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
gap += (abs(goal%limit) + abs(cnt%limit))%limit ? 1 : 0;
printf(" #3#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
}
else if (cnt != 0 && goal / cnt > 0) {
printf(" #4#line:%d abs(abs(goal - cnt)):%lld\n", __LINE__, abs(abs(goal - cnt)));
gap = abs(abs((goal - cnt)/limit));
printf(" #4#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
gap += abs(abs((goal - cnt)%limit)) ? 1 : 0;
printf(" #4#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
}
else if (cnt == 0) {
gap = abs(goal)/limit;
printf(" #5#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
gap += abs(goal%limit) ? 1 : 0;
}
}
else {
if (goal != 0 && cnt / goal <= 0) {
printf(" #6#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
gap = abs(cnt/limit);
printf(" #6#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
gap += abs(goal/limit);
printf(" #6#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
gap += (abs(goal%limit) + abs(cnt%limit))/limit;
printf(" #6#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
gap += (abs(goal%limit) + abs(cnt%limit)%limit) ? 1 : 0;
printf(" #6#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
}
else if (goal != 0 && cnt / goal > 0) {
gap = abs(abs((cnt - goal)/limit));
printf(" #7#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
gap += abs(abs((cnt - goal)%limit)) ? 1 : 0;
}
else if (goal == 0) {
gap = abs(cnt/limit);
printf(" #8#line:%d cnt:%lld goal:%d limit:%d gap:%d\n", __LINE__, cnt, goal, limit, gap);
gap += abs(cnt%limit) ? 1 : 0;
}
}
return gap;
}
int minElements(int* nums, int numsSize, int limit, int goal){
long long cnt = 0;
long long gap_value = 0;
int num = 0;
for (int i = 0; i < numsSize; i ++) {
cnt += nums[i];
}
printf("cnt:%lld\n", cnt);
gap_value = get_cnt_and_goal_gap(cnt, goal, limit);
printf("line:%d gap_value:%d limit:%d num:%d\n", __LINE__, gap_value, limit, num);
return gap_value;
}
还有一种更为简单明了的方法,这种充分考虑了溢出的问题
int minElements(int* nums, int numsSize, int limit, int goal){
int res = 0;
long long sums = 0;
for (int i = 0; i < numsSize; i++) {
sums += nums[i];
}
res = abs((goal - sums)/limit);
res += abs((goal - sums)%limit) ? 1 : 0;
return res;
}