Skip to content

Commit 0f46694

Browse files
committed
🚀 29-Aug-2020
1 parent 03a30b3 commit 0f46694

7 files changed

+502
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Given two strings text1 and text2, return the length of their longest common subsequence.
2+
3+
A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.
4+
5+
6+
7+
If there is no common subsequence, return 0.
8+
9+
10+
11+
Example 1:
12+
13+
Input: text1 = "abcde", text2 = "ace"
14+
Output: 3
15+
Explanation: The longest common subsequence is "ace" and its length is 3.
16+
Example 2:
17+
18+
Input: text1 = "abc", text2 = "abc"
19+
Output: 3
20+
Explanation: The longest common subsequence is "abc" and its length is 3.
21+
Example 3:
22+
23+
Input: text1 = "abc", text2 = "def"
24+
Output: 0
25+
Explanation: There is no such common subsequence, so the result is 0.
26+
27+
28+
Constraints:
29+
30+
1 <= text1.length <= 1000
31+
1 <= text2.length <= 1000
32+
The input strings consist of lowercase English characters only.
33+
34+
35+
36+
37+
38+
39+
class Solution {
40+
public:
41+
42+
int LCS(string text1, string text2, int m, int n){
43+
int dp[m+1][n+1];
44+
for(int i=0;i<m+1;i++) dp[i][0]=0;
45+
for(int j=0;j<n+1;j++) dp[0][j]=0;
46+
47+
for(int i=1;i<m+1;i++){
48+
for(int j=1;j<n+1;j++){
49+
if(text1[i-1]==text2[j-1])
50+
dp[i][j]=1+dp[i-1][j-1];
51+
else dp[i][j]=max(dp[i-1][j], dp[i][j-1]);
52+
}
53+
}
54+
return dp[m][n];
55+
}
56+
57+
int longestCommonSubsequence(string text1, string text2) {
58+
int m=text1.length();
59+
int n=text2.length();
60+
61+
return LCS(text1, text2, m, n);
62+
}
63+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Given the API rand7 which generates a uniform random integer in the range 1 to 7, write a function rand10 which generates a uniform random integer in the range 1 to 10. You can only call the API rand7 and you shouldn't call any other API. Please don't use the system's Math.random().
2+
3+
Notice that Each test case has one argument n, the number of times that your implemented function rand10 will be called while testing.
4+
5+
Follow up:
6+
7+
What is the expected value for the number of calls to rand7() function?
8+
Could you minimize the number of calls to rand7()?
9+
10+
11+
Example 1:
12+
13+
Input: n = 1
14+
Output: [2]
15+
Example 2:
16+
17+
Input: n = 2
18+
Output: [2,8]
19+
Example 3:
20+
21+
Input: n = 3
22+
Output: [3,8,10]
23+
24+
25+
Constraints:
26+
27+
1 <= n <= 105
28+
29+
30+
31+
32+
33+
34+
35+
// The rand7() API is already defined for you.
36+
// int rand7();
37+
// @return a random integer in the range 1 to 7
38+
39+
class Solution {
40+
public:
41+
int rand10() {
42+
while(1){
43+
int row=(rand7()-1)*7;
44+
int col=rand7();
45+
int curr=row+col;
46+
if(curr<=40)
47+
return (curr-1)%10 + 1;
48+
}
49+
}
50+
};
51+
52+
53+
54+
/*
55+
rand7() gives a random number in range of 1 to 7
56+
rand7()-1 gives a random number in range of 0 to 6
57+
(rand7()-1)*7 gives a random number in range of 0 to 42
58+
For a uniform distribution we want only numbers in range of 0 to 40
59+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Given an unsorted array of integers, find the length of longest increasing subsequence.
2+
3+
Example:
4+
5+
Input: [10,9,2,5,3,7,101,18]
6+
Output: 4
7+
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
8+
Note:
9+
10+
There may be more than one LIS combination, it is only necessary for you to return the length.
11+
Your algorithm should run in O(n2) complexity.
12+
Follow up: Could you improve it to O(n log n) time complexity?
13+
14+
15+
16+
17+
// O(n^2)
18+
19+
class Solution {
20+
public:
21+
int lengthOfLIS(vector<int>& nums) {
22+
int n=nums.size();
23+
if(n==0) return 0;
24+
vector<int> lis(n,1);
25+
for(int i=1;i<n;i++){
26+
for(int j=0; j<i; j++){
27+
if(nums[i]>nums[j] && lis[i] < lis[j]+1)
28+
lis[i]=lis[j]+1;
29+
}
30+
}
31+
return *max_element(lis.begin(), lis.end());
32+
}
33+
};
34+
35+
36+
37+
// O(nlogn)
38+
39+
class Solution {
40+
public:
41+
int lengthOfLIS(vector<int>& nums) {
42+
int n=nums.size();
43+
if(n==0) return 0;
44+
vector<int> seq;
45+
seq.push_back(nums[0]);
46+
47+
for(int i=1;i<n;i++){
48+
if(seq.back()<nums[i])
49+
seq.push_back(nums[i]);
50+
else {
51+
int index=lower_bound(seq.begin(), seq.end(), nums[i])-seq.begin();
52+
seq[index]=nums[i];
53+
}
54+
}
55+
return seq.size();
56+
}
57+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Given the API rand7 which generates a uniform random integer in the range 1 to 7, write a function rand10 which generates a uniform random integer in the range 1 to 10. You can only call the API rand7 and you shouldn't call any other API. Please don't use the system's Math.random().
2+
3+
Notice that Each test case has one argument n, the number of times that your implemented function rand10 will be called while testing.
4+
5+
Follow up:
6+
7+
What is the expected value for the number of calls to rand7() function?
8+
Could you minimize the number of calls to rand7()?
9+
10+
11+
Example 1:
12+
13+
Input: n = 1
14+
Output: [2]
15+
Example 2:
16+
17+
Input: n = 2
18+
Output: [2,8]
19+
Example 3:
20+
21+
Input: n = 3
22+
Output: [3,8,10]
23+
24+
25+
Constraints:
26+
27+
1 <= n <= 105
28+
29+
30+
31+
32+
33+
34+
35+
// The rand7() API is already defined for you.
36+
// int rand7();
37+
// @return a random integer in the range 1 to 7
38+
39+
class Solution {
40+
public:
41+
int rand10() {
42+
while(1){
43+
int row=(rand7()-1)*7;
44+
int col=rand7();
45+
int curr=row+col;
46+
if(curr<=40)
47+
return (curr-1)%10 + 1;
48+
}
49+
}
50+
};
51+
52+
53+
54+
/*
55+
rand7() gives a random number in range of 1 to 7
56+
rand7()-1 gives a random number in range of 0 to 6
57+
(rand7()-1)*7 gives a random number in range of 0 to 42
58+
For a uniform distribution we want only numbers in range of 0 to 40
59+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
2+
3+
You have the following 3 operations permitted on a word:
4+
5+
Insert a character
6+
Delete a character
7+
Replace a character
8+
Example 1:
9+
10+
Input: word1 = "horse", word2 = "ros"
11+
Output: 3
12+
Explanation:
13+
horse -> rorse (replace 'h' with 'r')
14+
rorse -> rose (remove 'r')
15+
rose -> ros (remove 'e')
16+
Example 2:
17+
18+
Input: word1 = "intention", word2 = "execution"
19+
Output: 5
20+
Explanation:
21+
intention -> inention (remove 't')
22+
inention -> enention (replace 'i' with 'e')
23+
enention -> exention (replace 'n' with 'x')
24+
exention -> exection (replace 'n' with 'c')
25+
exection -> execution (insert 'u')
26+
27+
28+
29+
30+
31+
// Recursive (TLE)
32+
33+
class Solution {
34+
public:
35+
36+
int editDistance(string word1, string word2, int m, int n){
37+
if(m==0) return n;
38+
if(n==0) return m;
39+
40+
if(word1[m-1]==word2[n-1]) return editDistance(word1, word2, m-1, n-1);
41+
return 1 + min(editDistance(word1, word2, m-1, n), min(editDistance(word1, word2, m, n-1), editDistance(word1, word2, m-1, n-1)));
42+
}
43+
44+
int minDistance(string word1, string word2) {
45+
int m=word1.length();
46+
int n=word2.length();
47+
48+
return editDistance(word1, word2, m, n);
49+
}
50+
};
51+
52+
53+
// Recursive (memoized)
54+
55+
class Solution {
56+
public:
57+
58+
int editDistance(string word1, string word2, int m, int n, vector<vector<int> > &dp){
59+
if(m==0) return n;
60+
if(n==0) return m;
61+
62+
if(dp[m-1][n-1]!=-1) return dp[m-1][n-1];
63+
64+
if(word1[m-1]==word2[n-1]) return dp[m-1][n-1]=editDistance(word1, word2, m-1, n-1, dp);
65+
return dp[m-1][n-1]= 1 + min(editDistance(word1, word2, m-1, n, dp), min(editDistance(word1, word2, m, n-1, dp), editDistance(word1, word2, m-1, n-1, dp)));
66+
}
67+
68+
int minDistance(string word1, string word2) {
69+
int m=word1.length();
70+
int n=word2.length();
71+
72+
vector<vector<int> > dp(m+1, vector<int> (n+1, -1));
73+
74+
return editDistance(word1, word2, m, n, dp);
75+
}
76+
};
77+
78+
79+
80+
// DP
81+
82+
class Solution {
83+
public:
84+
85+
int editDistance(string word1, string word2, int m, int n){
86+
int dp[m+1][n+1];
87+
88+
for(int i=0;i<m+1;i++) dp[i][0]=i;
89+
for(int j=0;j<n+1;j++) dp[0][j]=j;
90+
91+
for(int i=1;i<m+1;i++){
92+
for(int j=1;j<n+1;j++){
93+
if(word1[i-1]==word2[j-1]) dp[i][j]=dp[i-1][j-1];
94+
else dp[i][j]= 1+ min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]));
95+
}
96+
}
97+
return dp[m][n];
98+
}
99+
100+
int minDistance(string word1, string word2) {
101+
int m=word1.length();
102+
int n=word2.length();
103+
104+
return editDistance(word1, word2, m, n);
105+
}
106+
};

0 commit comments

Comments
 (0)