0% found this document useful (0 votes)
49 views8 pages

AP - Complex Som

Uploaded by

Shantanu Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views8 pages

AP - Complex Som

Uploaded by

Shantanu Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Complex Problem
Advance Programming Lab – II

Student Name: Somya Kukreti UID: 21BCS8652


Branch: BE-CSE Section/Group:CC-610/B

Problem 1: Given an array arr[] of size N, the task is to find the length of the
Longest Increasing Subsequence (LIS) i.e., the longest possible subsequence in
which the elements of the subsequence are sorted in increasing order.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int longestIncreasingSubsequence(vector<int> &arr)


{
int n = [Link]();
vector<int> lis(n, 1);

for (int i = 1; i < n; ++i)


{
for (int j = 0; j < i; ++j)
{
if (arr[i] > arr[j])
{
lis[i] = max(lis[i], lis[j] + 1);
}
}
}

return *max_element([Link](), [Link]());


}

int main()
{ vector<int> arr = {3, 10, 2, 1, 20};
cout << "Length of Longest Increasing Subsequence: " <<
longestIncreasingSubsequence(arr) << endl;
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 2: Given a Binary Tree with all unique values and two nodes value, n1 and
n2. The task is to find the lowest common ancestor of the given two nodes. We may
assume that either both n1 and n2 are present in the tree or none of them are present.
LCA: It is the first common ancestor of both the nodes n1 and n2 from bottom of
tree.

#include <iostream>
using namespace std;

struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q)


{
if (root == NULL)
return NULL;

if (root == p || root == q)
return root;

TreeNode *left = lowestCommonAncestor(root->left, p, q);


TreeNode *right = lowestCommonAncestor(root->right, p, q);

if (left && right)


return root;

return (left != NULL) ? left : right;


}

int main() {
cout << "code Executed by Vikash and UID-21BCS8773\n";
TreeNode *root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = NULL;
root->left->right = NULL;
root->right->left = NULL;
root->right->right = NULL;

TreeNode *n1 = root->left;


TreeNode *n2 = root->right;

TreeNode *lca = lowestCommonAncestor(root, n1, n2);


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
cout << "Lowest Common Ancestor: " << lca->val << endl;

delete root->left;
delete root->right;
delete root;

return 0;
}

Problem 3: Given two strings, string, and pattern, the task is to find the smallest
substring in string containing all characters of pattern.

#include <iostream>
#include <unordered_map>
#include <climits>
using namespace std;

string smallestSubstringContainingPattern(string str, string pattern)


{
int n = [Link]();
int m = [Link]();

if (m > n)
return "";

unordered_map<char, int> patternFreq, strFreq;


for (char c : pattern)
{
patternFreq[c]++;
}

int left = 0, minLen = INT_MAX, count = 0, minLeft = 0;

for (int right = 0; right < n; ++right)


{
char currChar = str[right];
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if ([Link](currChar) != [Link]())
{
strFreq[currChar]++;

if (strFreq[currChar] <= patternFreq[currChar])


{
count++;
}

while (count == m)
{
int windowLen = right - left + 1;
if (windowLen < minLen)
{
minLen = windowLen;
minLeft = left;
}

char leftChar = str[left];

if ([Link](leftChar) != [Link]())
{
strFreq[leftChar]--;
if (strFreq[leftChar] < patternFreq[leftChar])
{
count--;
}
}
left++;
}}}
if (minLen == INT_MAX)
return "";
return [Link](minLeft, minLen);
}

int main()
{
cout << "Code Executed by Vikash and UID-21BCS8773\n";
string str1 = "this is a test string";
string pattern1 = "tist";
cout << "Output for string1: " << smallestSubstringContainingPattern(str1,
pattern1) << endl;

string str2 = "geeksforgeeks";


string pattern2 = "ork";
cout << "Output for string2: " << smallestSubstringContainingPattern(str2,
pattern2) << endl;

return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 4: Given an integer array nums of unique elements, return all possible
subsets (the power set). The solution set must not contain duplicate subsets. Return
the solution in any order.

#include <iostream>
#include <vector>
using namespace std;

int minEdit(string word1, string word2)


{
int m = [Link]();
int n = [Link]();

vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

for (int i = 0; i <= m; ++i)


{
for (int j = 0; j <= n; ++j)
{
if (i == 0)
dp[i][j] = j;
else if (j == 0)
dp[i][j] = i;
else if (word1[i - 1] == word2[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] =1+min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j -1]));
}}

return dp[m][n];
}

