Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSA & CP Interview Question For Preparation #1441

Merged
merged 2 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Program's_Contributed_By_Contributors/C++/5_Sort_Colors.cpp
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 Program's_Contributed_By_Contributors/C++/6_stock_buy_and_sell.cpp
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 Program's_Contributed_By_Contributors/C++/7_rotate_image.cpp
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 Program's_Contributed_By_Contributors/C++/8_Merge_intervals.cpp
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 Program's_Contributed_By_Contributors/C++/9_Merge_sorted_array.cpp
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;
}