Inputs:
n(int) andnvalues (float) that represent exchange rates (different between them) with a random value between4and5.Output: compute the maximum number of values that can be used (in the same order) to represent an ascending then descending curve?
e.x. The eight values
4.5 4.6 4.3 4.0 4.8 4.4 4.7 4.1
should output
5 (4.5 4.6 4.8 4.4 4.1)
My approach
- If I try successive
ifs, I get a random array that respects the curve condition, but not the longest. - I have not tried backtracking because I am not that familiar with it, but something tells me I have to compute all the solutions with it then pick the longest.
- And lastly: brute force, but since it is an assignment for algorithm design; I may as well not hand it in. :)
Is there a simpler/more efficient/faster method?
Here's my try based on Daniel Lemire's algorithm. It seems it doesn't take into account the positions 0, i and n. I'm sure the ifs are the problem, how can I fix them?
for(int i = 0; i<n-1; i++){
int countp=0; // count ascending
int countn=0; // count descending
for(int j=0;j<=i;j++){
if(currency[j]<currency[j+1]){
countp++;
System.out.print(j+" ");
}
}
System.out.print("|| ");
for(int j=i;j<n-1;j++){
if(currency[j]>currency[j+1]){
countn++;
System.out.print(j+" ");
}
}
System.out.println();
if(countn+countp>maxcount) maxcount=countn+countp;
}