Redian新闻
>
推荐几支Stem Cell stocks吧.
avatar
推荐几支Stem Cell stocks吧.# Stock
f*r
1
今天看了一道DP题目,自己写了一个函数,请大家看看是否有问题:
题目:
A sequence of numbers is called a zig-zag sequence if the differences betwee
n successive numbers strictly alternate between positive and negative. The f
irst difference (if one exists) may be either positive or negative. A sequen
ce 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 n
avatar
v*c
2
求祝福: 嫂子明天上海二签.
谢谢大家!
avatar
b*t
3
我印象中都不是很贵,属于多年被打压对象,也许傲八上台这些股票的funding都没有
问题了。
avatar
x*3
4
没看懂你的解法,
比如这段程序,
if(DiffArray[i]*DiffArray[i+1] < 0)
你只是判断相邻的三个元素是不是zig zag,
但是longest zz subsequence里的元素不一定要相邻
能说下你的subproblem space是什么吗
我的subproblem定义是
给定序列A[1..n]
LCCZ(i) = the length of longest ZZ subsequence ending at A[i]
原题的解LCCZ(A) = max(A[i]) 1<=i<=n
avatar
v*5
5
Good Luck!

【在 v******c 的大作中提到】
: 求祝福: 嫂子明天上海二签.
: 谢谢大家!

avatar
b*t
6
我印象中都不是很贵,属于多年被打压对象,也许傲八上台这些股票的funding都没有
问题了。
avatar
B*t
7
想复杂了,这个贪心就可以了, 设置个flag, s[i]>s[i-1], flag=true,
s[i]flag=true的时候,如果s[i]>=s[i-1],i++ 直到s[i]反之类似。

