TopCoder:ZigZag(动态规划--最长非降子序列)

本文介绍了如何利用动态规划解决TopCoder上的ZigZag序列问题。ZigZag序列定义为正负差异交替的数字序列,例如1,7,4,9,2,5。目标是找到给定整数序列的最长ZigZag子序列。解题思路类似于最长非降子序列,通过维护两个数组down[]和up[],分别记录当前数字后面比它小和大的序列长度,根据比较结果更新这两个数组,最终找到最长序列的长度。" 109236309,10068608,Python内置函数详解:69个函数全解析,"['Python', '编程语言', '函数库']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看动态规划的题 在topcoder里看的这道题就来练练手

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: int[]
Returns: int
Method signature: int longestZigZag(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.
 
Examples
0)
   
{ 1, 7, 4, 9, 2, 5 }
Returns: 6
The entire sequence is a zig-zag sequence.
1)
   
{ 1, 17, 5, 10, 13, 15, 10, 5, 16, 8 }
Returns: 7
There are several subsequences that achieve this length. One is 1,17,10,13,10,16,8.
2)
   
{ 44 }
Returns: 1
3)
   
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }
Returns: 2
4)
   
{ 70, 55, 13, 2, 99, 2, 80, 80, 80, 80, 100, 19, 7, 5, 5, 5, 1000, 32, 32 }
Returns: 8
5)
   
{ 374, 40, 854, 203, 203, 156, 362, 279, 812, 955, 
600, 947, 978, 46, 100, 953, 670, 862, 568, 188, 
67, 669, 810, 704, 52, 861, 49, 640, 370, 908, 
477, 245, 413, 109, 659, 401, 483, 308, 609, 120, 
249, 22, 176, 279, 23, 22, 617, 462, 459, 244 }

Returns: 36

题目大意是:一个数字序列如果严格遵守正负之间持续交替的差异称为Zig-Zag序列。第一个差异(如果存在)可以是正的或负的。
例如,1,7,4,9,2,5是Zig-Zag序列因为差异(6,-3,5,-7,3)交替的正面和负面的。相反,1,4,7,2,5和1,7,4,5,5不是Zig-Zag序列,
给定一个整数序列,返回序列的最长Zig-Zag序列。

思路:和求解最长非降子序列的思想一样,a[i]为当前数字,a[j]为前面的数字,down[]为该数下一个数比他小的序列的长度,如:1,17                                            17的down[1]=2,up[1]=1           up[]为该数下一个数比他大的序列的长度,如:2,1  1的up[1]=2,down[1]=1

         若a[j]<a[i]:则a[i]下一个数应该比他小,down[i]=up[j]+1;

         若a[j]>a[i]:则a[i]下一个数应该比他大,up[i]=down[j]+1;

         len表示当前序列长度最大的.

public class TopCoder__ZigZag {
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		String a=s.nextLine();//注意这里是读行 只能读第一行 最后那个试例 将它们弄到同一行
		String b[]=a.split(" ");//两个数中间用空格隔开,若想直接复制题里的 将这里的空格改成“,”即可 
		int arr[]=new int[b.length];
		for(int i=0;i<b.length;i++){
			arr[i]=Integer.parseInt(b[i]);
		}
		System.out.println(len(arr));
	}
	public static int len(int a[]){
		if(a.length==1)return 1;
		int up[]=new int[a.length];
		int down[]=new int[a.length];
		int len=1;
		up[0]=1;
		down[0]=1;
		for(int i=1;i<a.length;i++){
			for(int j=0;j<i;j++){
				if(a[i]<a[j]){
//					if(up[i]<down[j]+1)
						up[i]=down[j]+1;//其实这里不用判断 因为它和前面的是一个序列 所以长度+1肯定大
					down[i]=Math.max(down[i],down[j]);
				}
					
				else if(a[i]>a[j]){
//					if(down[i]<up[j]+1)
						down[i]=up[j]+1;
					up[i]=Math.max(up[i],up[j]);
				}
				else{
					down[i]=down[j];
					up[i]=up[j];
				}
			}
			len=Math.max(len, Math.max(down[i],up[i]));
		}
		return len;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值