Skip to content

Commit

Permalink
MakeMyTrip Coding Round
Browse files Browse the repository at this point in the history
  • Loading branch information
satyajeetramnit committed Jul 23, 2022
1 parent 3a1a24b commit 4d3b777
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 0 deletions.
6 changes: 6 additions & 0 deletions GE Healthcare/Managerial/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"*.rmd": "markdown",
"xstring": "cpp"
}
}
21 changes: 21 additions & 0 deletions MakeMyTrip/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/MinGW/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
63 changes: 63 additions & 0 deletions MakeMyTrip/longestSubsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Problem Description : Longest Special Subsequences
// You are given a string S of the length n consisting of only lowercase alphabets. If the two consecutive characters in a subsequence have a difference that is no more than k, then it is called a Special Subsequence. Find the length of the longest special subsequence.

// INPUT FORMAT :

// Two space separated integers n and k
// String S
// OUTPUT FORMAT :
// Print the length of the longest special subsequence in a new line.

// CONSTRAINTS :
// 1<= n <= (10^5)
// 1<=k<=26

// Sample Input :
// 7 2
// afcbedf

// Sample Output :
// 4

// Explaination :
// One of the longest special subsequence present in afcbedf is a,c,b,d. It is special because |a-c|<=2 , |c-b|<=2 and |b-d|<=2.

#include<bits/stdc++.h>
using namespace std;

int longest_special_subseq(int n, int k, string s){
vector<int> dp(n, 0);
int max_length[26] = {0};

for (int i = 0; i < n; i++){
int curr = s[i] - 'a';
int lower = max(0, curr - k);
int upper = min(25, curr + k);
for (int j = lower; j < upper + 1; j++){
dp[i] = max(dp[i], max_length[j] + 1);
}
max_length[curr] = max(dp[i], max_length[curr]);
}

int ans = 0;
for(int i:dp) ans = max(i, ans);
return ans;
}

int main(){
int t, n, k;
cin >> t;
while(t--){
cin >> n >> k;
string s;
cin >> s;
cout << longest_special_subseq(n, k, s) << endl;
}
}

// Sample Input:
// 1
// 7 2 afcbedg

// Sample Output:
// 4
138 changes: 138 additions & 0 deletions MakeMyTrip/safePaths.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Problem Description - SAFE PATHS
// There are n cities numbered from 1 to n and there are n-1 bidirectional roads such that all cities are connected . There are k trees, each one is in a different city. You are currently in the 1st city. You want to visit city X, such that neither X nor the the cities in the path from 1 to X has a tree. Find out how many such X can you visit (X != 1).

// INPUT FORMAT :
// First line contains two space separted integers n , k representing the number of cities and the number of trees respectively.
// Next n-1 lines : Two integers ui and vi meaning there is a road between cities ui and vi.
// Next line : k integers p1,p2,p3....pk where there is a tree at pi city.

// OUTPUT FORMAT :
// Print a single integer denoting the number of cities you can visit.

// Input Constraints :

// 1<=n<=10^5
// 0<=k<n
// 1<=ui,vi<=n; ui != vi
// 2<=pi<=n

// Sample Input :
// 6 3
// 1 2
// 1 6
// 2 3
// 2 4
// 2 5
// 2 3 4

// Sample Output :
// 1

// Explaination :
// There are total 5 possiblities :

// 1 -> 2
// 1 -> 2 -> 3
// 1 -> 2 -> 4
// 1 -> 2 -> 5
// 1 -> 6
// In the first 4 cases , the second city has a tree, so he can't go to 2nd , 3rd , 4th and 5th city. But in 5th case, none of the city on the simple path has a theif so he can go to 6th city.

// Working Solution :

// The idea is to visit those cities from 1 that don't have a tree by doing a BFS or a DFS.
// Here is a working CPP code that uses a BFS based traversal approach.


#include<bits/stdc++.h>
using namespace std;

int main(){
int n,k;
cin>>n>>k;
vector<int> adj[n+1];
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<int> vis(n+1);
vector<int> A(k);
for(int i=0;i<k;i++)
cin>>A[k], vis[A[k]] = 1;
if(vis[1]==1){
cout << 0 << endl;
return 0;
}
queue<int> q;
q.push(1);
vis[1] = 1;
int ans = 0;
while(!q.empty()){
int f = q.front();
q.pop();
for(auto itr:adj[f]){
if(vis[itr]==0){
vis[itr] = 1;
ans++;
q.push(itr);
}
}
}
cout << ans << endl;
}

// int solution(vector<int> thieves,vector<vector<int>> edges){
// int n = thieves.size();
// vector<int> adj[n+1];
// for(int i=0;i<n-1;i++){
// int a = edges[i][0];
// int b = edges[i][1];
// adj[a].push_back(b);
// adj[b].push_back(a);
// }
// vector<int> vis(n+1);
// vector<int> A(thieves.begin(),thieves.end());
// for(int i=0;i<thieves.size();i++)
// vis[thieves[i]] = 1;
// if(vis[1]==1){
// return 0;
// }
// queue<int> q;
// q.push(1);
// vis[1] = 1;
// int ans = 0;
// while(!q.empty()){
// int f = q.front();
// q.pop();
// for(auto itr:adj[f]){
// if(vis[itr]==0){
// vis[itr] = 1;
// ans++;
// q.push(itr);
// }
// }
// }
// return ans;
// }

// int main(){
// ios::sync_with_stdio(false);
// cin.tie(0);
// int n,k;
// cin>>n>>k;
// vector<vector<int> >edges(n-1,vector<int>(2));
// for(int i_edges=0;i_edges<n-1;i_edges++){
// for(int j_edges=0;j_edges<2;j_edges++){
// cin>>edges[i_edges][j_edges];
// }
// }
// vector<int> thieves(k);
// for(int i_thieves=0;i_thieves<k;i_thieves++){
// cin>>thieves[i_thieves];
// }
// int out_;
// out_ = solution(thieves,edges);
// cout<<out_<<endl;
// }
Binary file added MakeMyTrip/safePaths.exe
Binary file not shown.

0 comments on commit 4d3b777

Please sign in to comment.