Skip to content

Commit adf9804

Browse files
authored
Merge pull request halfrost#70 from halfrost/fix_1300
Due to problem 1300 test case change, so solution should modify
2 parents 3e30199 + f56f688 commit adf9804

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

leetcode/1300.Sum-of-Mutated-Array-Closest-to-Target/1300. Sum of Mutated Array Closest to Target.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ func findBestValue(arr []int, target int) int {
1010
high = mid
1111
}
1212
}
13+
if high == 100000 {
14+
res := 0
15+
for _, num := range arr {
16+
if res < num {
17+
res = num
18+
}
19+
}
20+
return res
21+
}
1322
// 比较阈值线分别定在 left - 1 和 left 的时候与 target 的接近程度
1423
sum1, sum2 := calculateSum(arr, low-1), calculateSum(arr, low)
1524
if target-sum1 <= sum2-target {

leetcode/1300.Sum-of-Mutated-Array-Closest-to-Target/1300. Sum of Mutated Array Closest to Target_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ func Test_Problem1300(t *testing.T) {
3737
ans1300{5},
3838
},
3939

40+
// new case
41+
{
42+
para1300{[]int{2, 3, 5}, 11},
43+
ans1300{5},
44+
},
45+
4046
{
4147
para1300{[]int{60864, 25176, 27249, 21296, 20204}, 56803},
4248
ans1300{11361},

leetcode/1300.Sum-of-Mutated-Array-Closest-to-Target/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Output: 11361
5757
## 代码
5858

5959
```go
60+
6061
func findBestValue(arr []int, target int) int {
6162
low, high := 0, 100000
6263
for low < high {
@@ -67,6 +68,15 @@ func findBestValue(arr []int, target int) int {
6768
high = mid
6869
}
6970
}
71+
if high == 100000 {
72+
res := 0
73+
for _, num := range arr {
74+
if res < num {
75+
res = num
76+
}
77+
}
78+
return res
79+
}
7080
// 比较阈值线分别定在 left - 1 和 left 的时候与 target 的接近程度
7181
sum1, sum2 := calculateSum(arr, low-1), calculateSum(arr, low)
7282
if target-sum1 <= sum2-target {
@@ -82,4 +92,12 @@ func calculateSum(arr []int, mid int) int {
8292
}
8393
return sum
8494
}
95+
96+
func min(a int, b int) int {
97+
if a > b {
98+
return b
99+
}
100+
return a
101+
}
102+
85103
```

website/content/ChapterFour/1300.Sum-of-Mutated-Array-Closest-to-Target.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Output: 11361
5757
## 代码
5858

5959
```go
60+
6061
func findBestValue(arr []int, target int) int {
6162
low, high := 0, 100000
6263
for low < high {
@@ -67,6 +68,15 @@ func findBestValue(arr []int, target int) int {
6768
high = mid
6869
}
6970
}
71+
if high == 100000 {
72+
res := 0
73+
for _, num := range arr {
74+
if res < num {
75+
res = num
76+
}
77+
}
78+
return res
79+
}
7080
// 比较阈值线分别定在 left - 1 和 left 的时候与 target 的接近程度
7181
sum1, sum2 := calculateSum(arr, low-1), calculateSum(arr, low)
7282
if target-sum1 <= sum2-target {
@@ -82,4 +92,12 @@ func calculateSum(arr []int, mid int) int {
8292
}
8393
return sum
8494
}
95+
96+
func min(a int, b int) int {
97+
if a > b {
98+
return b
99+
}
100+
return a
101+
}
102+
85103
```

0 commit comments

Comments
 (0)