Question: Please code in Java. Thanks! You build homes out of wood and you need material from a nearby forest. However, you want to avoid deforestation,
Please code in Java. Thanks!
You build homes out of wood and you need material from a nearby forest. However, you want to avoid deforestation, so you decide for each tree you cut down, you'll leave its neighbors alone, giving the forest time to recover. However, you still need as much wood as possible, so you have to be careful about which trees you pick to cut down. Write pickTrees, which takes in an array of N trees arr where arx[i] represents how much wood you can harvest by cutting down tree i, It should return the max amount of wood you can harvest while following the rule of skipping neighbors: // Pick tree o, tree 2, and tree 4 => 1 + 3 + 5 = 9 wood total int testresult5 = Ricktrees(new int[] {1, 2, 3, 4, 5}); System.out.println(testResult5); // should output = // Pick tree 1 and tree 3 => 3 + 3 6 wood total int testResult6 = Ricktrees(new int[] {1, 3, 4, 3}); System.outeprintln(testResult); // should output 6 // Pick tree and tree 3 => 5 + 9 = 14 wood total int testResult7 = Ricktrees(new int[] {5, 1, 4, 9}); System.outekrintin(testResult7); // should output 14
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
