problem Ⅰ
213. House Robber II
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: nums = [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
Example 2:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 3:
Input: nums = [1,2,3]
Output: 3
solution 1
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size()==1)return nums[0];
if(nums.size()==2)return max(nums[0], nums[1]);
if(nums.size()==3)return max(max(nums[0], nums[1]), nums[2]);
vector<int> sum1(nums.size(), 0), sum2(nums.size(), 0);
sum1[2] = nums[0]+nums[2];
sum1[3] = max(nums[2], nums[3])+nums[0];
sum2[1] = nums[1];
sum2[2] = max(nums[1], nums[2]);
int i=3;
for(; i<nums.size()-2; i++){
sum1[i+1] = max(nums[i+1]+sum1[i-1], sum1[i]);
sum2[i] = max(nums[i]+sum2[i-2], sum2[i-1]);
}
--i;
for(; i<nums.size(); i++)
sum2[i] = max(nums[i]+sum2[i-2], sum2[i-1]);
return max(sum1[nums.size()-2], sum2[nums.size()-1]);
}
};
NOTE :
this problem is an enhanced version of this one
time complexity
:
O
(
n
)
O(n)
O(n)
space complexity
:
O
(
n
)
O(n)
O(n)
solution 2 short code
class Solution {
public:
int houseRobber(vector<int>& nums) {
int dp[nums.size()+1];
dp[0] = nums[0];
dp[1] = max(nums[0], nums[1]);
for (int i=2; i<nums.size(); i++)
dp[i] = max(dp[i-1], nums[i]+dp[i-2]);
return dp[nums.size()-1];
}
int rob(vector<int>& nums) {
if (nums.size() == 0) return 0;
if (nums.size() == 1) return nums[0];
if (nums.size() == 2) return max(nums[0], nums[1]);
vector<int> v1(nums.begin(), nums.end()-1);
vector<int> v2(nums.begin()+1, nums.end());
return max(houseRobber(v1), houseRobber(v2));
}
};
time complexity
:
O
(
2
n
)
O(2n)
O(2n)
space complexity
:
O
(
n
)
O(n)
O(n)
problem Ⅱ
740. Delete and Earn
You are given an integer array nums. You want to maximize the number of points you get by performing the following operation any number of times:
Pick any nums[i] and delete it to earn nums[i] points. Afterwards, you must delete every element equal to nums[i] - 1 and every element equal to nums[i] + 1.
Return the maximum number of points you can earn by applying the above operation some number of times.
Example 1:
Input: nums = [3,4,2]
Output: 6
Explanation: You can perform the following operations:
- Delete 4 to earn 4 points. Consequently, 3 is also deleted. nums = [2].
- Delete 2 to earn 2 points. nums = [].
You earn a total of 6 points.
Example 2:
Input: nums = [2,2,3,3,3,4]
Output: 9
Explanation: You can perform the following operations:
- Delete a 3 to earn 3 points. All 2's and 4's are also deleted. nums = [3,3].
- Delete a 3 again to earn 3 points. nums = [3].
- Delete a 3 once more to earn 3 points. nums = [].
You earn a total of 9 points.
solution 1
after sort(), if n have neighbors n-1 or n+1, they must be around n, so every deletion of n is equvalent
so we just need to convert [1 1 1 1 2 2 3 3 3] into [4 4 9]
and it just a simple dp problem
class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
//time : O(n), space : O(logn)~O(n)
sort(nums.begin(), nums.end());
//time : O(n), space : O(dis : O(w) + val : O(w))=O(2w)=O(w)
vector<int> val, dis;
dis.push_back(0);
int vals = nums[0];
for(int i=0; i<nums.size(); i++){//O(n)
if(i==nums.size()-1){
val.push_back(vals);
break;
}
if(nums[i]==nums[i+1]){
vals += nums[i+1];
}else{
val.push_back(vals);
vals = nums[i+1];
dis.push_back(nums[i+1]-nums[i]);
}
}
if(val.size()==1)return val[0];
//time : O(n), space : O(1)
int dp1=val[0], dp2;
if(dis[1] != 1)dp2 = val[0]+val[1];
else dp2 = max(val[0], val[1]);
for(int i=2; i<val.size(); i++){// O(n)
int t = dp2;
if(dis[i] != 1) dp2 = val[i]+dp2;
else dp2 = max(val[i]+dp1, dp2);
dp1 = t;
}
return dp2;
}
};
NOTE:
time complexity
:
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)
space complexity
:
O
(
w
)
O(w)
O(w)
w
: range of values
solution 2 Judgment method
class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
vector<int> count(10001, 0);
for(int x : nums)count[x]++;
int dp1 = 0, dp2 = 0, prev = -1;
for(int k=0; k<=10000; k++){
if(count[k]){
int m = max(dp1, dp2);
if(k-1 != prev){
dp2 = k*count[k]+m;
dp1 = m;
}else{
dp2 = k*count[k]+dp1;
dp1 = m;
}
prev = k;
}
}
return max(dp1, dp2);
}
};
NOTE:
time complexity
:
O
(
n
+
w
)
O(n+w)
O(n+w)
space complexity
:
O
(
w
)
O(w)
O(w)
w
: range of values