邻居之间的差之和为偶数的最长子序列的长度
在代码挑战中我被问了一个问题。问题要求我返回最长子序列的长度,其中已排序(非递减)子序列中邻居之间的差之和为偶数。对于每个子序列,程序应该找到子序列的排序(非递减)版本,然后对邻居之间的差异求和并检查它是否为偶数。 例如,对于给定的数组: [7,1,3,4,6,2,4]->子序列应该是整个数组 [7,1,3,4,6,2,4] ->因为排序时差异是偶数 [1,2,3,4,4,6,7] -> 1+1+1+0+2+1 = 6(偶数),因此,程序应返回 7。如果给定数组为 [7,3,4,6,2,4],则程序应返回 5。
我觉得我应该用动态编程来解决这个问题,但是,我对如何处理排序约束感到困惑。解决这个问题最有效的方法是什么?
In Code challange I have been asked a question. Questions asks me to return the length of the longest subsequence where sum of the difference between neighbours in the sorted(non-decreasing) subsequence is even. For every subsequence program should find the sorted(non-decreasing) version of the subsequence then should sum the difference between neighbours and check if it is even or not.
For example, for the given array:
[7,1,3,4,6,2,4] -> the subsequence should be whole array [7,1,3,4,6,2,4] -> because when it is sorted difference is even [1,2,3,4,4,6,7] -> 1+1+1+0+2+1 = 6(even), therefore, program should return 7. If the given array would be [7,3,4,6,2,4] then the program should return 5.
I feel like I should approach the question with dynamic programing, however, I am confused about how to handle the sorted constraint. What would be the most efficient approach to this question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
观察相邻元素之间的差异之和等于最小和最大元素之间的差异。例如:
所以问题就简化为:返回最大元素与最小元素之差为偶数的子序列的最大长度。
简单的方法
首先对数组进行排序。使用内置排序函数,排序操作的时间复杂度应为
O(n log(n))
。然后查找以下索引:应为(索引 4 - 索引 2 + 1,索引 3 - 索引 1 + 1)的最大值。您可以通过对数组进行两次独立迭代来在
O(n)
中找到索引。您可以在下面找到实现。[7,1,3,4,6,2,4]
的输出:[7,3,4,6,2,4]
的输出:总体时间复杂度 =
O(n log(n))
更好的方法
请注意,我们不需要对向量进行排序。因为我们要做的是找到最小偶数和最大偶数之间的元素数量,或者最小奇数和最大奇数之间的元素数量。
我们可以在
O(n)
内解决问题,而无需对数组进行排序。实现如下:总体时间复杂度:
O(n)
。Observe that sum of the differences between adjacent elements is equal to the difference between minimum and maximum element. For example:
So the question reduces to: Return the maximum length of the subsequence in which the difference between maximum and minimum element is even.
Naive Approach
First sort the array. Time complexity of sorting operation should be
O(n log(n))
with a built-in sort function. Then find indices of:Answer should be maximum of (index of 4 - index of 2 + 1, index of 3 - index of 1 + 1). You can find the indices in
O(n)
by two independent iteration over the array. You can find the implementation below.Output for
[7,1,3,4,6,2,4]
:Output for
[7,3,4,6,2,4]
:Overall time complexity =
O(n log(n))
Better Approach
Notice that we do not need to sort the vector. Because what we have to do is to find number of elements between minimum even and maximum even, OR number of elements between minimum odd and maximum odd.
We can solve the problem in
O(n)
without sorting the array. Implementation can be found below:Overall time complexity:
O(n)
.