-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3201-FindTheMaximumLengthOfValidSubsequenceI.go
More file actions
119 lines (104 loc) · 3.36 KB
/
3201-FindTheMaximumLengthOfValidSubsequenceI.go
File metadata and controls
119 lines (104 loc) · 3.36 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
// 3201. Find the Maximum Length of Valid Subsequence I
// You are given an integer array nums.
// A subsequence sub of nums with length x is called valid if it satisfies:
// (sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2.
// Return the length of the longest valid subsequence of nums.
// A subsequence is an array that can be derived from another array by deleting some
// or no elements without changing the order of the remaining elements.
// Example 1:
// Input: nums = [1,2,3,4]
// Output: 4
// Explanation:
// The longest valid subsequence is [1, 2, 3, 4].
// Example 2:
// Input: nums = [1,2,1,1,2,1,2]
// Output: 6
// Explanation:
// The longest valid subsequence is [1, 2, 1, 2, 1, 2].
// Example 3:
// Input: nums = [1,3]
// Output: 2
// Explanation:
// The longest valid subsequence is [1, 3].
// Constraints:
// 2 <= nums.length <= 2 * 10^5
// 1 <= nums[i] <= 10^7
import "fmt"
func maximumLength(nums []int) int {
flag := nums[0] % 2
res, even, odd := 0, 0, 0 // alternating count, even, odd counts
for _, v := range nums {
if v % 2 == 0 {
even++
} else {
odd++
}
if v % 2 == flag {
flag ^= 1 // toggle flag value
res++
}
}
max := func (x, y int) int { if x > y { return x; }; return y; }
return max(res,max(even, odd))
}
func maximumLength1(nums []int) int {
mp := make([]int, 2)
mp[nums[0]%2]++
res, flag := 1, nums[0] % 2
for i := 1; i < len(nums); i++ {
if nums[i] % 2 != flag {
res++
flag = nums[i] % 2
}
mp[nums[i] % 2]++
}
max := func (x, y int) int { if x > y { return x; }; return y; }
return max(res, max(mp[0], mp[1]))
}
func maximumLength2(nums []int) int {
a, b, c, d := 0, 0, 0, 0
for _, x := range nums {
if x & 1 == 0 {
b++
d = c + 1
} else {
a++
c = d + 1
}
}
max := func (x, y int) int { if x > y { return x; }; return y; }
return max(max(a, b), max(c, d))
}
func main() {
// Example 1:
// Input: nums = [1,2,3,4]
// Output: 4
// Explanation:
// The longest valid subsequence is [1, 2, 3, 4].
fmt.Println(maximumLength([]int{1,2,3,4})) // 4
// Example 2:
// Input: nums = [1,2,1,1,2,1,2]
// Output: 6
// Explanation:
// The longest valid subsequence is [1, 2, 1, 2, 1, 2].
fmt.Println(maximumLength([]int{1,2,1,1,2,1,2})) // 6
// Example 3:
// Input: nums = [1,3]
// Output: 2
// Explanation:
// The longest valid subsequence is [1, 3].
fmt.Println(maximumLength([]int{1,3})) // 2
fmt.Println(maximumLength([]int{1,2,3,4,5,6,7,8,9})) // 9
fmt.Println(maximumLength([]int{9,8,7,6,5,4,3,2,1})) // 9
fmt.Println(maximumLength1([]int{1,2,3,4})) // 4
fmt.Println(maximumLength1([]int{1,2,1,1,2,1,2})) // 6
fmt.Println(maximumLength1([]int{1,3})) // 2
fmt.Println(maximumLength1([]int{1,2,3,4,5,6,7,8,9})) // 9
fmt.Println(maximumLength1([]int{9,8,7,6,5,4,3,2,1})) // 9
fmt.Println(maximumLength2([]int{1,2,3,4})) // 4
fmt.Println(maximumLength2([]int{1,2,1,1,2,1,2})) // 6
fmt.Println(maximumLength2([]int{1,3})) // 2
fmt.Println(maximumLength2([]int{1,2,3,4,5,6,7,8,9})) // 9
fmt.Println(maximumLength2([]int{9,8,7,6,5,4,3,2,1})) // 9
}