Michael Howard
BAN USERThis looks like a cool solution. I was trying to work something out using 3 regular expressions, but I think your method is the way to go.
However, it will fail for terms which are anagrams. As an example, if we search for "Dictionary", the word 'Indicators' will be counted as a word which needs only 1 letter changed to get an exact match.
How can we change your technique to account for this?
The idea is right, although the code will not run as written. I like the solution because it is so simple. Here are a few small corrections:
public static void main(String[] args)
{
// TODO code application logic here
int[] array = {-10, -3, -2, -9, -1, -8};
int startIndex = 0;
int startSum = 0;
int maxSum = maxSum(array, startIndex, startSum);
if(maxSum == 0)
{
maxSum = maxElementInArray(array);
}
System.out.println(maxSum);
}
public static int maxElementInArray(int[] A)
{
int max = Integer.MIN_VALUE;
for (int index = 0; index < A.length; index++)
{
if (A[index] > max)
{
max = A[index];
}
}
return max;
}
public static int max(int a, int b)
{
if (a > b)
{
return a;
}
else return b;
}
public static int maxSum(int[] A , int currIndex , int sum)
{
if(currIndex >= A.length)
{
return 0;
}
return sum+max(maxSum(A,currIndex+1,sum),A[currIndex]+maxSum(A,currIndex+2,sum));
}
I see now. In that case, the method which you and The Artist have posted is definitely the correct solution.
- Michael Howard November 12, 2012