Skip to content

Commit

Permalink
Merge branch 'Ayushsinhahaha:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Saurabh0625 authored Oct 28, 2022
2 parents c1d8191 + b706c59 commit c0c1467
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 0 deletions.
17 changes: 17 additions & 0 deletions First_Missing_Positive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Given an unsorted integer array nums, return the smallest missing positive integer.

class Solution
{
public:
int firstMissingPositive(vector<int>& A) {
int n = A.size();
for(int i = 0 ; i < n; i++)
while(A[i] > 0 and A[i]<n and A[i] != A[A[i]-1])
swap(A[i], A[A[i]-1]);

for(int i = 0 ; i < n; i++)
if(A[i] != i+1)
return i+1;
return n+1;
}
};
47 changes: 47 additions & 0 deletions LIS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<bits/stdc++.h>
using namespace std;

int CeilIndex(vector<int>& v, int l, int r, int key)
{
while (r - l > 1) {
int m = l + (r - l) / 2;
if (v[m] >= key)
r = m;
else
l = m;
}
return r;
}

int LongestIncreasingSubsequenceLength(vector<int>& v)
{
if (v.size() == 0)
return 0;

vector<int> tail(v.size(), 0);
int length = 1;

tail[0] = v[0];
for (size_t i = 1; i < v.size(); i++) {
if (v[i] < tail[0])
tail[0] = v[i];
else if (v[i] > tail[length - 1])
tail[length++] = v[i];
else
tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i];
}

return length;
}

int main() {
int n;
cin>>n;
vector<int>v(n);
for(int i=0; i<n; i++)
cin>>v[i];

cout<<LongestIncreasingSubsequenceLength(v);

return 0;
}
36 changes: 36 additions & 0 deletions Sieve of Eratosthenes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>

using namespace std;
vector<int> sieveOfEratosthenes(int N)
{
vector<int> result;
vector<int> dp(N+1,0);
dp[1]=1;
for(int i=2; i<dp.size(); i++)
{
if(dp[i]==0){
result.push_back(i);
for(int j=2; j*i<=N; j++)
{
dp[j*i]=1;

}
}
}
return result;
}

int main()
{
cout<<"please enter the number"<<endl;
int n;
cin>>n;
cout<<endl;
vector<int> result= sieveOfEratosthenes(n);
for(auto x: result)
{
cout<<x<<" ";
}

return 0;
}
16 changes: 16 additions & 0 deletions Unique_Paths.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m,vector<int> (n,1)); // 2d array with all values 1
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
return dp[m-1][n-1];
}
};
// 1. First of all, we will make a 2d array with m rows and n columns storing value 1.
// 2. vector(n, 1) defines an array of size n with each value 1.
// 3. Iterate the loop to add values of above and left value. Because the robot can come at a block either from above or from left.
// 4. Return the value of dp at the finish point i.e., dp[m-1][n-1]
26 changes: 26 additions & 0 deletions char_frequency_count.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int count_set_bit(int n)
{
int mask=1;
int ans=0;
while(mask<=n)
{
if(mask&n)
{
ans++;
}
mask=mask<<1;
}
return ans;
}
int main()
{
cout<<"please enter the number:-";
int n;
cin>>n;
int ans=count_set_bit(n);
cout<<ans<<endl;

}
41 changes: 41 additions & 0 deletions kadane_algo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// C++ program to print largest contiguous array sum using KADANE ALGORITHM
#include<iostream>
#include<climits>
using namespace std;