differences betwee
negative. The f
negative. A sequen
sequence.
differences (6,-3
1,4,7,2,5 and 1
first two differen
zero.

【在 f*******r 的大作中提到】
: 今天看了一道DP题目,自己写了一个函数,请大家看看是否有问题:
: 题目:
: A sequence of numbers is called a zig-zag sequence if the differences betwee
: n successive numbers strictly alternate between positive and negative. The f
: irst difference (if one exists) may be either positive or negative. A sequen
: ce 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 n

avatar
j*u
8
Good luck.
avatar
I*S
9
R
avatar
x*3
10
不是很理解,能run个例子吗

【在 B*****t 的大作中提到】
: 想复杂了,这个贪心就可以了, 设置个flag, s[i]>s[i-1], flag=true,
: s[i]: flag=true的时候,如果s[i]>=s[i-1],i++ 直到s[i]: 反之类似。
:
: differences betwee
: negative. The f
: negative. A sequen
: sequence.
: differences (6,-3

avatar
f*5
11
Good luck.
avatar
h*6
12
这是Topcoder上的题吧。
遍历一遍就可以了:
如果当前和前一个相等,则跳过
如果当前应该比前一个大实际也比前一个大,计数加1,标志翻转
如果当前应该比前一个小实际也比前一个小,计数加1,标志翻转
每次循环必须更新当前数和前一个数,仅计数加1的时候标志才翻转。
avatar
A*a
13
Bless
avatar
B*t
14
如 1 3 2 1 4
3>1 flag=true; res = 2;
2<3 flag=!flag; res++;
1<2 continue;
4>1 flag=!flag; res++;
每次flag翻转的时候res++;

【在 x******3 的大作中提到】
: 不是很理解,能run个例子吗
avatar
j*u
15
过了吗

【在 v******c 的大作中提到】
: 求祝福: 嫂子明天上海二签.
: 谢谢大家!

avatar
x*3
16
对于第一个数怎么处理
自动计入longest ZZ subsequence吗

【在 h**6 的大作中提到】
: 这是Topcoder上的题吧。
: 遍历一遍就可以了:
: 如果当前和前一个相等,则跳过
: 如果当前应该比前一个大实际也比前一个大,计数加1,标志翻转
: 如果当前应该比前一个小实际也比前一个小,计数加1,标志翻转
: 每次循环必须更新当前数和前一个数,仅计数加1的时候标志才翻转。

avatar
d*n
17
int longestZigZag(int sequence[], int num)
{
// arguments validation skipped
int s= 1;
while (s < num && sequence[s] == sequence[s-1])
s++;
if (s == num ) return 1;
int count = 2;
int increasing = (sequence[s]-sequence[s-1] > 0)?1:-1;
while (s < num)
{
if (increasing*(sequence[s]-sequence[s-1]) < 0)
{
++count;
increasing *= -1;
}
++s;
}
return count;
}

【在 x******3 的大作中提到】
: 对于第一个数怎么处理
: 自动计入longest ZZ subsequence吗

avatar
x*3
18
多谢BlueAnt和durbin, 终于是想明白了
avatar
f*r
19
谢谢指点,呵呵,大家说的对,
我想错了,总以为longest subsequence要是continuous的序列,
应该是这样简单比较就可以了,还不需要
额外分配空间,谢谢!

【在 B*****t 的大作中提到】
: 想复杂了,这个贪心就可以了, 设置个flag, s[i]>s[i-1], flag=true,
: s[i]: flag=true的时候,如果s[i]>=s[i-1],i++ 直到s[i]: 反之类似。
:
: differences betwee
: negative. The f
: negative. A sequen
: sequence.
: differences (6,-3

avatar
b*j
20
他的是对的,按照你的改了就错了,呵呵
avatar
K*g
21
这个不需要DP吧?
avatar
l*a
22
大家都想明白了?
这个结果我觉得不正确把。
你的算法是如果当前的状态跟上次的状态不一致,则++
如果一样,则不计数。
这样的话,请看 1,4,3,10,9,8,6 ,7,6,9
差值为 3,-1,7,-1,-1,-1,1,-1,3
当扫描到10/9的时候,与3/10不同还可以++,然后9/8,8/6都是递减,跳过
然后6/7是增加了,按照大家的算法,长度可以++了
可实际上这样的序列就是 1,4,3,10,9,(6)7
其实最后还是两次数值下降,这样的结果应该是不正确的吧
难道我什么地方理解得有问题?
欢迎指正

【在 f*******r 的大作中提到】
: 谢谢指点,呵呵,大家说的对,
: 我想错了,总以为longest subsequence要是continuous的序列,
: 应该是这样简单比较就可以了,还不需要
: 额外分配空间,谢谢!

avatar
y*i
23
补充下, 算法的复杂度为O(n)
avatar
d*k
24
这个算法貌似还有些问题,比如
输入:[3,3,3]
期盼输出:0
实际输出:1

【在 d****n 的大作中提到】
: int longestZigZag(int sequence[], int num)
: {
: // arguments validation skipped
: int s= 1;
: while (s < num && sequence[s] == sequence[s-1])
: s++;
: if (s == num ) return 1;
: int count = 2;
: int increasing = (sequence[s]-sequence[s-1] > 0)?1:-1;
: while (s < num)

avatar
d*k
25
我也试试,请指教,C语言风格:
int longestZigZagSequence(int[] s, int size){
if(NULL == s || size < 0){
fprintf(stderr, "Incorrect parameters in longestZigZagSequence(...)!
")
exit(1);
} else if(size < 2) {
return 0;
}
int i;
int flag = 0;
int zzCount = 0;
for(i=1; iif(s[i] == s[i-1]){
continue;
} else if(s[i] > s[i-1] && flag <= 0){
zzCount++;
flag = 1;
} else if(s[i] < s

【在 d****n 的大作中提到】
: int longestZigZag(int sequence[], int num)
: {
: // arguments validation skipped
: int s= 1;
: while (s < num && sequence[s] == sequence[s-1])
: s++;
: if (s == num ) return 1;
: int count = 2;
: int increasing = (sequence[s]-sequence[s-1] > 0)?1:-1;
: while (s < num)

avatar
l*a
26
please use some samples for testing

longestZigZagSequence(...)!

【在 d****k 的大作中提到】
: 我也试试,请指教,C语言风格:
: int longestZigZagSequence(int[] s, int size){
: if(NULL == s || size < 0){
: fprintf(stderr, "Incorrect parameters in longestZigZagSequence(...)!
: ")
: exit(1);
: } else if(size < 2) {
: return 0;
: }
: int i;

avatar
y*i
27
测试下
1 2 1 2 1000 999 1000 999 1000 999 1000

)!

【在 d****k 的大作中提到】
: 我也试试,请指教,C语言风格:
: int longestZigZagSequence(int[] s, int size){
: if(NULL == s || size < 0){
: fprintf(stderr, "Incorrect parameters in longestZigZagSequence(...)!
: ")
: exit(1);
: } else if(size < 2) {
: return 0;
: }
: int i;

avatar
d*k
28
删除索引为3的元素,剩下的是一个最长的序列

..

【在 y*****i 的大作中提到】
: 测试下
: 1 2 1 2 1000 999 1000 999 1000 999 1000
:
: )!

avatar
x*r
29
不是很懂你的问题,但我觉得他们的意思是
10->9 ++
9->8 keep
8->6 keep
6->7 ++(这一步没有错,因为你只要放弃之前的9,选这个6,就可以满足zigzag条件)
7->6 ++
6->9 ++
所以我看好像是没错呀

【在 l*****a 的大作中提到】
: 大家都想明白了?
: 这个结果我觉得不正确把。
: 你的算法是如果当前的状态跟上次的状态不一致,则++
: 如果一样,则不计数。
: 这样的话,请看 1,4,3,10,9,8,6 ,7,6,9
: 差值为 3,-1,7,-1,-1,-1,1,-1,3
: 当扫描到10/9的时候,与3/10不同还可以++,然后9/8,8/6都是递减,跳过
: 然后6/7是增加了,按照大家的算法,长度可以++了
: 可实际上这样的序列就是 1,4,3,10,9,(6)7
: 其实最后还是两次数值下降,这样的结果应该是不正确的吧

avatar
y*i
30
我理解错了,多谢指正!

【在 d****k 的大作中提到】
: 删除索引为3的元素,剩下的是一个最长的序列
:
: ..

avatar
K*g
31
请问你怎么知道放弃前面的9呢,如果按照前面的代码,是不会放弃9的,除非我们加判
断条件。

【在 x****r 的大作中提到】
: 不是很懂你的问题,但我觉得他们的意思是
: 10->9 ++
: 9->8 keep
: 8->6 keep
: 6->7 ++(这一步没有错,因为你只要放弃之前的9,选这个6,就可以满足zigzag条件)
: 7->6 ++
: 6->9 ++
: 所以我看好像是没错呀

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。