Created
June 27, 2019 15:05
-
-
Save deepaksinghvi/8c0d68833db1fc79f79981d7e455f6ca to your computer and use it in GitHub Desktop.
Max Look
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Deepak Singhvi | |
*/ | |
public class MaxLoot { | |
public static int maxLoot(int input[], int inputSize) | |
{ | |
if (inputSize == 0) | |
return 0; | |
if (inputSize == 1) | |
return input[0]; | |
if (inputSize == 2) | |
return Math.max(input[0], input[1]); | |
// maxLoot[i] is the temporary store value at any point of time of calculation. | |
// maxLoot[n-1] i.e. the last element would have maximum loot value. | |
int[] maxLoot = new int[inputSize]; | |
maxLoot[0] = input[0]; | |
maxLoot[1] = Math.max(input[0], input[1]); | |
for (int i = 2; i<inputSize; i++) | |
maxLoot[i] = Math.max(input[i]+maxLoot[i-2], maxLoot[i-1]); | |
return maxLoot[inputSize-1]; // maximum loot value. | |
} | |
public static void main (String[] args) { | |
int input[] = {5, 5, 10, 100, 10, 5}; | |
System.out.println("Maximum loot value : " + maxLoot(input, input.length)); | |
input = new int[]{1,2,3}; | |
System.out.println("Maximum loot value : " + maxLoot(input, input.length)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment