Skip to content

Commit 4cf3661

Browse files
committed
Chapter 09 section 04 codes C++ codes updated. Java codes added. new optimized algo added.
1 parent 4c45652 commit 4cf3661

File tree

19 files changed

+532
-30
lines changed

19 files changed

+532
-30
lines changed

09-Dynamic-Programming/Course Code (C++)/04-House-Robber/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ project(04_House_Robber)
33

44
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
55

6-
set(SOURCE_FILES main4.cpp)
6+
set(SOURCE_FILES main5.cpp)
77
add_executable(04_House_Robber ${SOURCE_FILES})

09-Dynamic-Programming/Course Code (C++)/04-House-Robber/main.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,49 @@
33

44
using namespace std;
55

6+
/// 198. House Robber
7+
/// https://leetcode.com/problems/house-robber/description/
8+
/// 记忆化搜索
9+
/// 时间复杂度: O(n^2)
10+
/// 空间复杂度: O(n)
611
class Solution {
12+
713
private:
814
// memo[i] 表示考虑抢劫 nums[i...n) 所能获得的最大收益
915
vector<int> memo;
1016

1117
// 考虑抢劫nums[index...nums.size())这个范围的所有房子
12-
int tryRob( vector<int> &nums, int index){
18+
int tryRob(const vector<int> &nums, int index){
1319

14-
if( index >= nums.size() )
20+
if(index >= nums.size())
1521
return 0;
1622

17-
if( memo[index] != -1 )
23+
if(memo[index] != -1)
1824
return memo[index];
1925

2026
int res = 0;
21-
for( int i = index ; i < nums.size() ; i ++ )
22-
res = max(res, nums[i] + tryRob(nums, i+2));
27+
for(int i = index ; i < nums.size() ; i ++)
28+
res = max(res, nums[i] + tryRob(nums, i + 2));
2329
memo[index] = res;
2430
return res;
2531
}
32+
2633
public:
2734
int rob(vector<int>& nums) {
2835

29-
memo = vector<int>(nums.size(), -1);
36+
memo.clear();
37+
for(int i = 0 ; i < nums.size() ; i ++)
38+
memo.push_back(-1);
3039
return tryRob(nums, 0);
3140
}
3241
};
3342

3443
int main() {
3544

45+
int nums[] = {2, 1};
46+
vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));
47+
48+
cout << Solution().rob(vec) << endl;
49+
3650
return 0;
3751
}

09-Dynamic-Programming/Course Code (C++)/04-House-Robber/main2.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@
33

44
using namespace std;
55

6+
/// 198. House Robber
7+
/// https://leetcode.com/problems/house-robber/description/
8+
/// 动态规划
9+
/// 时间复杂度: O(n^2)
10+
/// 空间复杂度: O(n)
611
class Solution {
712

813
public:
914
int rob(vector<int>& nums) {
1015

1116
int n = nums.size();
1217

13-
if( n == 0 )
18+
if(n == 0)
1419
return 0;
1520

1621
// memo[i] 表示考虑抢劫 nums[i...n) 所能获得的最大收益
1722
vector<int> memo(n, 0);
18-
memo[n-1] = nums[n-1];
19-
for( int i = n-2 ; i >= 0 ; i -- )
23+
memo[n - 1] = nums[n - 1];
24+
for(int i = n - 2 ; i >= 0 ; i --)
2025
for (int j = i; j < n; j++)
21-
memo[i] = max(memo[i], nums[j] + (j + 2 < n ? memo[j + 2] : 0) );
26+
memo[i] = max(memo[i],
27+
nums[j] + (j + 2 < n ? memo[j + 2] : 0));
2228

2329
return memo[0];
2430
}
@@ -27,9 +33,9 @@ class Solution {
2733
int main() {
2834

2935
int nums[] = {2,1};
30-
vector<int> vec(nums, nums+sizeof(nums)/sizeof(int));
36+
vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));
3137

32-
cout<<Solution().rob(vec)<<endl;
38+
cout << Solution().rob(vec) << endl;
3339

3440
return 0;
3541
}

09-Dynamic-Programming/Course Code (C++)/04-House-Robber/main3.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,49 @@
33

44
using namespace std;
55

