Skip to content

Commit 953a4d7

Browse files
committed
🚀 16-Sep-2020
1 parent 6165160 commit 953a4d7

File tree

4 files changed

+248
-5
lines changed

4 files changed

+248
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Recursive (TLE)
2+
3+
class Solution {
4+
public:
5+
6+
int robUtil(vector<int> &nums, int i){
7+
if(i<0)
8+
return 0;
9+
return max(nums[i]+robUtil(nums, i-2), robUtil(nums, i-1));
10+
}
11+
12+
int rob(vector<int>& nums) {
13+
return robUtil(nums, nums.size()-1);
14+
}
15+
};
16+
17+
18+
19+
20+
// Recursive memoized
21+
22+
class Solution {
23+
public:
24+
25+
int robUtil(vector<int> &nums, int i, vector<int> &memo){
26+
if(i<0)
27+
return 0;
28+
if(memo[i]>=0) return memo[i];
29+
30+
return memo[i]=max(nums[i]+robUtil(nums, i-2, memo), robUtil(nums, i-1, memo));
31+
}
32+
33+
int rob(vector<int>& nums) {
34+
vector<int> memo(nums.size(), -1);
35+
return robUtil(nums, nums.size()-1, memo);
36+
}
37+
};
38+
39+
40+
41+
42+
/*
43+
Step 1. Figure out recursive relation.
44+
A robber has 2 options: a) rob current house i; b) don't rob current house.
45+
If an option "a" is selected it means she can't rob previous i-1 house but can safely proceed to the one before previous i-2 and gets all cumulative loot that follows.
46+
If an option "b" is selected the robber gets all the possible loot from robbery of i-1 and all the following buildings.
47+
So it boils down to calculating what is more profitable:
48+
49+
robbery of current house + loot from houses before the previous
50+
loot from the previous house robbery and any loot captured before that
51+
rob(i) = Math.max( rob(i - 2) + currentHouseValue, rob(i - 1) )
52+
53+
Step 2. Recursive (top-down)
54+
Converting the recurrent relation from Step 1 shound't be very hard.
55+
56+
Step 3. Recursive + memo (top-down).
57+
58+
59+
//Sourece: https://leetcode.com/problems/house-robber/discuss/156523/From-good-to-great.-How-to-approach-most-of-DP-problems.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Recursive (TLE)
2+
3+
class Solution {
4+
public:
5+
6+
int robUtil(vector<int> &nums, int i){
7+
if(i<0)
8+
return 0;
9+
return max(nums[i]+robUtil(nums, i-2), robUtil(nums, i-1));
10+
}
11+
12+
int rob(vector<int>& nums) {
13+
return robUtil(nums, nums.size()-1);
14+
}
15+
};
16+
17+
18+
19+
20+
// Recursive memoized
21+
22+
class Solution {
23+
public:
24+
25+
int robUtil(vector<int> &nums, int i, vector<int> &memo){
26+
if(i<0)
27+
return 0;
28+
if(memo[i]>=0) return memo[i];
29+
30+
return memo[i]=max(nums[i]+robUtil(nums, i-2, memo), robUtil(nums, i-1, memo));
31+
}
32+
33+
int rob(vector<int>& nums) {
34+
vector<int> memo(nums.size(), -1);
35+
return robUtil(nums, nums.size()-1, memo);
36+
}
37+
};
38+
39+
40+
41+
42+
/*
43+
Step 1. Figure out recursive relation.
44+
A robber has 2 options: a) rob current house i; b) don't rob current house.
45+
If an option "a" is selected it means she can't rob previous i-1 house but can safely proceed to the one before previous i-2 and gets all cumulative loot that follows.
46+
If an option "b" is selected the robber gets all the possible loot from robbery of i-1 and all the following buildings.
47+
So it boils down to calculating what is more profitable:
48+
49+
robbery of current house + loot from houses before the previous
50+
loot from the previous house robbery and any loot captured before that
51+
rob(i) = Math.max( rob(i - 2) + currentHouseValue, rob(i - 1) )
52+
53+
Step 2. Recursive (top-down)
54+
Converting the recurrent relation from Step 1 shound't be very hard.
55+
56+
Step 3. Recursive + memo (top-down).
57+
58+
59+
//Sourece: https://leetcode.com/problems/house-robber/discuss/156523/From-good-to-great.-How-to-approach-most-of-DP-problems.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.
2+
3+
If the last word does not exist, return 0.
4+
5+
Note: A word is defined as a maximal substring consisting of non-space characters only.
6+
7+
Example:
8+
9+
Input: "Hello World"
10+
Output: 5
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
class Solution {
24+
public:
25+
int lengthOfLastWord(string s) {
26+
reverse(s.begin(), s.end());
27+
stringstream ss(s);
28+
string tmp;
29+
ss>>tmp;
30+
return tmp.length();
31+
}
32+
};
33+
34+
35+
36+
37+
38+
39+
40+
41+
class Solution {
42+
public:
43+
int lengthOfLastWord(string s) {
44+
stringstream ss(s);
45+
string tmp;
46+
while(ss>>tmp);
47+
return tmp.length();
48+
}
49+
};
50+
51+
52+
53+
54+
55+
56+
57+
58+
class Solution {
59+
public:
60+
int lengthOfLastWord(string s) {
61+
int tail=s.length()-1, len=0;
62+
while(tail>=0 && s[tail]==' ')
63+
tail--;
64+
while(tail>=0 && s[tail]!=' '){
65+
len++;
66+
tail--;
67+
}
68+
return len;
69+
}
70+
};

competitive programming/leetcode/58. Length of Last Word.cpp

+60-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.
1+
NGiven a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.
22

33
If the last word does not exist, return 0.
44

@@ -20,6 +20,64 @@ Output: 5
2020

2121

2222

23+
class Solution {
24+
public:
25+
int lengthOfLastWord(string s) {
26+
reverse(s.begin(), s.end());
27+
stringstream ss(s);
28+
string tmp;
29+
ss>>tmp;
30+
return tmp.length();
31+
}
32+
};
33+
34+
35+
36+
37+
38+
39+
40+
41+
class Solution {
42+
public:
43+
int lengthOfLastWord(string s) {
44+
stringstream ss(s);
45+
string tmp;
46+
while(ss>>tmp);
47+
return tmp.length();
48+
}
49+
};
50+
51+
52+
53+
54+
55+
56+
57+
58+
class Solution {
59+
public:
60+
int lengthOfLastWord(string s) {
61+
int tail=s.length()-1, len=0;
62+
while(tail>=0 && s[tail]==' ')
63+
tail--;
64+
while(tail>=0 && s[tail]!=' '){
65+
len++;
66+
tail--;
67+
}
68+
return len;
69+
}
70+
};
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
2381
class Solution {
2482
public:
2583
int lengthOfLastWord(string s) {
@@ -28,10 +86,7 @@ class Solution {
2886
for(int i=n-1;i>=0;i--){
2987
if(s[i]!=' ')
3088
len++;
31-
else {
32-
if(len>0) break;
33-
else len=0;
34-
}
89+
else if(len>0) break;
3590
}
3691
return len;
3792
}

0 commit comments

Comments
 (0)