2024-12-02 19:28:55 +00:00
|
|
|
package two
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"slices"
|
|
|
|
)
|
|
|
|
|
2024-12-02 22:39:56 +00:00
|
|
|
func withDampener(nums []int, cmp comparator, count int) bool {
|
2024-12-02 19:28:55 +00:00
|
|
|
if count > 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < len(nums); i++ {
|
2024-12-02 22:39:56 +00:00
|
|
|
if cmp(nums, i) || !isSafe(nums[i-1], nums[i]) {
|
2024-12-02 19:28:55 +00:00
|
|
|
count++
|
|
|
|
removeFirst, removeSecond := nextLevels(nums, i)
|
2024-12-02 22:39:56 +00:00
|
|
|
return withDampener(removeFirst, cmp, count) ||
|
|
|
|
withDampener(removeSecond, cmp, count)
|
2024-12-02 19:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSafe(m, n int) bool {
|
|
|
|
return int(math.Abs(float64(m-n))) <= 3
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextLevels(nums []int, i int) ([]int, []int) {
|
|
|
|
first, second := slices.Clone(nums), slices.Clone(nums)
|
|
|
|
return append(first[:i-1], first[i:]...), append(second[:i], second[i+1:]...)
|
|
|
|
}
|