Due to problem 1300 test case change, so solution should modify

This commit is contained in:
YDZ
2020-09-18 12:15:02 +08:00
parent 3e301996c9
commit f56f6889bc
4 changed files with 51 additions and 0 deletions

View File

@ -10,6 +10,15 @@ func findBestValue(arr []int, target int) int {
high = mid
}
}
if high == 100000 {
res := 0
for _, num := range arr {
if res < num {
res = num
}
}
return res
}
// 比较阈值线分别定在 left - 1 和 left 的时候与 target 的接近程度
sum1, sum2 := calculateSum(arr, low-1), calculateSum(arr, low)
if target-sum1 <= sum2-target {

View File

@ -37,6 +37,12 @@ func Test_Problem1300(t *testing.T) {
ans1300{5},
},
// new case
{
para1300{[]int{2, 3, 5}, 11},
ans1300{5},
},
{
para1300{[]int{60864, 25176, 27249, 21296, 20204}, 56803},
ans1300{11361},

View File

@ -57,6 +57,7 @@ Output: 11361
## 代码
```go
func findBestValue(arr []int, target int) int {
low, high := 0, 100000
for low < high {
@ -67,6 +68,15 @@ func findBestValue(arr []int, target int) int {
high = mid
}
}
if high == 100000 {
res := 0
for _, num := range arr {
if res < num {
res = num
}
}
return res
}
// 比较阈值线分别定在 left - 1 和 left 的时候与 target 的接近程度
sum1, sum2 := calculateSum(arr, low-1), calculateSum(arr, low)
if target-sum1 <= sum2-target {
@ -82,4 +92,12 @@ func calculateSum(arr []int, mid int) int {
}
return sum
}
func min(a int, b int) int {
if a > b {
return b
}
return a
}
```