-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2771-LongestNon-DecreasingSubarrayFromTwoArrays.go
More file actions
103 lines (93 loc) · 4.42 KB
/
2771-LongestNon-DecreasingSubarrayFromTwoArrays.go
File metadata and controls
103 lines (93 loc) · 4.42 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
package main
// 2771. Longest Non-decreasing Subarray From Two Arrays
// You are given two 0-indexed integer arrays nums1 and nums2 of length n.
// Let's define another 0-indexed integer array, nums3, of length n.
// For each index i in the range [0, n - 1], you can assign either nums1[i] or nums2[i] to nums3[i].
// Your task is to maximize the length of the longest non-decreasing subarray in nums3 by choosing its values optimally.
// Return an integer representing the length of the longest non-decreasing subarray in nums3.
// Note: A subarray is a contiguous non-empty sequence of elements within an array.
// Example 1:
// Input: nums1 = [2,3,1], nums2 = [1,2,1]
// Output: 2
// Explanation: One way to construct nums3 is:
// nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1].
// The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2.
// We can show that 2 is the maximum achievable length.
// Example 2:
// Input: nums1 = [1,3,2,1], nums2 = [2,2,3,4]
// Output: 4
// Explanation: One way to construct nums3 is:
// nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4].
// The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length.
// Example 3:
// Input: nums1 = [1,1], nums2 = [2,2]
// Output: 2
// Explanation: One way to construct nums3 is:
// nums3 = [nums1[0], nums1[1]] => [1,1].
// The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length.
// Constraints:
// 1 <= nums1.length == nums2.length == n <= 10^5
// 1 <= nums1[i], nums2[i] <= 10^9
import "fmt"
func maxNonDecreasingLength(nums1 []int, nums2 []int) int {
dp1, dp2 := make([]int, len(nums1)), make([]int, len(nums1))
dp1[0], dp2[0] = 1, 1
max := func (values ...int) int {
mx := -1 << 32 - 1
for _, v := range values {
if v > mx { mx = v; }
}
return mx
}
for i := 1; i < len(nums1); i++ {
dp1[i], dp2[i] = 1, 1
if nums1[i] >= nums1[i-1] { dp1[i] = max(dp1[i-1]+1, dp1[i]); }
if nums1[i] >= nums2[i-1] { dp1[i] = max(dp2[i-1]+1, dp1[i]); }
if nums2[i] >= nums1[i-1] { dp2[i] = max(dp1[i-1]+1, dp2[i]); }
if nums2[i] >= nums2[i-1] { dp2[i] = max(dp2[i-1]+1, dp2[i]); }
}
return max(max(dp1...), max(dp2...))
}
func maxNonDecreasingLength1(nums1 []int, nums2 []int) int {
res, n := 1, len(nums1)
dp := make([][2]int, n) // dp[idx:] && 以 nums1[i] 或者 nums2[i]开头,对应的最长子数组长度是多长
dp[n - 1] = [2]int{ 1, 1 }
max := func (x, y int) int { if x > y { return x; }; return y; }
for i := n - 2; i >= 0; i-- {
ca, cb := 1, 1
if nums1[i+1] >= nums1[i] { ca = max(ca, 1 + dp[i+1][0]); }
if nums2[i+1] >= nums1[i] { ca = max(ca, 1 + dp[i+1][1]); }
if nums1[i+1] >= nums2[i] { cb = max(cb, 1 + dp[i+1][0]); }
if nums2[i+1] >= nums2[i] { cb = max(cb, 1 + dp[i+1][1]); }
dp[i] = [2]int{ca, cb}
res = max(res, max(ca, cb))
}
return res
}
func main() {
// Example 1:
// Input: nums1 = [2,3,1], nums2 = [1,2,1]
// Output: 2
// Explanation: One way to construct nums3 is:
// nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1].
// The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2.
// We can show that 2 is the maximum achievable length.
fmt.Println(maxNonDecreasingLength([]int{2,3,1},[]int{1,2,1})) // 2
// Example 2:
// Input: nums1 = [1,3,2,1], nums2 = [2,2,3,4]
// Output: 4
// Explanation: One way to construct nums3 is:
// nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4].
// The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length.
fmt.Println(maxNonDecreasingLength([]int{1,3,2,1},[]int{2,2,3,4})) // 4
// Example 3:
// Input: nums1 = [1,1], nums2 = [2,2]
// Output: 2
// Explanation: One way to construct nums3 is:
// nums3 = [nums1[0], nums1[1]] => [1,1].
// The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length.
fmt.Println(maxNonDecreasingLength([]int{1,1},[]int{2,2})) // 2
fmt.Println(maxNonDecreasingLength1([]int{2,3,1},[]int{1,2,1})) // 2
fmt.Println(maxNonDecreasingLength1([]int{1,3,2,1},[]int{2,2,3,4})) // 4
fmt.Println(maxNonDecreasingLength1([]int{1,1},[]int{2,2})) // 2
}