package org.wesome.dsalgo;
public class MaxPossibleLoot {
public static int findMaxPossibleLoot(int houseArr[]) {
int noOfHouse = houseArr.length;
if (noOfHouse == 0) {
System.out.println("house array is empty");
return 0;
}
if (noOfHouse == 1) {
System.out.println("only 1 house is available, hence its value is the maximum loot possible");
return houseArr[0];
}
if (noOfHouse == 2) {
System.out.println("only 2 house are available, hence maximum of both is the maximum loot possible");
return Math.max(houseArr[0], houseArr[1]);
}
/* base conditions for max loot dp */
int[] maxLootDp = new int[noOfHouse];
maxLootDp[0] = houseArr[0];
maxLootDp[1] = Math.max(houseArr[0], houseArr[1]);
/* since we already know the initial 2 values of maxLootDp array, we will start from 3rd house ie 2nd index */
for (int indx = 2; indx < noOfHouse; indx++) {
maxLootDp[indx] = Math.max(houseArr[indx] + maxLootDp[indx - 2], maxLootDp[indx - 1]);
}
return maxLootDp[noOfHouse - 1];
}
}
package org.wesome.dsalgo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class MaxPossibleLootTest {
@Test
void findMaxPossibleLootTest1() {
int houseArr[] = {6, 7, 1, 3, 8, 2, 4};
Assertions.assertEquals(19, MaxPossibleLoot.findMaxPossibleLoot(houseArr));
}
@Test
void findMaxPossibleLootTest2() {
int houseArr[] = {6, 7};
Assertions.assertEquals(7, MaxPossibleLoot.findMaxPossibleLoot(houseArr));
}
@Test
void findMaxPossibleLootTest3() {
int houseArr[] = {};
Assertions.assertEquals(0, MaxPossibleLoot.findMaxPossibleLoot(houseArr));
}
}
plugins {
id 'java'
id "io.freefair.lombok" version "6.4.1"
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}
test {
useJUnitPlatform()
}