Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*' where:
The matching should cover the entire input string (not partial).
Constraints:
Examples:
Input 1:
s = "aa", p = "a"
Output 1:
false
Explanation:
"a" does not match the entire string "aa".
Example 2:
Input 2:
s = "aa", p = "*"
Output 2:
true
Explanation:
'*' matches any sequence.
Input 3:
s = "cb", p = "?a"
Output 3:
false
Explanation:
'?' matches 'c', but the second letter is 'a', which does not match 'b'.
Given an integer array nums, find the subarray which has the largest sum and return its sum.
Constraints:
Examples:
Input 1:
nums = [-2,1,-3,4,-1,2,1,-5,4]
Output:
6
Explanation:
[4,-1,2,1] has the largest sum = 6.
Input 2:
nums = [1]
Output 2:
1
Input 3:
nums = [5,4,-1,7,8]
Output 3:
23
Given two binary trees, you have to overlap Tree 2 over Tree 1.
Example
Tree 1: 4, 5, 6, 1, Null, 4, Null
Tree 2: 7, -2, -6, Null, 5, 4, 3
Output:
7, -2, -6, 1, -5, 4 ,3
Homes are aligned in a row. In any given row, maximum half of the homes are occupied. In a season, people got infected and all must vacate homes to move to unoccupied homes. Movements are sequential and not parallel (i.e one home completes the movement, then only the second home starts). To reduce the overall time taken, it's better to move to a nearby empty room than a farther one. i'th home to j'th home movement results in abs(j-i) distance. Minimize the overall distance covered by the time all infected home completes the movement to unoccupied homes.
Input : n = no of homes String s denoting occupied and unoccupied homes(1 = occupied, 0 = unoccupied)
Example:
Input
n = 4
"1001"
Output:
2
A string S of length N has to be reconstructed from 2(start, end)+k substrings(random length). Start substring is a prefix of S and end substring is a suffix of S. These substrings can be overlapping each other from right, left or both sides with minimum of 4 characters.
Example:
Input
abcde123, de123xyz, 3xyzpqr
Output :
abcde123xyzpqr
There are n balloons and n bullets and each balloon is assigned with a particular number (point).
Whenever a particular balloon is shot the no of points increases by
You have to output the maximum no of points possible.
Input
1 2 3 4
Output
20
This is a well known dynamic programming problem which uses the idea of string matching DP.
string str , pat;
int dp[2001][2001];
int dfs(int i , int j){
//BASE CASE
if(i == str.size() && j == pat.size())return 1;
if(i == str.size() && j != pat.size()){
if(pat[j]=='*')return dp[i][j] = dfs(i , j+1);
else return dp[i][j] = 0;
}
if(i != str.size() && j == pat.size())return 0;
if(dp[i][j] != -1)return dp[i][j];
int val = 0;
// Case 1 : string matches with pattern , move both pointers ahead.
if(str[i] == pat[j])val |= dfs(i+1 , j+1);
// Case 2 : Match '?' with the string pointer , move both pointers ahead.
if(pat[j] == '?')val |= dfs(i+1 , j+1);
// Case 3 : Match '*' with zero or more string pointers.
if(pat[j] == '*'){
val |= dfs(i+1,j);
val |= dfs(i+1,j+1);
val |= dfs(i , j+1);
}
return dp[i][j] = val;
}
This is a standard problem, usually done by Kadane's algorithm.
Following are the steps for Kadane's algorithm:
int maxSubarraySum(vector<int> a)
{
int n=a.size();
int maxSoFar=-1e9,maxEndingHere=0;
for(int i = 0;i < n;i++)
{
maxEndingHere=maxEndingHere+a[i];
maxSoFar=max(maxSoFar,maxEndingHere);
maxEndingHere=max(maxEndingHere,0ll);
}
return maxSoFar;
}
Solution-Q3
Approach
Step 1: Generate the binary trees from the input.
Step 2: Merge the binary trees.
Step 3: Decompose the binary tree back into array format.
Here are the required function codes:
node* overlapTrees(node* r1, node* r2) {
if(r2 == nullptr)
return r1;
if(r1 == nullptr) {
return r2;
}
r2 -> left = overlapTrees(r1 -> left, r2 -> left);
r2 -> right = overlapTrees(r1 -> right, r2 -> right);
return r2;
}
vector <int> flattenTree(node* root) {
node* cur = root;
vector <int> tree;
queue <node*> q;
q.push(root);
if(cur != nullptr) {
tree.push_back(root -> val);
} else {
tree.push_back(0);
}
while(!q.empty()) {
if(cur -> left == nullptr && cur -> right == nullptr) {
q.pop();
continue;
}
if(cur -> left != nullptr) {
tree.push_back(cur -> left -> val);
q.push(cur -> left);
} else {
tree.push_back(0);
}
if(cur -> right != nullptr) {
tree.push_back(cur -> right -> val);
q.push(cur -> right);
} else {
tree.push_back(0);
}
q.pop();
if(!q.empty()) {
cur = q.front();
}
}
return tree;
}
node* tree_create(int* arr, int n){
queue <node*> q;
int i = 1;
bool l = true;
node* root = new node(arr[0]);
node* cur = root;
q.push(cur);
while(!q.empty() && i < n){
if(l) {
l ^= 1;
if(arr[i] != 0) {
cur -> left = new node(arr[i]);
q.push(cur -> left);
} else {
cur -> left = nullptr;
}
} else {
l ^= 1;
if(arr[i] != 0) {
cur -> right = new node(arr[i]);
q.push(cur -> right);
} else {
cur -> right = nullptr;
}
q.pop();
if(!q.empty()) {
cur = q.front();
}
}
i++;
}
return root;
}
Question 4
overview:
i
to house number j
is equal to abs(i-j)
Solution (O(N^2)):
i1
moves to j1
and family at family at i2
moves to j2
, and i1 < i2
, then j1 < j2
j1
is in between i1
and i2
, or j1
is after i2
j1 < j2
gives a lower time'1 indexed
occupied[]
and empty[]
respectivelyoccupied[]
= K
occupied[i]
= index of 'i^th' occupied houseempty[]
= L
empty[ij
= index of 'j^th' empty housedist[][]
Dist[i][j]
= Distance between i^th
occupied house and j^th
empty house .Dist[][]
= K*L
dp[i][j]
= minimum time taken for movement of all families residing till i^th
occupied house to a empty house such that family on the i^th
occupied house moves to a empty house on or before j^th
empty houseInitialization:
dp[i][j] = INF
dp[1][1] = dist[1][1]
dp[1][j] = min(dp[1][j-1],dist[1][j])
Updation:
dp[i][j] = min(dp[i][j-1],dp[i-1][j-1]+dist[i][j])
final answer:
dp[K][i]
, 1 <= i <= L