int main()
{
cout << "Code Executed by Vikash and UID-21BCS8773\n";
string str1 = "geek";
string str2 = "gesek";
cout << "Output for str1 and str2: " << minEdit(str1, str2) << endl;
str1 = "cat";
str2 = "cut";
cout << "Output for str1 and str2: " << minEdit(str1, str2) << endl;
str1 = "sunday";
str2 = "saturday";
cout << "Output for str1 and str2: " << minEdit(str1, str2) << endl;

return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 5: Given an array arr[] of size N and an integer K. Find the maximum for
each and every contiguous subarray of size K.

#include <iostream>
#include <deque>
#include <vector>
using namespace std;

vector<int> maxOfSubarrays(int arr[], int n, int k)


vector<int> result;
deque<int> dq;

for (int i = 0; i < n; ++i){


while (![Link]() && [Link]() <= i - k)
dq.pop_front();
while (![Link]() && arr[[Link]()] < arr[i])
dq.pop_back();
dq.push_back(i);

if (i >= k - 1)
result.push_back(arr[[Link]()]);
}
return result;
}

int main(){
cout << "Code Executed by Vikash and UID-21BCS8773\n";

int arr1[] = {1, 2, 3, 1, 4, 5, 2, 3, 6};


int n1 = sizeof(arr1) / sizeof(arr1[0]);
int k1 = 3;
vector<int> result1 = maxOfSubarrays(arr1, n1, k1);
for (int num : result1)
{
cout << num << " ";
}
cout << endl;
int arr2[] = {8, 5, 10, 7, 9, 4, 15, 12, 90, 13};
int n2 = sizeof(arr2) / sizeof(arr2[0]);
int k2 = 4;
vector<int> result2 = maxOfSubarrays(arr2, n2, k2);
for (int num : result2){
cout << num << " ";
}
cout << endl;
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 6: Largest-rectangle-in-histogram.
class Solution {
public:
int largestRectangleArea(vector<int>& heights)
{ int ans = 0;
stack<int> stack;

for (int i = 0; i <= [Link](); ++i)


{ while (![Link]() &&
(i == [Link]() || heights[[Link]()] > heights[i]))
{ const int h = heights[[Link]()];
[Link]();
const int w = [Link]() ? i : i - [Link]() - 1;
ans = max(ans, h * w);
}
[Link](i);
}
return ans;
}
};

Problem 7: Binary Tree Maximum Path Sum.


class Solution {
public:
int maxPathSum(TreeNode* root)
{ int ans = INT_MIN;
maxPathSumDownFrom(root, ans);
return ans;
}
private:
int maxPathSumDownFrom(TreeNode* root, int& ans)
{ if (root == nullptr)
return 0;

const int l = max(0, maxPathSumDownFrom(root->left, ans));


const int r = max(0, maxPathSumDownFrom(root->right, ans));
ans = max(ans, root->val + l + r);
return root->val + max(l, r);
}};

Ques – 8 Best Time to Buy and Sell Stock IV


class Solution {
public:
int maxProfit(int k, vector<int>& prices)
{ if (k >= [Link]() / 2) {
int sell = 0;
int hold = INT_MIN;

for (const int price : prices)


{ sell = max(sell, hold +
price); hold = max(hold, sell -
price);
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
return sell;
}
vector<int> sell(k + 1);
vector<int> hold(k + 1, INT_MIN);
for (const int price : prices)
for (int i = k; i > 0; --i) {
sell[i] = max(sell[i], hold[i] + price);
hold[i] = max(hold[i], sell[i - 1] - price);
return sell[k];
}
};

Problem 9: Dungeon Game.


class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon)
{ const int m = [Link]();
const int n = dungeon[0].size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, INT_MAX));
dp[m][n - 1] = 1;
dp[m - 1][n] = 1;

for (int i = m - 1; i >= 0; --i)


for (int j = n - 1; j >= 0; --j) {
dp[i][j] = min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]; dp[i]
[j] = max(dp[i][j], 1);
}
return dp[0][0];
}
};

Problem 10: Candy


class Solution {
public:
int candy(vector<int>& ratings)
{ const int n = [Link]();
int ans = 0;
vector<int> l(n, 1);
vector<int> r(n, 1);

for (int i = 1; i < n; ++i)


if (ratings[i] > ratings[i - 1])
l[i] = l[i - 1] + 1;

for (int i = n - 2; i >= 0; --i)


if (ratings[i] > ratings[i + 1])
r[i] = r[i + 1] + 1;

for (int i = 0; i < n; ++i)


ans += max(l[i], r[i]);

return ans;
}
};

You might also like