c++ - 最长双调子序列
问题描述
我正在尝试解决 Longest Bi tonic 子序列问题,尽管我从各个站点获得了运行代码,但我无法弄清楚为什么我的解决方案不起作用。我正在尝试返回最大长度子序列。我创建了一个名为数组的类型,它存储每个元素的值,如果它包含在升序子序列或降序子序列中。
#include <bits/stdc++.h>
#define li long int
int longestBitonicSubarray(int *input, int n) {
li* output = new li[n];
li* type = new li[n];
output[0] = 1;
type[0] = 0;
/*
type when 1 means order is ascending
type when 2 means order is descending
type when 0 not decided
*/
for(li i=1;i<n;i++) {
output[i] = 1;
type[i] = 0;
for(li j=i-1;j>=0;j--) {
if(type[j] == 0 || type[j] == 1) {
if(input[j] < input[i]) {
li pos_ans = output[j] + 1;
if(output[i] < pos_ans) {
output[i] = pos_ans;
type[i] = 1;
}
} else if(input[j] > input[i]) {
li pos_ans = output[j] + 1;
if(output[i] < pos_ans) {
output[i] = pos_ans;
type[i] = 2;
}
}
} else {
if(input[j] > input[i]) {
li pos_ans = output[j] + 1;
if(output[i] < pos_ans) {
output[i] = pos_ans;
type[i] = 2;
}
}
}
}
}
li ans = *max_element(output,output+n);
delete [] output;
delete [] type;
return ans;
}
解决方案
推荐阅读
- python - 为什么 python 对这些比较感到困惑?
- javascript - 相同的代码是从网页复制的,但它不起作用?
- javascript - some() 如何用于检查 JS 中的重复项?
- amazon-web-services - CloudFormation 是否可以跨嵌套堆栈共享条件定义?
- sql-server - 递归查询计算文件夹大小
- c++ - 模板函子与类模板参数推导
- javascript - 将过滤器应用于 React 数组并显示过滤后的结果
- flutter - 在颤振应用程序中如何将数据从标题放到appBar
- c# - 如何实时收听和使用来自 Azure IoT 中心的消息
- assembly - 内存堆栈:来自 esp 的 sub 20