-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3269-ConstructingTwoIncreasingArrays.go
More file actions
104 lines (89 loc) · 3.35 KB
/
3269-ConstructingTwoIncreasingArrays.go
File metadata and controls
104 lines (89 loc) · 3.35 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
// 3269. Constructing Two Increasing Arrays
// Given 2 integer arrays nums1 and nums2 consisting only of 0 and 1,
// your task is to calculate the minimum possible largest number in arrays nums1 and nums2, after doing the following.
// Replace every 0 with an even positive integer and every 1 with an odd positive integer.
// After replacement, both arrays should be increasing and each integer should be used at most once.
// Return the minimum possible largest number after applying the changes.
// Example 1:
// Input: nums1 = [], nums2 = [1,0,1,1]
// Output: 5
// Explanation:
// After replacing, nums1 = [], and nums2 = [1, 2, 3, 5].
// Example 2:
// Input: nums1 = [0,1,0,1], nums2 = [1,0,0,1]
// Output: 9
// Explanation:
// One way to replace, having 9 as the largest element is nums1 = [2, 3, 8, 9], and nums2 = [1, 4, 6, 7].
// Example 3:
// Input: nums1 = [0,1,0,0,1], nums2 = [0,0,0,1]
// Output: 13
// Explanation:
// One way to replace, having 13 as the largest element is nums1 = [2, 3, 4, 6, 7], and nums2 = [8, 10, 12, 13].
// Constraints:
// 0 <= nums1.length <= 1000
// 1 <= nums2.length <= 1000
// nums1 and nums2 consist only of 0 and 1.
import "fmt"
func minLargest(nums1 []int, nums2 []int) int {
nextNumber := func(value, odd int) int {
value++
if value % 2 != odd {
value++
}
return value
}
n := len(nums1)
dp, dp2 := make([]int, n + 1), make([]int, n + 1)
for i := 0; i < len(nums1); i++ {
dp[i + 1] = nextNumber(dp[i], nums1[i])
}
for i := 0; i < len(nums2); i++ {
dp2[0] = nextNumber(dp[0], nums2[i])
for j := 0; j < n; j++ {
dp2[j + 1] = min(nextNumber(dp2[j], nums1[j]), nextNumber(dp[j + 1], nums2[i]))
}
for j := 0; j < len(dp); j++ {
dp[j] = dp2[j]
}
}
return dp[n]
}
//
// object Solution {
// def minLargest(nums1: Array[Int], nums2: Array[Int]): Int = {
// def nextNumber(value: Int, odd: Int): Int = {
// var newValue = value + 1
// if ((newValue % 2) != odd) newValue += 1
// newValue
// }
// val dp = Array.fill(nums1.length + 1)(0)
// val dp2 = Array.fill(nums1.length + 1)(0)
// nums1.zipWithIndex.foreach { case (v, i) => dp(i + 1) = nextNumber(dp(i), v) }
// nums2.zipWithIndex.foreach { case (v2, _) => dp2(0) = nextNumber(dp.head, v2)
// nums1.zipWithIndex.foreach { case (v, i) => dp2(i + 1) = nextNumber(dp2(i), v).min(nextNumber(dp(i + 1), v2)) }
// dp.indices.foreach(i => dp(i) = dp2(i))
// }
// dp.last
// }
// }
func main() {
// Example 1:
// Input: nums1 = [], nums2 = [1,0,1,1]
// Output: 5
// Explanation:
// After replacing, nums1 = [], and nums2 = [1, 2, 3, 5].
fmt.Println(minLargest([]int{}, []int{1,0,1,1})) // 5
// Example 2:
// Input: nums1 = [0,1,0,1], nums2 = [1,0,0,1]
// Output: 9
// Explanation:
// One way to replace, having 9 as the largest element is nums1 = [2, 3, 8, 9], and nums2 = [1, 4, 6, 7].
fmt.Println(minLargest([]int{0,1,0,1}, []int{1,0,0,1})) // 9
// Example 3:
// Input: nums1 = [0,1,0,0,1], nums2 = [0,0,0,1]
// Output: 13
// Explanation:
// One way to replace, having 13 as the largest element is nums1 = [2, 3, 4, 6, 7], and nums2 = [8, 10, 12, 13].
fmt.Println(minLargest([]int{0,1,0,0,1}, []int{0,0,0,1})) // 13
}