[topcoder]ZigZag
http://community.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493
动态规划题。如果不用DP,暴力的应当在2^n*n的复杂度吧。动态规划现在我的思维还老停留在一维而且没有第二层循环。但其实如果一开始的复杂度很高,稍微有几个循环,指数级别,一点问题没有。
题目描述:给出n个数:A[1], A[2], ... , A[n],求最长的子序列的长度,子序列中相邻数之间的差值是正负交替出现的。
f1[i]表示 最后一个数是A[i]并且A[i]和前一个数的差值为正的最长子序列的长度。
f2[i]表示最后一个数是A[i]并且A[i]和前一个数的差值为负的最长子序列的长度。
对于f1[i], 和它有联系的子问题是f2[j] (0<= j < i).f1[i] = max(f1[i], f2[j]+1) (A[i] > A[j])
对于f2[i], 和它有联系的子问题是 f1j] (0 <= j < I),f2[i] = max(f2[i], f1[j]+10 (A[i] < A[j])
计算子问题的顺序:A数组下标从小到大,顺推。
public class ZigZag {
public int longestZigZag(int[] sequence) {
if (sequence.length == 0) return 0;
int[] up = new int[sequence.length];
int[] down = new int[sequence.length];
up[0] = 1;
down[0] = 1;
int ret = 0;
for (int i = 1 ; i < sequence.length; i++) {
int max_up = 0;
int max_down = 0;
for (int j = 0; j < i; j++) {
if (sequence[j] > sequence[i]) {
if (down[j] > max_down) max_down = down[j];
}
else if (sequence[j] < sequence[i]) {
if (up[j] > max_up) max_up = up[j];
}
}
up[i] = max_down + 1;
down[i] = max_up + 1;
if (up[i] > ret) ret = up[i];
if (down[i] > ret) ret = down[i];
}
return ret;
}
}
[topcoder]ZigZag的更多相关文章
- [Topcoder]ZigZag(dp)
题目链接:https://community.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493 题意:给一串数字,求出最长的波 ...
- DP练习(初级):ZigZag
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493 类似于求最长子串的方法.dp[0 ...
- [LeetCode] Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- 整数压缩编码 ZigZag
在分析Avro源码时,发现Avro为了对int.long类型数据压缩,采用Protocol Buffers的ZigZag编码(Thrift也采用了ZigZag来压缩整数). 1. 补码编码 为了便于后 ...
- No.006:ZigZag Conversion
问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
随机推荐
- 项目中的那些事---JavaScript
一.String.charAt(index) 作用:获取字符串指定索引位置的字符 注意:index的值是0~(字符串长度-1)之间的值 <script type="text/javas ...
- Tomcat遇到的问题The Tomcat server configuration at ServersTomcat v5.5 Server at localhost-config is missing. Check..
一.解决方法 删除Servers视图,重新创建一个即可.
- 抓取dump
1,在程序奔溃前部署.adplus.exe -crash -pn explorer.exe -o d: -crash:当进程挂掉的时候抓取dump,只能抓取到进程报错时的信息,如果进程不报错,就无法抓 ...
- Ueditor中增加迅雷下载支持
在项目中有遇到需要在Ueditor中加一个链接,迅雷的开头是thunder 会被默认加上http:// 最后的 结果就变成了http://thunder://xxxxx 导致用户点击失败: 其实在 ...
- QMessageBox 弹出框上的按钮设置为中文
Qt 默认的弹出框上的按钮式英文,虽然也知道是什么意思,但终究不如中文看着顺眼. QMessageBox box(QMessageBox::Warning,"标题","弹 ...
- SharedPreference 的存取
1.通过名称来获取指定的SharedPreferences,下面这句代码表示获取名字问hello的SharedPreferences,数据保存在 data/data/package名/shared_p ...
- 如何利用VS2010安装和部署应用程序
转自:http://jingyan.baidu.com/article/4b52d70255d7f0fc5d774b4d.html 1.假设你当前的winform已经okay了 2.解决方案中新建&q ...
- 关于maven参数过滤
一.maven通过设置过滤器,可以使maven在编译打包时实现参数过滤的功能(详细配置说明略) <filters> <filter>../antx.properties< ...
- MacOS10.9获取Android源码不完全笔记(2014)
第一步:安装Macports 这个我就不叙述了,网上有无数教程 第二步:创建一个磁盘镜像 1.打开磁盘工具,然后: 第三步:使用Macport安装编译环境 1.打开终端输入以下内容 sudo port ...
- matlab之点运算基本思想及几何平移变换
1.对数变换可以增强图像中较暗部分的细节,因为对数可以将较小的值放大,而较大的值缩小 2.伽马变换:y = (x + esp) ^ γ,x,y的取值范围是0到1,esp是补偿系数,γ为伽马系数.γ的不 ...