Conflicts:
	src/main/java/leetcode/editor/cn/all.json
This commit is contained in:
huangge1199 2021-07-15 12:08:39 +08:00
commit 25cbdf372c
5 changed files with 302 additions and 0 deletions

View File

@ -0,0 +1,132 @@
//给你两个正整数数组 nums1 nums2 数组的长度都是 n
//
// 数组 nums1 nums2 绝对差值和 定义为所有 |nums1[i] - nums2[i]|0 <= i < n 总和下标从 0 开始
//
//
// 你可以选用 nums1 中的 任意一个 元素来替换 nums1 中的 至多 一个元素 最小化 绝对差值和
//
// 在替换数组 nums1 中最多一个元素 之后 返回最小绝对差值和因为答案可能很大所以需要对 109 + 7 取余 后返回
//
// |x| 定义为
//
//
// 如果 x >= 0 值为 x 或者
// 如果 x <= 0 值为 -x
//
//
//
//
// 示例 1
//
//
//输入nums1 = [1,7,5], nums2 = [2,3,5]
//输出3
//解释有两种可能的最优方案
//- 将第二个元素替换为第一个元素[1,7,5] => [1,1,5] 或者
//- 将第二个元素替换为第三个元素[1,7,5] => [1,5,5]
//两种方案的绝对差值和都是 |1-2| + (|1-3| 或者 |5-3|) + |5-5| = 3
//
//
// 示例 2
//
//
//输入nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]
//输出0
//解释nums1 nums2 相等所以不用替换元素绝对差值和为 0
//
//
// 示例 3
//
//
//输入nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]
//输出20
//解释将第一个元素替换为第二个元素[1,10,4,4,2,7] => [10,10,4,4,2,7]
//绝对差值和为 |10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20
//
//
//
//
// 提示
//
//
// n == nums1.length
// n == nums2.length
// 1 <= n <= 105
// 1 <= nums1[i], nums2[i] <= 105
//
// Related Topics 贪心 数组 二分查找 有序集合
// 👍 74 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
//1818:绝对差值和
class MinimumAbsoluteSumDifference {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumAbsoluteSumDifference().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minAbsoluteSumDiff(int[] nums1, int[] nums2) {
final int MOD = 1000000007;
//创建 nums1 副本 并排序
int[] temp = Arrays.copyOf(nums1, nums1.length);
Arrays.sort(temp);
//求解 minDif 数组
int len = nums1.length;
int[] minDif = new int[len];
for (int i = 0; i < len; i++) {
int[] arr = leftright(temp, nums2[i]);
if (nums2[i] - temp[arr[0]] < temp[arr[1]] - nums2[i])
minDif[i] = temp[arr[0]];
else
minDif[i] = temp[arr[1]];
}
//寻找合适的元素替换nums1中的元素
int index = 0;
int max = 0;
for (int i = 0; i < len; i++) {
int dif = Math.abs(nums1[i] - nums2[i]) - Math.abs(minDif[i] - nums2[i]);
if (dif > max) {
index = i;
max = dif;
}
}
nums1[index] = minDif[index];
//累加和
int sum = 0;
for (int i = 0; i < len; i++)
sum = (sum + Math.abs(nums1[i] - nums2[i])) % MOD;
return sum;
}
//二分查找
public int[] leftright(int[] arr, int value) {
int left = 0;
int right = arr.length - 1;
int[] result = new int[2];
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == value) {
result[0] = result[1] = mid;
return result;
} else if (arr[mid] < value)
left = mid + 1;
else
right = mid - 1;
}
result[0] = left > 0 ? left - 1 : 0;
result[1] = right < arr.length - 1 ? right + 1 : arr.length - 1;
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,56 @@
<p>给你两个正整数数组 <code>nums1</code><code>nums2</code> ,数组的长度都是 <code>n</code></p>
<p>数组 <code>nums1</code><code>nums2</code><strong>绝对差值和</strong> 定义为所有 <code>|nums1[i] - nums2[i]|</code><code>0 <= i < n</code>)的 <strong>总和</strong><strong>下标从 0 开始</strong>)。</p>
<p>你可以选用 <code>nums1</code> 中的 <strong>任意一个</strong> 元素来替换 <code>nums1</code> 中的 <strong>至多</strong> 一个元素,以 <strong>最小化</strong> 绝对差值和。</p>
<p>在替换数组 <code>nums1</code> 中最多一个元素 <strong>之后</strong> ,返回最小绝对差值和。因为答案可能很大,所以需要对 <code>10<sup>9</sup> + 7</code> <strong>取余 </strong>后返回。</p>
<p><code>|x|</code> 定义为:</p>
<ul>
<li>如果 <code>x >= 0</code> ,值为 <code>x</code> ,或者</li>
<li>如果 <code>x <= 0</code> ,值为 <code>-x</code></li>
</ul>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums1 = [1,7,5], nums2 = [2,3,5]
<strong>输出:</strong>3
<strong>解释:</strong>有两种可能的最优方案:
- 将第二个元素替换为第一个元素:[1,<strong>7</strong>,5] => [1,<strong>1</strong>,5] ,或者
- 将第二个元素替换为第三个元素:[1,<strong>7</strong>,5] => [1,<strong>5</strong>,5]
两种方案的绝对差值和都是 <code>|1-2| + (|1-3| 或者 |5-3|) + |5-5| = </code>3
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10]
<strong>输出:</strong>0
<strong>解释:</strong>nums1 和 nums2 相等,所以不用替换元素。绝对差值和为 0
</pre>
<p><strong>示例 3</strong><strong></strong></p>
<pre>
<strong>输入:</strong>nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4]
<strong>输出:</strong>20
<strong>解释:</strong>将第一个元素替换为第二个元素:[<strong>1</strong>,10,4,4,2,7] => [<strong>10</strong>,10,4,4,2,7]
绝对差值和为 <code>|10-9| + |10-3| + |4-5| + |4-1| + |2-7| + |7-4| = 20</code>
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums1[i], nums2[i] <= 10<sup>5</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>二分查找</li><li>有序集合</li></div></div>\n<div><li>👍 74</li><li>👎 0</li></div>

