mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
Due to problem 1300 test case change, so solution should modify
This commit is contained in:
@ -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 {
|
||||
|
@ -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},
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
```
|
Reference in New Issue
Block a user