int maxSubArraySum(int a[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0,
start =0, end = 0, s=0;

for (int i=0; i< size; i++ )
{
max_ending_here += a[i];

if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}

if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
cout << "Maximum contiguous sum is "
<< max_so_far << endl;
cout << "Starting index "<< start
<< endl << "Ending index "<< end << endl;
}

/*Driver program to test maxSubArraySum*/
int main()
{
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int n = sizeof(a)/sizeof(a[0]);
int max_sum = maxSubArraySum(a, n);
return 0;
}
87 changes: 87 additions & 0 deletions kmp_algotihm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// C++ program for implementation of KMP pattern searching algorithm
#include <bits/stdc++.h>

void computeLPSArray(char* pat, int M, int* lps);

// Prints occurrences of txt[] in pat[]
void KMPSearch(char* pat, char* txt)
{
int M = strlen(pat);
int N = strlen(txt);

// create lps[] that will hold the longest prefix suffix
// values for pattern
int lps[M];

// Preprocess the pattern (calculate lps[] array)
computeLPSArray(pat, M, lps);

int i = 0; // index for txt[]
int j = 0; // index for pat[]
while ((N - i) >= (M - j)) {
if (pat[j] == txt[i]) {
j++;
i++;
}

if (j == M) {
printf("Found pattern at index %d ", i - j);
j = lps[j - 1];
}

// mismatch after j matches
else if (i < N && pat[j] != txt[i]) {
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}

// Fills lps[] for given pattern pat[0..M-1]
void computeLPSArray(char* pat, int M, int* lps)
{
// length of the previous longest prefix suffix
int len = 0;

lps[0] = 0; // lps[0] is always 0

// the loop calculates lps[i] for i = 1 to M-1
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
// This is tricky. Consider the example.
// AAACAAAA and i = 7. The idea is similar
// to search step.
if (len != 0) {
len = lps[len - 1];

// Also, note that we do not increment
// i here
}
else // if (len == 0)
{
lps[i] = 0;
i++;
}
}
}
}

// Driver program to test above function
int main()
{
char txt[] = "CDCDABABDABACCDCDDABABCABABCDCD";
char pat[] = "ABABCABAB";
KMPSearch(pat, txt);
return 0;
}
24 changes: 24 additions & 0 deletions max_path_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
private :
int solve(TreeNode* root, int& res){
if(root==nullptr){
return 0;
}

int l=solve(root->left,res);
int r=solve(root->right,res);


int temp=max(max(l,r)+root->val,root->val);
int ans=max(temp, l+r+root->val);
res=max(res,ans);
return temp;
}
public:
int maxPathSum(TreeNode* root ){
int res=INT_MIN;
solve(root,res);
return res;

}
};
45 changes: 45 additions & 0 deletions minimum-window-substring
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution {
public:
string minWindow(string s, string t) {
unordered_map<char,int>um;
for(int i=0;i<t.size();i++){
um[t[i]]++;
}
int count=um.size();
string ans="";
int ans_size=INT_MAX;
int i=0,j=0;
while(j<s.size()){
if(um.count(s[j])){
um[s[j]]--;
if(um[s[j]]==0)
count--;
}
if(count>0){
j++;
}
else if(count==0){
if(ans_size > j-i+1){
ans=s.substr(i, j-i+1);
ans_size=j-i+1;
}

while(count==0){
if(ans_size > j-i+1){
ans=s.substr(i, j-i+1);
ans_size=j-i+1;
}

if(um.count(s[i])){
um[s[i]]++;
if(um[s[i]]==1)
count++;
}
i++;
}
j++;
}
}
return ans;
}
};
49 changes: 49 additions & 0 deletions painter_partition_dp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// A DP based CPP program for painter's partition problem
#include <climits>
#include <iostream>
using namespace std;


int sum(int arr[], int from, int to)
{
int total = 0;
for (int i = from; i <= to; i++)
total += arr[i];
return total;
}

int findMax(int arr[], int n, int k)
{
int dp[k + 1][n + 1] = { 0 };

for (int i = 1; i <= n; i++)
dp[1][i] = sum(arr, 0, i - 1);

for (int i = 1; i <= k; i++)
dp[i][1] = arr[0];

for (int i = 2; i <= k; i++) {
for (int j = 2; j <= n; j++) {

int best = INT_MAX;

for (int p = 1; p <= j; p++)
best = min(best, max(dp[i - 1][p],
sum(arr, p, j - 1)));

dp[i][j] = best;
}
}

return dp[k][n];
}

// driver function
int main()
{
int arr[] = { 10, 20, 60, 50, 30, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
cout << findMax(arr, n, k) << endl;
return 0;
}
Loading

0 comments on commit c0c1467

Please sign in to comment.