6-
// 改变状态定义
6+
/// 198. House Robber
7+
/// https://leetcode.com/problems/house-robber/description/
8+
/// 记忆化搜索, 改变状态定义
9+
/// 时间复杂度: O(n^2)
10+
/// 空间复杂度: O(n)
711
class Solution {
12+
813
private:
914
// memo[i] 表示考虑抢劫 nums[0...i] 所能获得的最大收益
1015
vector<int> memo;
1116

1217
// 考虑抢劫nums[0...index]这个范围的所有房子
13-
int tryRob( vector<int> &nums, int index){
18+
int tryRob(const vector<int> &nums, int index){
1419

15-
if( index < 0 )
20+
if(index < 0)
1621
return 0;
1722

18-
if( memo[index] != -1 )
23+
if(memo[index] != -1)
1924
return memo[index];
2025

2126
int res = 0;
22-
for( int i = 0 ; i <= index ; i ++ )
23-
res = max(res, nums[i] + tryRob(nums, i-2));
27+
for(int i = 0 ; i <= index ; i ++)
28+
res = max(res, nums[i] + tryRob(nums, i - 2));
2429
memo[index] = res;
2530
return res;
2631
}
32+
2733
public:
2834
int rob(vector<int>& nums) {
2935

30-
memo = vector<int>(nums.size(), -1);
36+
memo.clear();
37+
for(int i = 0 ; i < nums.size() ; i ++)
38+
memo.push_back(-1);
3139
return tryRob(nums, nums.size() - 1 );
3240
}
3341
};
3442

3543
int main() {
3644

45+
int nums[] = {2, 1};
46+
vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));
47+
48+
cout << Solution().rob(vec) << endl;
49+
3750
return 0;
3851
}

09-Dynamic-Programming/Course Code (C++)/04-House-Robber/main4.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
using namespace std;
55

6-
// 改变状态定义
6+
/// 198. House Robber
7+
/// https://leetcode.com/problems/house-robber/description/
8+
/// 动态规划, 改变状态定义
9+
/// 时间复杂度: O(n^2)
10+
/// 空间复杂度: O(n)
711
class Solution {
812

913
public:
@@ -16,20 +20,21 @@ class Solution {
1620
// memo[i] 表示考虑抢劫 nums[0...i] 所能获得的最大收益
1721
vector<int> memo(n, 0);
1822
memo[0] = nums[0];
19-
for( int i = 1 ; i < n ; i ++ )
23+
for(int i = 1 ; i < n ; i ++)
2024
for (int j = i; j >= 0; j--)
21-
memo[i] = max(memo[i], nums[j] + (j - 2 >= 0 ? memo[j - 2] : 0) );
25+
memo[i] = max(memo[i],
26+
nums[j] + (j - 2 >= 0 ? memo[j - 2] : 0));
2227

2328
return memo[n-1];
2429
}
2530
};
2631

2732
int main() {
2833

29-
int nums[] = {2,1};
30-
vector<int> vec(nums, nums+sizeof(nums)/sizeof(int));
34+
int nums[] = {2, 1};
35+
vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));
3136

32-
cout<<Solution().rob(vec)<<endl;
37+
cout << Solution().rob(vec) << endl;
3338

