Click here to Skip to main content
15,922,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to write a code that will find the longest decreasing subsequence in input list of integers. For example. if the list is: 8 4 2 3 2, then the subsequence is 8 4 2.

With help of internet, I've got something like this so far, but it's not working for this and give me wrong answear.

Java
int curr_len2 = 0, max_len2 = 1, start2 = 0;
		
		for (int i = 1; i <= list.size() - 1; i++){
			if (list.get(i) < listN.get(i-1)){
				curr_len2++;
				if (curr_len2 > max_len2){
					max_len2 = curr_len2;
					start = i - max_len2 + 1;
				}
			} else {
				curr_len2 = 1;
			}
		}
		
		for (int i = 0; i < max_len2; i++)
			listDesc.add(start+i);
Posted
Comments
PIEBALDconsult 6-Nov-14 16:35pm    
I don't know Java, but isn't it object-oriented?
I wonder whether or not " i <= list.size() - 1 " is correct
Sergey Alexandrovich Kryukov 6-Nov-14 18:08pm    
No, you didn't define what you need to find. Why 8 4 2 3 or 8 4 2 3 2 is not a sub-sequence?
An example is not a definition.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900