File tree 3 files changed +64
-0
lines changed
3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ A repository containing link of good interview questions.
18
18
* [ Reducing Dishes] ( https://leetcode.com/problems/reducing-dishes/ ) ,[ Solution] ( https://github.com/TheSYNcoder/InterviewPrep/blob/master/Solutions/reducing-dishes.cpp )
19
19
* [ K-different subarrays] ( https://leetcode.com/articles/subarrays-with-k-different-integers/ )
20
20
* [ 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 )
21
22
* [ Distinct Subsequences] ( https://leetcode.com/problems/distinct-subsequences-ii/ )
22
23
* [ Minimum Cost for Tickets [ DP] ] ( https://leetcode.com/problems/minimum-cost-for-tickets/ )
23
24
* [ Valid perfect square [ Bin Search]] ( https://leetcode.com/problems/valid-perfect-square/ ) . [ Solution] ( Solutions/is_perfect_square.cpp ) (Little tricky implementation)
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments