πDay 19. Stickler Thief II π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
You are given an array arr[] representing houses arranged in a circular fashion. Each house contains a certain value, and a thief aims to maximize the stolen amount while ensuring that no two adjacent houses are robbed.
Since the houses form a circle, the first and last house are also considered adjacent.
π Example Walkthrough:
Example 1:
Input:
arr[] = [2, 3, 2]Output:
3Explanation:
The houses at index 0 and 2 are adjacent, so they cannot be robbed together. The maximum amount that can be stolen is 3 (robbing the house at index 1).
Example 2:
Input:
arr[] = [1, 2, 3, 1]Output:
4Explanation:
The optimal strategy is to rob houses at index 0 and 2, which gives 1 + 3 = 4.
Example 3:
Input:
arr[] = [2, 2, 3, 1, 2]Output:
5Explanation:
Possible choices:
Rob house at index 0 and 2 β
2 + 3 = 5Rob house at index 2 and 4 β
3 + 2 = 5The maximum amount that can be stolen is 5.
Constraints:
$(2 \leq \text{arr.size()} \leq 10^5)$
$(0 \leq \text{arr}[i] \leq 10^4)$
π― My Approach:
Optimized Dynamic Programming
Since the houses are arranged in a circle, the first and last houses cannot be robbed together.
The problem reduces to two linear House Robber problems:
Robbing from house 0 to n-2 (excluding the last house).
Robbing from house 1 to n-1 (excluding the first house).
We solve the House Robber problem for both cases using Dynamic Programming with space optimization.
The final answer is the maximum of both cases.
Algorithm Steps:
Define a helper function
robRange(l, r):Use two variables (prev1, prev2) to track maximum money stolen up to the previous two houses.
Iterate through the houses from
ltor, updating the maximum amount stolen.Return
prev1(max stolen amount).
Compute the result by taking the maximum of:
robRange(0, n-2)(excluding last house).robRange(1, n-1)(excluding first house).
Edge case: If there's only one house, return its value.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(N), since we iterate through the houses twice.
Expected Auxiliary Space Complexity: O(1), since we use only a few variables (constant space).
π Solution Code
Code (C++)
class Solution {
public:
int maxValue(vector<int>& nums) {
int n = nums.size();
if (n == 1) return nums[0];
auto robRange = [&](int l, int r) {
int prev2 = 0, prev1 = 0;
for (int i = l; i <= r; i++) {
int curr = max(prev1, prev2 + nums[i]);
prev2 = prev1;
prev1 = curr;
}
return prev1;
};
return max(robRange(0, n - 2), robRange(1, n - 1));
}
};Code (Java)
class Solution {
public int maxValue(int[] nums) {
int n = nums.length;
if (n == 1) return nums[0];
return Math.max(rob(nums, 0, n - 2), rob(nums, 1, n - 1));
}
private int rob(int[] nums, int l, int r) {
int prev2 = 0, prev1 = 0;
for (int i = l; i <= r; i++) {
int curr = Math.max(prev1, prev2 + nums[i]);
prev2 = prev1;
prev1 = curr;
}
return prev1;
}
}Code (Python)
class Solution:
def maxValue(self, nums):
if len(nums) == 1:
return nums[0]
def rob(l, r):
prev2 = prev1 = 0
for i in range(l, r + 1):
prev2, prev1 = prev1, max(prev1, prev2 + nums[i])
return prev1
return max(rob(0, len(nums) - 2), rob(1, len(nums) - 1))π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
π Visitor Count
Last updated