21. Stickler Thief
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
I will use dynamic programming to solve this problem. I will maintain a dynamic programming array dp
, where dp[i]
represents the maximum sum of stolen items up to the i
-th item.
By intuition, we have to skip the i-1
element to add with the current i
th number. To address this, we checked previous maximum possibilities from either i-2
or i-3
and added them to our current number.
Initialize a vector dp
of size n
with all elements set to 0.
Iterate through the items in the array:
If i
is greater than or equal to 2, add dp[i-2]
to dp[i]
. This represents the maximum sum excluding the previous item.
If i
is greater than or equal to 3, compare dp[i]
with dp[i-3]
. If dp[i-3]
is larger, update dp[i]
with this value. This ensures that we don't select adjacent items.
Add the value of the current item arr[i]
to dp[i]
.
Update the out
variable with the maximum of out
and dp[i]
. This keeps track of the overall maximum sum.
Finally, return out
, which contains the maximum sum of stolen items.
Time Complexity: The algorithm iterates through the input array once, so the time complexity is O(n)
, where n
is the number of items in the array.
Auxiliary Space Complexity: We use an additional vector dp
of size n to store the dynamic programming values, so the auxiliary space complexity is O(n)
.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.