-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from TheSYNcoder/master
leetcode problems added
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|