Skip to content

Commit

Permalink
Merge pull request #2 from TheSYNcoder/master
Browse files Browse the repository at this point in the history
leetcode problems added
  • Loading branch information
skm2000 committed Jun 2, 2020
2 parents 99327d8 + 84738cd commit c8de950
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ A repository containing link of good interview questions.
* [Reducing Dishes](https://leetcode.com/problems/reducing-dishes/),[Solution](https://github.com/TheSYNcoder/InterviewPrep/blob/master/Solutions/reducing-dishes.cpp)
* [K-different subarrays](https://leetcode.com/articles/subarrays-with-k-different-integers/)
* [Possible Bipartition](https://leetcode.com/problems/possible-bipartition/) , [Solution](https://github.com/TheSYNcoder/InterviewPrep/blob/master/Solutions/Possible%20Bipartition.cpp)
* [Course Schedule](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/538/week-5-may-29th-may-31st/3344/), [Solution](https://github.com/TheSYNcoder/InterviewPrep/blob/master/Solutions/Course_Schedule.cpp)
* [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences-ii/)
* [Minimum Cost for Tickets [DP] ](https://leetcode.com/problems/minimum-cost-for-tickets/)
* [Valid perfect square [Bin Search]](https://leetcode.com/problems/valid-perfect-square/). [Solution](Solutions/is_perfect_square.cpp) (Little tricky implementation)
Expand Down
40 changes: 40 additions & 0 deletions Solutions/Course_Schedule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<bits/stdc++.h>
using namespace std;

//The solution basically uses the concept of graph coloring
// 0: node has not been visited
// 2: node is in processing mode
// 1: node has allready been processed

class Solution {
private:
bool isCyclic(vector<vector<int>>&adj,vector<int>&visited,int current){
if(visited[current]==2)
return true;
visited[current]=2;
for(int i=0;i<adj[current].size();i++){
if(visited[adj[current][i]]!=1){
if(isCyclic(adj,visited,adj[current][i]))
return true;
}
}
visited[current]=1;
return false;
}
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>>adj(numCourses);
for(int i=0;i<prerequisites.size();i++){
adj[prerequisites[i][0]].push_back(prerequisites[i][1]);
}
vector<int>visited(numCourses,0);
for(int i=0;i<numCourses;i++){
if(visited[i]==0){
if(isCyclic(adj,visited,i)){
return false;
}
}
}
return true;
}
};
23 changes: 23 additions & 0 deletions Solutions/Russian_Doll_Envelope.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<bits/stdc++.h>
using namespace std;

// Problem Statement : https://leetcode.com/problems/russian-doll-envelopes/

//Similar to LIS with a tweak.
int maxEnvelopes(vector<vector<int>>& envelopes) {
sort(envelopes.begin(),envelopes.end());
int n=envelopes.size(),res=1;
vector<int>dp(n,1);
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
if(envelopes[i][0]>envelopes[j][0] && envelopes[i][1]>envelopes[j][1]){
dp[i] = max(dp[i],1+dp[j]);
}
}
res = max(res,dp[i]);
}
if(n==0) return 0;
else return res;
}


0 comments on commit c8de950

Please sign in to comment.