2024-12-02 19:28:55 +00:00
|
|
|
package two
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"slices"
|
|
|
|
)
|
|
|
|
|
2024-12-02 22:28:14 +00:00
|
|
|
func withComparator(nums []int, comparator comparator, count int) bool {
|
2024-12-02 19:28:55 +00:00
|
|
|
if count > 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < len(nums); i++ {
|
|
|
|
if comparator(nums, i) || !isSafe(nums[i-1], nums[i]) {
|
|
|
|
count++
|
|
|
|
removeFirst, removeSecond := nextLevels(nums, i)
|
2024-12-02 22:28:14 +00:00
|
|
|
return withComparator(removeFirst, comparator, count) ||
|
|
|
|
withComparator(removeSecond, comparator, 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:]...)
|
|
|
|
}
|