View File

@ -0,0 +1,85 @@
//给你一个整数数组 nums请你将该数组升序排列
//
//
//
//
//
//
// 示例 1
//
// 输入nums = [5,2,3,1]
//输出[1,2,3,5]
//
//
// 示例 2
//
// 输入nums = [5,1,1,2,0,0]
//输出[0,0,1,1,2,5]
//
//
//
//
// 提示
//
//
// 1 <= nums.length <= 50000
// -50000 <= nums[i] <= 50000
//
// Related Topics 数组 分治 桶排序 计数排序 基数排序 排序 优先队列 归并排序
// 👍 315 👎 0
package leetcode.editor.cn;
import java.util.Random;
//912:排序数组
class SortAnArray {
public static void main(String[] args) {
//测试代码
Solution solution = new SortAnArray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
private Random random;
public int[] sortArray(int[] nums) {
random=new Random(System.currentTimeMillis());
pivot(nums, 0, nums.length - 1);
return nums;
}
private void pivot(int[] nums, int start, int end) {
if (start >= end)
return;
int mid = random.nextInt(end - start + 1) + start;
swap(nums, start, mid);
int num = nums[start];
int left = start + 1;
int right = end;
while (left < right) {
while (left < right && nums[left] <= num)
left++;
while (left < right && nums[right] >= num)
right--;
swap(nums, left, right);
}
if (nums[left] <= num)
swap(nums, left, start);
else {
swap(nums, left - 1, start);
left--;
}
pivot(nums, left + 1, end);
pivot(nums, start, left - 1);
}
private void swap(int[] nums, int start, int end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,28 @@
<p>给你一个整数数组&nbsp;<code>nums</code>,请你将该数组升序排列。</p>
<p>&nbsp;</p>
<ol>
</ol>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [5,2,3,1]
<strong>输出:</strong>[1,2,3,5]
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [5,1,1,2,0,0]
<strong>输出:</strong>[0,0,1,1,2,5]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li><code>1 &lt;= nums.length &lt;= 50000</code></li>
<li><code>-50000 &lt;= nums[i] &lt;= 50000</code></li>
</ol>
<div><div>Related Topics</div><div><li>数组</li><li>分治</li><li>桶排序</li><li>计数排序</li><li>基数排序</li><li>排序</li><li>堆(优先队列)</li><li>归并排序</li></div></div>\n<div><li>👍 315</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long