Problem Statement

    

A sequence of numbers is called a zig-zag sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a zig-zag sequence.

For example, 1,7,4,9,2,5 is a zig-zag sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, 1,4,7,2,5 and 1,7,4,5,5 are not zig-zag sequences, the first because its first two differences are positive and the second because its last difference is zero.

Given a sequence of integers, sequence, return the length of the longest subsequence of sequence that is a zig-zag sequence. A subsequence is obtained by deleting some number of elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.

Definition

    
Class: ZigZag
Method: longestZigZag
Parameters: vector <int>
Returns: int
Method signature: int longestZigZag(vector <int> sequence)
(be sure your method is public)
    
 

Constraints

- sequence contains between 1 and 50 elements, inclusive.
- Each element of sequence is between 1 and 1000, inclusive.

题目中ZigZag序列即“小-大-小-大-小-大-小”,使用两个数组pos[i],neg[i]分别表示当前数字sequence[i]以‘大’结尾,以‘小’结尾。

要求轮流变大变小的最长序列,即最长递增子序列的变种。

例如序列: 1 17 5 10 13 15 10 5 16 8 

i        = 0  1   2  3   4   5   6   7  8   9
sequence = 1 17 5 10 13 15 10 5 16 8
pos[i] = 1 2 2 4 4 4 4 2 6 6
neg[i] = 1 1 3 3 2 2 5 5 3 7

首先,令pos[i]=neg[i]=1

递推公式

  sequence[i]>sequence[j], pos[i]=max(neg[j]+1,pos[i])-----------------------------①

  sequence[i]<sequence[j] , neg[i]=max(pos[j]+1,neg[i])----------------------------②

假设现在i=1 , j=0:

  由① 即pos[1]=max(neg[0]+1,pos[1])→pos[1]=2

现在i=2,j=0,1

  i=2,j=0:

    sequence[2]>sequence[0], 由① 即pos[2]=max(neg[0]+1,pos[2])→pos[2]=2

  i=2,j=1:

    sequence[2]<sequence[1], 由② 即neg[2]=max(pos[1]+1,neg[2])→neg[2]=3

现在i=3,j依次等于0,1,2

i=3,j=0:

  sequence[3]>sequence[0],由① 即pos[3]=max(neg[0]+1,pos[3])→pos[3]=2

i=3,j=1:

  sequence[3]<sequence[1], 由② 即neg[3]=max(pos[1]+1,neg[3])→neg[3]=3

  i=3,j=2:

    sequence[3]>sequence[2],由① 即pos[3]=max(neg[2]+1,pos[3])→pos[3]=4

以此类推...可以得出pos neg数组对应的各个数字相应的最长zigzag序列长度,取最大值就是要求的。

代码如下:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <list>
#include <cctype>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REP(i,j,k) for(int i = j ; i < k ; ++i)
#define MAXV (1000)
#define INF (0x6FFFFFFF)
using namespace std;
class ZigZag
{
public:
int longestZigZag(vector <int> sequence)
{
int pos[];
int neg[];
int ans=;
for(int i=; i<sequence.size(); i++)
{
pos[i]=;
neg[i]=;
for(int j=; j<i; j++)
{
if(sequence[i]>sequence[j])
pos[i]=max(neg[j]+,pos[i]);
else if(sequence[i]<sequence[j])
neg[i]=max(pos[j]+,neg[i]);
}
ans=max(ans,pos[i]);
ans=max(ans,neg[i]);
}
return ans;
}
};
int main()
{
int _x[] = { , , , , , , , , , , , , , , , , , , };
vector<int> x(_x , _x + sizeof(_x)/sizeof(_x[]));
ZigZag z;
printf("%d\n",z.longestZigZag(x));
return ;
}

