Skip to content

Commit c8de950

Browse files
authored
Merge pull request #2 from TheSYNcoder/master
leetcode problems added
2 parents 99327d8 + 84738cd commit c8de950

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ A repository containing link of good interview questions.
1818
* [Reducing Dishes](https://leetcode.com/problems/reducing-dishes/),[Solution](https://github.com/TheSYNcoder/InterviewPrep/blob/master/Solutions/reducing-dishes.cpp)
1919
* [K-different subarrays](https://leetcode.com/articles/subarrays-with-k-different-integers/)
2020
* [Possible Bipartition](https://leetcode.com/problems/possible-bipartition/) , [Solution](https://github.com/TheSYNcoder/InterviewPrep/blob/master/Solutions/Possible%20Bipartition.cpp)
21+
* [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)
2122
* [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences-ii/)
2223
* [Minimum Cost for Tickets [DP] ](https://leetcode.com/problems/minimum-cost-for-tickets/)
2324
* [Valid perfect square [Bin Search]](https://leetcode.com/problems/valid-perfect-square/). [Solution](Solutions/is_perfect_square.cpp) (Little tricky implementation)

Solutions/Course_Schedule.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
//The solution basically uses the concept of graph coloring
5+
// 0: node has not been visited
6+
// 2: node is in processing mode
7+
// 1: node has allready been processed
8+
9+
class Solution {
10+
private:
11+
bool isCyclic(vector<vector<int>>&adj,vector<int>&visited,int current){
12+
if(visited[current]==2)
13+
return true;
14+
visited[current]=2;
15+
for(int i=0;i<adj[current].size();i++){
16+
if(visited[adj[current][i]]!=1){
17+
if(isCyclic(adj,visited,adj[current][i]))
18+
return true;
19+
}
20+
}
21+
visited[current]=1;
22+
return false;
23+
}
24+
public:
25+
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
26+
vector<vector<int>>adj(numCourses);
27+
for(int i=0;i<prerequisites.size();i++){
28+
adj[prerequisites[i][0]].push_back(prerequisites[i][1]);
29+
}
30+
vector<int>visited(numCourses,0);
31+
for(int i=0;i<numCourses;i++){
32+
if(visited[i]==0){
33+
if(isCyclic(adj,visited,i)){
34+
return false;
35+
}
36+
}
37+
}
38+
return true;
39+
}
40+
};

Solutions/Russian_Doll_Envelope.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// Problem Statement : https://leetcode.com/problems/russian-doll-envelopes/
5+
6+
//Similar to LIS with a tweak.
7+
int maxEnvelopes(vector<vector<int>>& envelopes) {
8+
sort(envelopes.begin(),envelopes.end());
9+
int n=envelopes.size(),res=1;
10+
vector<int>dp(n,1);
11+
for(int i=1;i<n;i++){
12+
for(int j=0;j<i;j++){
13+
if(envelopes[i][0]>envelopes[j][0] && envelopes[i][1]>envelopes[j][1]){
14+
dp[i] = max(dp[i],1+dp[j]);
15+
}
16+
}
17+
res = max(res,dp[i]);
18+
}
19+
if(n==0) return 0;
20+
else return res;
21+
}
22+
23+

0 commit comments

Comments
 (0)