Skip to content

Commit dcaecb5

Browse files
committed
feat: add solutions to lc problem: No.1509
No.1509.Minimum Difference Between Largest and Smallest Value in Three Moves
1 parent 0a2ffb2 commit dcaecb5

File tree

7 files changed

+220
-2
lines changed

7 files changed

+220
-2
lines changed

solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,104 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:排序 + 贪心**
56+
57+
我们可以先判断数组长度是否小于 $5$,如果小于 $5$,那么直接返回 $0$。
58+
59+
否则,我们将数组排序,然后贪心地选择数组左边最小的 $l=[0,..3]$ 个数和右边最小的 $r = 3 - l$ 个数,取其中最小的差值 $nums[n - 1 - r] - nums[l]$ 即可。
60+
61+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `nums` 的长度。
62+
63+
相似题目:
64+
65+
- [2567. 修改两个元素的最小分数](/solution/2500-2599/2567.Minimum%20Score%20by%20Changing%20Two%20Elements/README.md)
66+
5567
<!-- tabs:start -->
5668

5769
### **Python3**
5870

5971
<!-- 这里可写当前语言的特殊实现逻辑 -->
6072

6173
```python
62-
74+
class Solution:
75+
def minDifference(self, nums: List[int]) -> int:
76+
n = len(nums)
77+
if n < 5:
78+
return 0
79+
nums.sort()
80+
ans = inf
81+
for l in range(4):
82+
r = 3 - l
83+
ans = min(ans, nums[n - 1 - r] - nums[l])
84+
return ans
6385
```
6486

6587
### **Java**
6688

6789
<!-- 这里可写当前语言的特殊实现逻辑 -->
6890

6991
```java
92+
class Solution {
93+
public int minDifference(int[] nums) {
94+
int n = nums.length;
95+
if (n < 5) {
96+
return 0;
97+
}
98+
Arrays.sort(nums);
99+
long ans = 1L << 60;
100+
for (int l = 0; l <= 3; ++l) {
101+
int r = 3 - l;
102+
ans = Math.min(ans, (long) nums[n - 1 - r] - nums[l]);
103+
}
104+
return (int) ans;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int minDifference(vector<int>& nums) {
115+
int n = nums.size();
116+
if (n < 5) {
117+
return 0;
118+
}
119+
sort(nums.begin(), nums.end());
120+
long long ans = 1L << 60;
121+
for (int l = 0; l <= 3; ++l) {
122+
int r = 3 - l;
123+
ans = min(ans, 1LL * nums[n - 1 - r] - nums[l]);
124+
}
125+
return ans;
126+
}
127+
};
128+
```
70129
130+
### **Go**
131+
132+
```go
133+
func minDifference(nums []int) int {
134+
n := len(nums)
135+
if n < 5 {
136+
return 0
137+
}
138+
sort.Ints(nums)
139+
ans := 1 << 60
140+
for l := 0; l <= 3; l++ {
141+
r := 3 - l
142+
ans = min(ans, nums[n-1-r]-nums[l])
143+
}
144+
return ans
145+
}
146+
147+
func min(a, b int) int {
148+
if a < b {
149+
return a
150+
}
151+
return b
152+
}
71153
```
72154

73155
### **...**

solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README_EN.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,83 @@ After performing 3 moves, the difference between the minimum and maximum is 7 -
6262
### **Python3**
6363

6464
```python
65-
65+
class Solution:
66+
def minDifference(self, nums: List[int]) -> int:
67+
n = len(nums)
68+
if n < 5:
69+
return 0
70+
nums.sort()
71+
ans = inf
72+
for l in range(4):
73+
r = 3 - l
74+
ans = min(ans, nums[n - 1 - r] - nums[l])
75+
return ans
6676
```
6777

6878
### **Java**
6979

7080
```java
81+
class Solution {
82+
public int minDifference(int[] nums) {
83+
int n = nums.length;
84+
if (n < 5) {
85+
return 0;
86+
}
87+
Arrays.sort(nums);
88+
long ans = 1L << 60;
89+
for (int l = 0; l <= 3; ++l) {
90+
int r = 3 - l;
91+
ans = Math.min(ans, (long) nums[n - 1 - r] - nums[l]);
92+
}
93+
return (int) ans;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int minDifference(vector<int>& nums) {
104+
int n = nums.size();
105+
if (n < 5) {
106+
return 0;
107+
}
108+
sort(nums.begin(), nums.end());
109+
long long ans = 1L << 60;
110+
for (int l = 0; l <= 3; ++l) {
111+
int r = 3 - l;
112+
ans = min(ans, 1LL * nums[n - 1 - r] - nums[l]);
113+
}
114+
return ans;
115+
}
116+
};
117+
```
71118
119+
### **Go**
120+
121+
```go
122+
func minDifference(nums []int) int {
123+
n := len(nums)
124+
if n < 5 {
125+
return 0
126+
}
127+
sort.Ints(nums)
128+
ans := 1 << 60
129+
for l := 0; l <= 3; l++ {
130+
r := 3 - l
131+
ans = min(ans, nums[n-1-r]-nums[l])
132+
}
133+
return ans
134+
}
135+
136+
func min(a, b int) int {
137+
if a < b {
138+
return a
139+
}
140+
return b
141+
}
72142
```
73143

74144
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int minDifference(vector<int>& nums) {
4+
int n = nums.size();
5+
if (n < 5) {
6+
return 0;
7+
}
8+
sort(nums.begin(), nums.end());
9+
long long ans = 1L << 60;
10+
for (int l = 0; l <= 3; ++l) {
11+
int r = 3 - l;
12+
ans = min(ans, 1LL * nums[n - 1 - r] - nums[l]);
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func minDifference(nums []int) int {
2+
n := len(nums)
3+
if n < 5 {
4+
return 0
5+
}
6+
sort.Ints(nums)
7+
ans := 1 << 60
8+
for l := 0; l <= 3; l++ {
9+
r := 3 - l
10+
ans = min(ans, nums[n-1-r]-nums[l])
11+
}
12+
return ans
13+
}
14+
15+
func min(a, b int) int {
16+
if a < b {
17+
return a
18+
}
19+
return b
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int minDifference(int[] nums) {
3+
int n = nums.length;
4+
if (n < 5) {
5+
return 0;
6+
}
7+
Arrays.sort(nums);
8+
long ans = 1L << 60;
9+
for (int l = 0; l <= 3; ++l) {
10+
int r = 3 - l;
11+
ans = Math.min(ans, (long) nums[n - 1 - r] - nums[l]);
12+
}
13+
return (int) ans;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def minDifference(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
if n < 5:
5+
return 0
6+
nums.sort()
7+
ans = inf
8+
for l in range(4):
9+
r = 3 - l
10+
ans = min(ans, nums[n - 1 - r] - nums[l])
11+
return ans

solution/2500-2599/2567.Minimum Score by Changing Two Elements/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969

7070
时间复杂度 $O(n \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `nums` 的长度。
7171

72+
相似题目:
73+
74+
- [1509. 三次操作后最大值与最小值的最小差](/solution/1500-1599/1509.Minimum%20Difference%20Between%20Largest%20and%20Smallest%20Value%20in%20Three%20Moves/README.md)
75+
7276
<!-- tabs:start -->
7377

7478
### **Python3**

0 commit comments

Comments
 (0)