-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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 #1441 from SohilP25/master
DSA & CP Interview Question For Preparation
- Loading branch information
Showing
5 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
Program's_Contributed_By_Contributors/C++/5_Sort_Colors.cpp
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,66 @@ | ||
// Problem Question : https://leetcode.com/problems/sort-colors/ | ||
// WARN : DUTCH NATIONAL FLAG ALGORITHM IS HERE (><) | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define ll long long | ||
#define endl '\n' | ||
|
||
//Solution 1(DUTCH NATONAL FLAG ALGO.) | ||
class Solution { | ||
public: | ||
void sortColors(vector<int>& nums) { | ||
int low = 0; | ||
int mid = 0; | ||
int hi = nums.size() - 1; | ||
|
||
while(mid<=hi) | ||
{ | ||
switch(nums[mid]) | ||
{ | ||
// If Element is 0 | ||
case 0 : | ||
swap(nums[mid++],nums[low++]); | ||
break; | ||
// If Element is 1 | ||
case 1 : | ||
mid++; | ||
break; | ||
// If Element is 2 | ||
case 2 : | ||
swap(nums[mid],nums[hi--]); | ||
break; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
|
||
// Solution 2(MY SOLN.) | ||
class Solution2 { | ||
public: | ||
void sortColors(vector<int>& nums) { | ||
int c0=0,c1=0; | ||
for(auto i : nums) | ||
{ | ||
if(i==0) | ||
c0++; | ||
else if(i==1) | ||
c1++; | ||
} | ||
for(int i = 0 ; i < c0 ; i++){ | ||
nums[i] = 0; | ||
} | ||
for(int i = c0 ; i < (c0+c1) ; i++){ | ||
nums[i] = 1; | ||
} | ||
for(int i = (c0+c1); i < nums.size() ; i++ ){ | ||
nums[i] = 2; | ||
} | ||
} | ||
}; | ||
|
||
int main(){ | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
return 0; | ||
} |
44 changes: 44 additions & 0 deletions
44
Program's_Contributed_By_Contributors/C++/6_stock_buy_and_sell.cpp
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,44 @@ | ||
// Problem Question : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define ll long long | ||
#define endl '\n' | ||
// Solution 1 : Optimal | ||
class Solution { | ||
public: | ||
int maxProfit(vector<int>& prices) { | ||
int mini = INT_MAX; | ||
int profit = 0; | ||
for(int i = 0 ; i < prices.size() ; i++) | ||
{ | ||
mini = min(mini,prices[i]); | ||
profit = max(profit,prices[i] - mini); | ||
} | ||
return profit; | ||
} | ||
}; | ||
|
||
// Solution 2 : Bruteforce | ||
class Solution { | ||
public: | ||
int maxProfit(vector<int>& prices) { | ||
int profit = 0; | ||
for(int i = 0 ; i < prices.size() ; i++) | ||
{ | ||
for(int j = i+1 ; j<prices.size() ; j++) | ||
{ | ||
if(prices[j] > prices[i]){ | ||
profit = max(prices[j] - prices[i],profit); | ||
} | ||
} | ||
} | ||
return profit; | ||
|
||
} | ||
}; | ||
int main(){ | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
return 0; | ||
} |
28 changes: 28 additions & 0 deletions
28
Program's_Contributed_By_Contributors/C++/7_rotate_image.cpp
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,28 @@ | ||
//Problem Question : https://leetcode.com/problems/rotate-image/ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define ll long long | ||
#define endl '\n' | ||
// Solution : | ||
class Solution { | ||
public: | ||
void rotate(vector<vector<int>>& matrix) { | ||
int n = matrix.size(); | ||
//First Transpose matrix means {i,j} <-> {j,i} | ||
for(int i = 0; i < n ; i++){ | ||
for(int j = 0 ; j < i ; j++){ | ||
swap(matrix[i][j],matrix[j][i]); | ||
} | ||
} | ||
//Reverse every row | ||
for(int i = 0 ; i < n ; i++){ | ||
reverse(matrix[i].begin(),matrix[i].end()); | ||
} | ||
} | ||
}; | ||
int main(){ | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
return 0; | ||
} |
38 changes: 38 additions & 0 deletions
38
Program's_Contributed_By_Contributors/C++/8_Merge_intervals.cpp
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,38 @@ | ||
//Problem Question : https://leetcode.com/problems/merge-intervals/ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define ll long long | ||
#define endl '\n' | ||
|
||
//solution : | ||
|
||
class Solution { | ||
public: | ||
vector<vector<int>> merge(vector<vector<int>>& intervals) { | ||
vector<vector<int>> v; | ||
if( intervals.size() ==0){ | ||
return v; | ||
} | ||
sort(intervals.begin(),intervals.end()); | ||
vector<int> temp = intervals[0]; | ||
for(auto it : intervals){ | ||
if(temp[1] >= it[0]){ | ||
temp[1] = max(temp[1], it[1]); | ||
} | ||
else{ | ||
v.push_back(temp); | ||
temp = it; | ||
} | ||
} | ||
v.push_back(temp); | ||
return v; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
return 0; | ||
} |
91 changes: 91 additions & 0 deletions
91
Program's_Contributed_By_Contributors/C++/9_Merge_sorted_array.cpp
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,91 @@ | ||
//Problem Question : https://leetcode.com/problems/merge-sorted-array/ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define ll long long | ||
#define endl '\n' | ||
// Time complexity: O(logn) | ||
// Space Complexity: O(1) | ||
//Solution 1 : | ||
class Solution { | ||
public: | ||
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { | ||
for(int k = m ; k <( m+n) ; k++ ) | ||
{ | ||
nums1[k] = nums2[k-m]; | ||
} | ||
if((m == 0) || (n == 0) ) | ||
{ | ||
return; | ||
} | ||
int gap = ceil(((m+n)/2.0)); | ||
while(gap > 0 ){ | ||
int i = 0 ; | ||
int j =gap; | ||
while(j < (m+n)) | ||
{ | ||
if(nums1[i] > nums1[j]) | ||
{ | ||
swap(nums1[i],nums1[j]); | ||
} | ||
i++; | ||
j++; | ||
} | ||
if(gap == 1 ){ | ||
break;} | ||
gap = ceil(gap/2.0); | ||
} | ||
} | ||
}; | ||
|
||
// Time complexity: O(n*m) + O(n) | ||
// Space Complexity: O(1) | ||
//Solution 2 : | ||
class Solution { | ||
public: | ||
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { | ||
for(int i = m ; i <( m+n) ; i++ ) | ||
{ | ||
nums1[i] = nums2[i-m]; | ||
} | ||
if((nums1.size() == 0) || (nums2.size() == 0) ) | ||
{ | ||
return; | ||
} | ||
int i = 0; | ||
while(i != m ) | ||
{ | ||
if(nums1[i] > nums1[m]){ | ||
swap(nums1[i],nums1[m]); | ||
sort(nums1.begin() + m ,nums1.end()); | ||
} | ||
i++; | ||
} | ||
} | ||
}; | ||
|
||
//BRUTEFORCE APPROCH : IF YOU DON'T WANT TO MAKE COMPLEX ;) | ||
//Solution 3 : | ||
// Time complexity: O(n*log(n))+O(n) | ||
// Space Complexity: O(1) | ||
class Solution { | ||
public: | ||
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { | ||
for(int i = m ; i <( m+n) ; i++ ) | ||
{ | ||
nums1[i] = nums2[i-m]; | ||
} | ||
if((nums1.size() == 0) || (nums2.size() == 0) ) | ||
{ | ||
return; | ||
} | ||
sort(nums1.begin(),nums1.end()); | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
ios::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
return 0; | ||
} |