3439
return 0;
3540
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
/// 198. House Robber
7+
/// https://leetcode.com/problems/house-robber/description/
8+
/// 记忆化搜索, 优化状态转移
9+
/// 时间复杂度: O(n)
10+
/// 空间复杂度: O(n)
11+
class Solution {
12+
private:
13+
// memo[i] 表示考虑抢劫 nums[i...n) 所能获得的最大收益
14+
vector<int> memo;
15+
16+
// 考虑抢劫nums[index...nums.size())这个范围的所有房子
17+
int tryRob(const vector<int> &nums, int index){
18+
19+
if(index >= nums.size())
20+
return 0;
21+
22+
if(memo[index] != -1)
23+
return memo[index];
24+
25+
// 或者当前房子放弃, 从下一个房子开始考虑
26+
// 或者抢劫当前的房子, 从i+2以后的房子开始考虑
27+
return memo[index] = max(tryRob(nums, index + 1),
28+
nums[index] + tryRob(nums, index + 2));
29+
}
30+
31+
public:
32+
int rob(vector<int>& nums) {
33+
34+
memo.clear();
35+
for(int i = 0 ; i < nums.size() ; i ++)
36+
memo.push_back(-1);
37+
return tryRob(nums, 0);
38+
}
39+
};
40+
41+
int main() {
42+
43+
int nums[] = {2, 1};
44+
vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));
45+
46+
cout << Solution().rob(vec) << endl;
47+
48+
return 0;
49+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
/// 198. House Robber
7+
/// https://leetcode.com/problems/house-robber/description/
8+
/// 动态规划, 优化状态转移
9+
/// 时间复杂度: O(n)
10+
/// 空间复杂度: O(n)
11+
class Solution {
12+
13+
public:
14+
int rob(vector<int>& nums) {
15+
16+
int n = nums.size();
17+
18+
if(n == 0)
19+
return 0;
20+
21+
// memo[i] 表示考虑抢劫 nums[i...n) 所能获得的最大收益
22+
vector<int> memo(n, 0);
23+
memo[n - 1] = nums[n - 1];
24+
for(int i = n - 2 ; i >= 0 ; i --)
25+
// 或者当前房子放弃, 从下一个房子开始考虑
26+
// 或者抢劫当前的房子, 从i+2以后的房子开始考虑
27+
memo[i] = max(memo[i + 1],
28+
nums[i] + (i + 2 < n ? memo[i + 2] : 0));
29+
30+
return memo[0];
31+
}
32+
};
33+
34+
int main() {
35+
36+
int nums[] = {2, 1};
37+
vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));
38+
39+
cout << Solution().rob(vec) << endl;
40+
41+
return 0;
42+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
/// 198. House Robber
7+
/// https://leetcode.com/problems/house-robber/description/
8+
/// 记忆化搜索, 改变状态定义, 优化转移方程
9+
/// 时间复杂度: O(n)
10+
/// 空间复杂度: O(n)
11+
class Solution {
12+
13+
private:
14+
// memo[i] 表示考虑抢劫 nums[0...i] 所能获得的最大收益
15+
vector<int> memo;
16+
17+
// 考虑抢劫nums[0...index]这个范围的所有房子
18+
int tryRob(const vector<int> &nums, int index){
19+
20+
if(index < 0)
21+
return 0;
22+
23+
if(memo[index] != -1)
24+
return memo[index];
25+
26+
// 或者当前房子放弃, 考虑[0...index-1]的所有房子
27+
// 或者抢劫当前的房子, 考虑[0...index-2]的所有房子
28+
return memo[index] = max(tryRob(nums, index - 1),
29+
nums[index] + tryRob(nums, index - 2));
30+
}
31+
32+
public:
33+
int rob(vector<int>& nums) {
34+
35+
memo.clear();
36+
for(int i = 0 ; i < nums.size() ; i ++)
37+
memo.push_back(-1);
38+
return tryRob(nums, nums.size() - 1 );
39+
}
40+
};
41+
42+
int main() {
43+
44+
int nums[] = {2, 1};
45+
vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));
46+
47+
cout << Solution().rob(vec) << endl;
48+
49+
return 0;
50+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
/// 198. House Robber
7+
/// https://leetcode.com/problems/house-robber/description/
8+
/// 动态规划, 改变状态定义, 优化转移方程
9+
/// 时间复杂度: O(n)
10+
/// 空间复杂度: O(n)
11+
class Solution {
12+
13+
public:
14+
int rob(vector<int>& nums) {
15+
16+
int n = nums.size();
17+
if( n == 0 )
18+
return 0;
19+
20+
// memo[i] 表示考虑抢劫 nums[0...i] 所能获得的最大收益
21+
vector<int> memo(n, 0);
22+
memo[0] = nums[0];
23+
for(int i = 1 ; i < n ; i ++)
24+
memo[i] = max(memo[i - 1],
25+
nums[i] + (i - 2 >= 0 ? memo[i - 2] : 0));
26+
27+
return memo[n-1];
28+
}
29+
};
30+
31+
int main() {
32+
33+
int nums[] = {2, 1};
34+
vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));
35+
36+
cout << Solution().rob(vec) << endl;
37+
38+
return 0;
39+
}

0 commit comments

Comments
 (0)