You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Solution {
public int minOperations(int n) {
int target = n % 2 == 0 ? 2 * (n / 2 - 1) + 2 : 2 * (n / 2) + 1;
int ans = 0;
for (int i = 0; i < n; ++i) {
int num = 2 * i + 1;
ans += Math.abs(target - num);
}
return ans / 2;
}
}
class Solution {
public long minOperations(int[] nums1, int[] nums2, int k) {
int n = nums1.length;
long diffSum = 0;
long negativeDiffSum = 0;
long ans = 0;
for (int i = 0; i < n; i++) {
int diff = nums1[i] - nums2[i];
if (diff == 0) {
continue;
}
if (k == 0) {
return -1;
}
if (diff % k != 0) {
return -1;
}
diff /= k;
if (diff < 0) {
negativeDiffSum += diff;
}
diffSum += diff;
}
if (diffSum != 0) {
return -1;
}
return -negativeDiffSum;
}
}
class Solution {
public long makeSimilar(int[] nums, int[] target) {
List<Integer> evenList = new ArrayList<>(), oddList = new ArrayList<>();
Arrays.sort(nums);
Arrays.sort(target);
for (int num : nums) {
if (num % 2 == 0) {
evenList.add(num);
} else {
oddList.add(num);
}
}
int evenIndex = 0, oddIndex = 0;
long ans = 0;
for (int t : target) {
long op = t % 2 == 0 ? (evenList.get(evenIndex++) - t) / 2 : (oddList.get(oddIndex++) - t) / 2;
ans += op > 0 ? op : 0;
}
return ans;
}
}
这道题难点在于:
分组
排序
The text was updated successfully, but these errors were encountered:
这是一系列变种题的系列最终hard题目。
题目有:
题目类型是:
解题关键:
那么先看1551题,第一题要用贪心思想,找到中间值,然后累加步数即可。
再看2541题,这是双周赛96的第二题,我WA了3次才过,有点不应该了。这题不难,主要是要仔细耐心,注意细节。如下:
来到最后一题2449。
观察条件,我们可以得到很有意思的点,这些都是破题的关键:
如果能彻底理解以上三点,就能够得出题解了。
思路:首先,我们要找到每个元素变换的目标,也就是能够变化达到的距离最近的目标;又因为题目保证了变化能够达到,所以我们不用担心找不到两个对应的索引能够互相成就变化达到各自的目标;那么如何寻找各自的目标呢?需要分组排序。我们通过条件1,可以知道要分成奇偶两组 ,然后对两组从小到大排序,就可以找到两组内每个元素对应的目标了;最后,根据上一题的思路,我们只需要记录正差值或者负差值作为答案即可。
这道题难点在于:
The text was updated successfully, but these errors were encountered: