Jimmy
BAN USER
Comments (4)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
say arr = {1, 2, 2, 3, 4, 4, 4, 5}
we need to change it into
arr - {1, 2, 3, 4, 5}
int num = arr[0];
list.add(num);
int j = 0;
for( int i = 1; i < arr.length; i++ ) {
if( list.get(j) < arr[i] ) {
list.add(arr[i]);
j++;
}
else { // duplicate
continue;
}
}
This solves the problem with complexity O(n), but requires an extra list.
Comment hidden because of low score. Click to expand.
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
// Recursive approach. Not efficient
- Jimmy December 10, 2007public int fibonacci( int num ) {
return (num <= 1) ? num : fibonacci(num - 1) + fibonacci(num - 2);
}
// More efficient iterative approach
public int fibonacci( int num ) {
if( num == 0 ) return 0;
long[] fib = new long[num + 1];
fib[0] = 0;
fib[1] = 1;
for( int i = 2; i <= num; i++ ) {
fib[i] = fib[i-1] + fib[i-2];
}
return fib[num];
}