-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path16_Find_the_Maximum_Length_of_Valid_Subsequence_I.cpp
More file actions
95 lines (64 loc) · 2.22 KB
/
Copy path16_Find_the_Maximum_Length_of_Valid_Subsequence_I.cpp
File metadata and controls
95 lines (64 loc) · 2.22 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
// 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 * 105
// 1 <= nums[i] <= 107
class Solution
{
public:
int maximumLength(vector<int> &nums)
{
int count_even = 0, count_odd = 0;
for (int num : nums)
{
if (num % 2 == 0)
count_even++;
else
count_odd++;
}
int even_dp = 0, odd_dp = 0;
for (int num : nums)
{
if (num % 2 == 0)
even_dp = max(even_dp, odd_dp + 1);
else
odd_dp = max(odd_dp, even_dp + 1);
}
return max({count_even, count_odd, even_dp, odd_dp});
}
};
/*
Code Explanation:
This solution finds the maximum length of a valid subsequence where adjacent elements sum to either all even or all odd numbers.
The algorithm works in two parts:
1. First loop counts total even and odd numbers in the array
2. Second loop uses dynamic programming to find longest alternating even-odd subsequence:
- even_dp tracks max subsequence ending with even number
- odd_dp tracks max subsequence ending with odd number
Finally returns maximum of:
- Total even numbers (count_even)
- Total odd numbers (count_odd)
- Longest even-ending subsequence (even_dp)
- Longest odd-ending subsequence (odd_dp)
Time Complexity: O(n) where n is length of input array
Space Complexity: O(1) as we only use constant extra space
*/