Maximize Non-Adjacent Harvest
A farmer owns a long strip of land divided into several plots, each yielding a certain value of crops. To maintain soil health and prevent pest spread, the farmer has a strict rule: no two adjacent plots can be harvested in the same season. The farmer wants to maximize the total value of crops harvested under this constraint. This problem is critical for optimizing agricultural yield and making efficient resource allocation decisions.
Your task is to implement a function maximizeNonAdjacentHarvest that calculates the maximum total value of crops the farmer can harvest from a given row of plots, adhering to the non-adjacent harvesting rule.
plotValues - An array of non-negative integers (number[] in TypeScript, array<number> in JavaScript), where plotValues[i] represents the monetary value of crops in the i-th plot.plotValues is an empty array, the maximum harvest value is 0.plotValues contains only one plot, the maximum harvest value is the value of that single plot.plotValues[i] >= 0).Examples
Example 1
Input:
plotValues = [1, 2, 3, 1]
Output:
4
Note:
The maximum value is achieved by harvesting plots with values 1 and 3 (total 4). Picking 2 and 1 would yield 3, but 1 and 3 are better.
Example 2
Input:
plotValues = [2, 7, 9, 3, 1]
Output:
12
Note:
The maximum value is achieved by harvesting plots with values 2, 9, and 1 (total 12).
Example 3
Input:
plotValues = [0]
Output:
0
Note:
With only one plot of value 0, the maximum harvest is 0.
Example 4
Input:
plotValues = []
Output:
0
Note:
An empty field yields no harvest.
Constraints
0 <= plotValues.length <= 10^50 <= plotValues[i] <= 1000plotValues will only contain non-negative integers.plotValues will always be a valid array of non-negative numbers).Loading editor...