-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmake_array_strictly_increasing.go
71 lines (64 loc) · 1.26 KB
/
make_array_strictly_increasing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"math"
"sort"
)
func makeArrayIncreasing(arr1 []int, arr2 []int) int {
n := len(arr1)
sort.Ints(arr2)
uniqueArr2 := []int{}
for i := 0; i < len(arr2); i++ {
if i+1 < len(arr2) && arr2[i] == arr2[i+1] {
continue
}
uniqueArr2 = append(uniqueArr2, arr2[i])
}
arr2 = uniqueArr2
newArr1 := make([]int, n+2)
for i := 0; i < n; i++ {
newArr1[i+1] = arr1[i]
}
newArr1[n+1] = math.MaxInt32
newArr1[0] = math.MinInt32
arr1 = newArr1
dp := make([]int, n+2)
for i := 0; i < n+2; i++ {
dp[i] = math.MaxInt32
}
dp[0] = 0
for i := 1; i < n+2; i++ {
for j := 0; j < i; j++ {
if arr1[j] < arr1[i] && dp[j] != math.MaxInt32 {
change := check(arr1, arr2, j, i)
if change >= 0 {
dp[i] = min(dp[i], dp[j]+change)
}
}
}
}
if dp[n+1] == math.MaxInt32 {
return -1
}
return dp[n+1]
}
func check(arr1 []int, arr2 []int, start int, end int) int {
if start+1 == end {
return 0
}
minVal := arr1[start]
maxVal := arr1[end]
idx := sort.Search(len(arr2), func(i int) bool { return arr2[i] > minVal })
maxCount := end - start - 1
endIdx := idx + maxCount - 1
if endIdx < len(arr2) && arr2[endIdx] < maxVal {
return maxCount
} else {
return -1
}
}
func min(a, b int) int {
if a < b {
return a
}
return b
}