Skip to content

Commit 3a2f409

Browse files
committedJul 3, 2024·
feat: solve No.350,1509
1 parent adde345 commit 3a2f409

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# 1509. Minimum Difference Between Largest and Smallest Value in Three Moves
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Greedy, Sorting.
5+
- Similar Questions: Minimize the Maximum Difference of Pairs.
6+
7+
## Problem
8+
9+
You are given an integer array `nums`.
10+
11+
In one move, you can choose one element of `nums` and change it to **any value**.
12+
13+
Return **the minimum difference between the largest and smallest value of `nums` **after performing at most three moves****.
14+
15+
 
16+
Example 1:
17+
18+
```
19+
Input: nums = [5,3,2,4]
20+
Output: 0
21+
Explanation: We can make at most 3 moves.
22+
In the first move, change 2 to 3. nums becomes [5,3,3,4].
23+
In the second move, change 4 to 3. nums becomes [5,3,3,3].
24+
In the third move, change 5 to 3. nums becomes [3,3,3,3].
25+
After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.
26+
```
27+
28+
Example 2:
29+
30+
```
31+
Input: nums = [1,5,0,10,14]
32+
Output: 1
33+
Explanation: We can make at most 3 moves.
34+
In the first move, change 5 to 0. nums becomes [1,0,0,10,14].
35+
In the second move, change 10 to 0. nums becomes [1,0,0,0,14].
36+
In the third move, change 14 to 1. nums becomes [1,0,0,0,1].
37+
After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.
38+
It can be shown that there is no way to make the difference 0 in 3 moves.
39+
```
40+
41+
Example 3:
42+
43+
```
44+
Input: nums = [3,100,20]
45+
Output: 0
46+
Explanation: We can make at most 3 moves.
47+
In the first move, change 100 to 7. nums becomes [3,7,20].
48+
In the second move, change 20 to 7. nums becomes [3,7,7].
49+
In the third move, change 3 to 7. nums becomes [7,7,7].
50+
After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.
51+
```
52+
53+
 
54+
**Constraints:**
55+
56+
57+
58+
- `1 <= nums.length <= 105`
59+
60+
- `-109 <= nums[i] <= 109`
61+
62+
63+
64+
## Solution
65+
66+
```javascript
67+
/**
68+
* @param {number[]} nums
69+
* @return {number}
70+
*/
71+
var minDifference = function(nums) {
72+
if (nums.length <= 4) return 0;
73+
nums.sort((a, b) => a - b);
74+
return Math.min(
75+
nums[nums.length - 4] - nums[0],
76+
nums[nums.length - 3] - nums[1],
77+
nums[nums.length - 2] - nums[2],
78+
nums[nums.length - 1] - nums[3],
79+
);
80+
};
81+
```
82+
83+
**Explain:**
84+
85+
nope.
86+
87+
**Complexity:**
88+
89+
* Time complexity : O(n * log(n)).
90+
* Space complexity : O(n).
91+
92+
## Solution 2
93+
94+
```javascript
95+
/**
96+
* @param {number[]} nums
97+
* @return {number}
98+
*/
99+
var minDifference = function(nums) {
100+
if (nums.length <= 4) return 0;
101+
var minQueue = new MinPriorityQueue();
102+
var maxQueue = new MaxPriorityQueue();
103+
for (var i = 0; i < nums.length; i++) {
104+
minQueue.enqueue(nums[i], nums[i]);
105+
if (minQueue.size() > 4) {
106+
minQueue.dequeue();
107+
}
108+
maxQueue.enqueue(nums[i], nums[i]);
109+
if (maxQueue.size() > 4) {
110+
maxQueue.dequeue();
111+
}
112+
}
113+
const arr = [
114+
...maxQueue.toArray().map(item => item.element).reverse(),
115+
...minQueue.toArray().map(item => item.element),
116+
];
117+
return Math.min(
118+
arr[arr.length - 4] - arr[0],
119+
arr[arr.length - 3] - arr[1],
120+
arr[arr.length - 2] - arr[2],
121+
arr[arr.length - 1] - arr[3],
122+
);
123+
};
124+
```
125+
126+
**Explain:**
127+
128+
nope.
129+
130+
**Complexity:**
131+
132+
* Time complexity : O(n * log(4) * log(4)) = O(n).
133+
* Space complexity : O(1).

Diff for: ‎301-400/350. Intersection of Two Arrays II.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 350. Intersection of Two Arrays II
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Hash Table, Two Pointers, Binary Search, Sorting.
5+
- Similar Questions: Intersection of Two Arrays, Find Common Characters, Find the Difference of Two Arrays, Choose Numbers From Two Arrays in Range, Intersection of Multiple Arrays, Minimum Common Value.
6+
7+
## Problem
8+
9+
Given two integer arrays `nums1` and `nums2`, return **an array of their intersection**. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
10+
11+
 
12+
Example 1:
13+
14+
```
15+
Input: nums1 = [1,2,2,1], nums2 = [2,2]
16+
Output: [2,2]
17+
```
18+
19+
Example 2:
20+
21+
```
22+
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
23+
Output: [4,9]
24+
Explanation: [9,4] is also accepted.
25+
```
26+
27+
 
28+
**Constraints:**
29+
30+
31+
32+
- `1 <= nums1.length, nums2.length <= 1000`
33+
34+
- `0 <= nums1[i], nums2[i] <= 1000`
35+
36+
37+
 
38+
**Follow up:**
39+
40+
41+
42+
- What if the given array is already sorted? How would you optimize your algorithm?
43+
44+
- What if `nums1`'s size is small compared to `nums2`'s size? Which algorithm is better?
45+
46+
- What if elements of `nums2` are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
47+
48+
49+
50+
## Solution
51+
52+
```javascript
53+
/**
54+
* @param {number[]} nums1
55+
* @param {number[]} nums2
56+
* @return {number[]}
57+
*/
58+
var intersect = function(nums1, nums2) {
59+
var map = {};
60+
for (var i = 0; i < nums1.length; i++) {
61+
map[nums1[i]] = (map[nums1[i]] || 0) + 1;
62+
}
63+
var res = [];
64+
for (var j = 0; j < nums2.length; j++) {
65+
if (map[nums2[j]]) {
66+
map[nums2[j]]--;
67+
res.push(nums2[j]);
68+
}
69+
}
70+
return res;
71+
};
72+
```
73+
74+
**Explain:**
75+
76+
nope.
77+
78+
**Complexity:**
79+
80+
* Time complexity : O(n + m).
81+
* Space complexity : O(n + m).

0 commit comments

Comments
 (0)