Skip to content

Commit 354c802

Browse files
committed
Merge pull request halfrost#84 from frankegoesdown/0910-smallest-range-ii
add 0910 smallest-range-ii
2 parents c117dfd + fcd2655 commit 354c802

File tree

5 files changed

+243
-0
lines changed

5 files changed

+243
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
func smallestRangeII(A []int, K int) int {
6+
n := len(A)
7+
sort.Ints(A)
8+
res := A[n-1] - A[0]
9+
for i := 0; i < n-1; i++ {
10+
a, b := A[i], A[i+1]
11+
high := max(A[n-1]-K, a+K)
12+
low := min(A[0]+K, b-K)
13+
res = min(res, high-low)
14+
}
15+
return res
16+
}
17+
18+
func max(a, b int) int {
19+
if a > b {
20+
return a
21+
}
22+
return b
23+
}
24+
25+
func min(a, b int) int {
26+
if a < b {
27+
return a
28+
}
29+
return b
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question910 struct {
9+
para910
10+
ans910
11+
}
12+
13+
type para910 struct {
14+
A []int
15+
K int
16+
}
17+
18+
type ans910 struct {
19+
one int
20+
}
21+
22+
// Test_Problem910 ...
23+
func Test_Problem910(t *testing.T) {
24+
25+
qs := []question910{
26+
27+
{
28+
para910{[]int{1}, 0},
29+
ans910{0},
30+
},
31+
{
32+
para910{[]int{0, 10}, 2},
33+
ans910{6},
34+
},
35+
{
36+
para910{[]int{1, 3, 6}, 3},
37+
ans910{3},
38+
},
39+
}
40+
41+
fmt.Printf("------------------------Leetcode Problem 910------------------------\n")
42+
43+
for _, q := range qs {
44+
_, p := q.ans910, q.para910
45+
fmt.Printf("【input】:%v 【output】:%v\n", p, smallestRangeII(p.A, p.K))
46+
}
47+
fmt.Printf("\n\n\n")
48+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [910. Smallest Range II](https://leetcode.com/problems/smallest-range-ii/)
2+
3+
## 题目
4+
5+
Given an array `A` of integers, for each integer `A[i]` we need to choose **either `x = -K` or `x = K`**, and add `x` to `A[i]` **(only once)**.
6+
7+
After this process, we have some array `B`.
8+
9+
Return the smallest possible difference between the maximum value of `B` and the minimum value of `B`.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: A = [1], K = 0
15+
Output: 0
16+
Explanation: B = [1]
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: A = [0,10], K = 2
23+
Output: 6
24+
Explanation: B = [2,8]
25+
```
26+
27+
**Example 3:**
28+
29+
```
30+
Input: A = [1,3,6], K = 3
31+
Output: 3
32+
Explanation: B = [4,6,3]
33+
```
34+
35+
**Note:**
36+
37+
1. `1 <= A.length <= 10000`
38+
2. `0 <= A[i] <= 10000`
39+
3. `0 <= K <= 10000`
40+
41+
## 题目大意
42+
43+
给你一个整数数组 A,对于每个整数 A[i],可以选择 x = -K 或是 x = K (K 总是非负整数),并将 x 加到 A[i] 中。在此过程之后,得到数组 B。返回 B 的最大值和 B 的最小值之间可能存在的最小差值。
44+
45+
## 解题思路
46+
47+
- 简单题。先排序,找出 A 数组中最大的差值。然后循环扫一遍数组,利用双指针,选择 x = -K 或是 x = K ,每次选择都更新一次最大值和最小值之间的最小差值。循环一次以后便可以找到满足题意的答案。
48+
49+
## 代码
50+
51+
```go
52+
package leetcode
53+
54+
import "sort"
55+
56+
func smallestRangeII(A []int, K int) int {
57+
n := len(A)
58+
sort.Ints(A)
59+
res := A[n-1] - A[0]
60+
for i := 0; i < n-1; i++ {
61+
a, b := A[i], A[i+1]
62+
high := max(A[n-1]-K, a+K)
63+
low := min(A[0]+K, b-K)
64+
res = min(res, high-low)
65+
}
66+
return res
67+
}
68+
69+
func max(a, b int) int {
70+
if a > b {
71+
return a
72+
}
73+
return b
74+
}
75+
76+
func min(a, b int) int {
77+
if a < b {
78+
return a
79+
}
80+
return b
81+
}
82+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# [910. Smallest Range II](https://leetcode.com/problems/smallest-range-ii/)
2+
3+
## 题目
4+
5+
Given an array `A` of integers, for each integer `A[i]` we need to choose **either `x = -K` or `x = K`**, and add `x` to `A[i]` **(only once)**.
6+
7+
After this process, we have some array `B`.
8+
9+
Return the smallest possible difference between the maximum value of `B` and the minimum value of `B`.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: A = [1], K = 0
15+
Output: 0
16+
Explanation: B = [1]
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: A = [0,10], K = 2
23+
Output: 6
24+
Explanation: B = [2,8]
25+
```
26+
27+
**Example 3:**
28+
29+
```
30+
Input: A = [1,3,6], K = 3
31+
Output: 3
32+
Explanation: B = [4,6,3]
33+
```
34+
35+
**Note:**
36+
37+
1. `1 <= A.length <= 10000`
38+
2. `0 <= A[i] <= 10000`
39+
3. `0 <= K <= 10000`
40+
41+
## 题目大意
42+
43+
给你一个整数数组 A,对于每个整数 A[i],可以选择 x = -K 或是 x = K (K 总是非负整数),并将 x 加到 A[i] 中。在此过程之后,得到数组 B。返回 B 的最大值和 B 的最小值之间可能存在的最小差值。
44+
45+
## 解题思路
46+
47+
- 简单题。先排序,找出 A 数组中最大的差值。然后循环扫一遍数组,利用双指针,选择 x = -K 或是 x = K ,每次选择都更新一次最大值和最小值之间的最小差值。循环一次以后便可以找到满足题意的答案。
48+
49+
## 代码
50+
51+
```go
52+
package leetcode
53+
54+
import "sort"
55+
56+
func smallestRangeII(A []int, K int) int {
57+
n := len(A)
58+
sort.Ints(A)
59+
res := A[n-1] - A[0]
60+
for i := 0; i < n-1; i++ {
61+
a, b := A[i], A[i+1]
62+
high := max(A[n-1]-K, a+K)
63+
low := min(A[0]+K, b-K)
64+
res = min(res, high-low)
65+
}
66+
return res
67+
}
68+
69+
func max(a, b int) int {
70+
if a > b {
71+
return a
72+
}
73+
return b
74+
}
75+
76+
func min(a, b int) int {
77+
if a < b {
78+
return a
79+
}
80+
return b
81+
}
82+
```

website/content/menu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ headless: true
428428
- [0901.Online-Stock-Span]({{< relref "/ChapterFour/0901.Online-Stock-Span.md" >}})
429429
- [0904.Fruit-Into-Baskets]({{< relref "/ChapterFour/0904.Fruit-Into-Baskets.md" >}})
430430
- [0907.Sum-of-Subarray-Minimums]({{< relref "/ChapterFour/0907.Sum-of-Subarray-Minimums.md" >}})
431+
- [0910.Smallest-Range-II]({{< relref "/ChapterFour/0910.Smallest-Range-II.md" >}})
431432
- [0911.Online-Election]({{< relref "/ChapterFour/0911.Online-Election.md" >}})
432433
- [0914.X-of-a-Kind-in-a-Deck-of-Cards]({{< relref "/ChapterFour/0914.X-of-a-Kind-in-a-Deck-of-Cards.md" >}})
433434
- [0918.Maximum-Sum-Circular-Subarray]({{< relref "/ChapterFour/0918.Maximum-Sum-Circular-Subarray.md" >}})

0 commit comments

Comments
 (0)