diff --git a/Program's_Contributed_By_Contributors/C++/5_Sort_Colors.cpp b/Program's_Contributed_By_Contributors/C++/5_Sort_Colors.cpp new file mode 100644 index 0000000000..3168b2e1f0 --- /dev/null +++ b/Program's_Contributed_By_Contributors/C++/5_Sort_Colors.cpp @@ -0,0 +1,66 @@ +// Problem Question : https://leetcode.com/problems/sort-colors/ +// WARN : DUTCH NATIONAL FLAG ALGORITHM IS HERE (><) +#include +using namespace std; +#define ll long long +#define endl '\n' + +//Solution 1(DUTCH NATONAL FLAG ALGO.) +class Solution { +public: + void sortColors(vector& 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& 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; +} \ No newline at end of file diff --git a/Program's_Contributed_By_Contributors/C++/6_stock_buy_and_sell.cpp b/Program's_Contributed_By_Contributors/C++/6_stock_buy_and_sell.cpp new file mode 100644 index 0000000000..4edbbc3eba --- /dev/null +++ b/Program's_Contributed_By_Contributors/C++/6_stock_buy_and_sell.cpp @@ -0,0 +1,44 @@ +// Problem Question : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ +#include +using namespace std; +#define ll long long +#define endl '\n' +// Solution 1 : Optimal +class Solution { +public: + int maxProfit(vector& 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& prices) { + int profit = 0; + for(int i = 0 ; i < prices.size() ; i++) + { + for(int j = i+1 ; j prices[i]){ + profit = max(prices[j] - prices[i],profit); + } + } + } + return profit; + + } +}; +int main(){ +ios::sync_with_stdio(0); +cin.tie(0); + + return 0; +} \ No newline at end of file diff --git a/Program's_Contributed_By_Contributors/C++/7_rotate_image.cpp b/Program's_Contributed_By_Contributors/C++/7_rotate_image.cpp new file mode 100644 index 0000000000..2efe986231 --- /dev/null +++ b/Program's_Contributed_By_Contributors/C++/7_rotate_image.cpp @@ -0,0 +1,28 @@ +//Problem Question : https://leetcode.com/problems/rotate-image/ +#include +using namespace std; +#define ll long long +#define endl '\n' +// Solution : +class Solution { +public: + void rotate(vector>& 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; +} \ No newline at end of file diff --git a/Program's_Contributed_By_Contributors/C++/8_Merge_intervals.cpp b/Program's_Contributed_By_Contributors/C++/8_Merge_intervals.cpp new file mode 100644 index 0000000000..b12eb9ff54 --- /dev/null +++ b/Program's_Contributed_By_Contributors/C++/8_Merge_intervals.cpp @@ -0,0 +1,38 @@ +//Problem Question : https://leetcode.com/problems/merge-intervals/ +#include +using namespace std; +#define ll long long +#define endl '\n' + +//solution : + +class Solution { +public: + vector> merge(vector>& intervals) { + vector> v; + if( intervals.size() ==0){ + return v; + } + sort(intervals.begin(),intervals.end()); + vector 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; +} \ No newline at end of file diff --git a/Program's_Contributed_By_Contributors/C++/9_Merge_sorted_array.cpp b/Program's_Contributed_By_Contributors/C++/9_Merge_sorted_array.cpp new file mode 100644 index 0000000000..0a6cc81d66 --- /dev/null +++ b/Program's_Contributed_By_Contributors/C++/9_Merge_sorted_array.cpp @@ -0,0 +1,91 @@ +//Problem Question : https://leetcode.com/problems/merge-sorted-array/ +#include +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& nums1, int m, vector& 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& nums1, int m, vector& 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& nums1, int m, vector& 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; +} \ No newline at end of file