动态规划初级练习(一):ZigZag的更多相关文章

  1. DP练习(初级):ZigZag

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493 类似于求最长子串的方法.dp[0 ...

  2. 动态规划初级 入门理解 C#代码

      using System; using System.Collections.Generic; using System.Linq; using System.Text; using Micros ...

  3. 动态规划初级练习(二):BadNeighbors

    Problem Statement      The old song declares "Go ahead and hate your neighbor", and the re ...

  4. LeetCode初级算法(动态规划+设计问题篇)

    目录 爬楼梯 买卖股票的最佳时机 最大子序和 打家劫舍 动态规划小结 Shuffle an Array 最小栈 爬楼梯 第一想法自然是递归,而且爬楼梯很明显是一个斐波拉切数列,所以就有了以下代码: c ...

  5. LeetCode探索初级算法 - 动态规划

    LeetCode探索初级算法 - 动态规划 今天在LeetCode上做了几个简单的动态规划的题目,也算是对动态规划有个基本的了解了.现在对动态规划这个算法做一个简单的总结. 什么是动态规划 动态规划英 ...

  6. LeetCode初级算法--动态规划01:爬楼梯

    LeetCode初级算法--动态规划01:爬楼梯 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...

  7. 算法练习LeetCode初级算法之动态规划

    爬楼梯:斐波那契数列 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 非递归解法 class S ...

  8. LeetCode初级算法的Python实现--动态规划

    动态规划的本质是递归:所以做题之前一定要会递归:递归式就是状态转移方程:这里将会介绍使用动态规划做题的思维方式. 统一的做题步骤: 1.用递归的方式写出代码:(此方法写的代码在leetcode中一定会 ...

  9. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

随机推荐

  1. Qt深入:不能不知道的Type、Attribute和Flags

    Qter高手与新手的区别在于:知道还是不知道 Qt不是开发语言,所以无所谓谁厉害.但使用他的Qter却有着差异,也许是面向对象语言本身的.或者实际经验上的.而对于Qt本身来说,高手与新手最显著的差异在 ...

  2. RabbitMQ Management HTTP API--官方文档

    Introduction Apart from this help page, all URIs will serve only resources of type application/json, ...

  3. linux 下各errno的意义

    strerror(errno):获取errno对应的错误 #include <string.h> /* for strerror */ #include <errno.h> # ...

  4. GridView禁止上下滚动的方法

    通常情况下,我们使用GridView来完成类似表格的布局,这种布局,我们只需要设置列数,会自动根据适配器的数据进行适配,非常灵活. GridView其实就是一个容器.允许向其内部添加控件,通常情况下, ...

  5. angular细节整理

    记录angularjs中比较容易忽视的问题 1.关于动态生成ui-sref的问题 ui-route中ui-sref中的路径无法动态生成的,如果要实现动态生成ui-sref路径,可以使用$state.g ...

  6. cmd连接mysql连接:mysql-h主机地址-u用户名-p用户密码(注:u与root可以不用加)

    MySQL导入导出命令1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc >wc ...

  7. 使用WMI控制Windows进程 和服务

    1.使用WMI控制Windows进程 本文主要介绍两种WMI的进行操作:检查进程是否存在.创建新进行 代码如下: using System; using System.Collections.Gene ...

  8. Android系统下的动态Dex加载与app速度优化

    1 问题 在Android系统中,一个App的所有代码都在一个Dex文件里面.Dex是一个类似Jar的存储了多有Java编译字节码的归档文件.因为Android系统使用Dalvik虚拟机,所以需要把 ...

  9. 你好,C++(8)如何表达那些始终保持不变的数据量?3.2.2 常量

    3.2.2  常量 与变量可以用在程序中表达那些可能会发生变化的数据量相对应地,在C++中,我们用常量来表达那些始终保持不变的数据量.简单来讲,就是程序中直接使用的数值.字符.字符串以及const关键 ...

  10. 外边距叠加collapsing-margin

    原载:Smallni | http://www.smallni.com/collapsing-margin/ 恩,margin叠加一直是个问题,也是我们一直会遇到的问题,很久以前就想把这个知识点整理下 ...